1 <?xml version=
"1.0" encoding=
"iso-8859-1"?>
3 PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
8 <title>crossover (Population)
</title>
9 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1" />
10 <link rel=
"stylesheet" href=
"../.././rdoc-style.css" type=
"text/css" media=
"screen" />
12 <body class=
"standalone-code">
13 <pre><span class=
"ruby-comment cmt"># File lib/libgenetics.rb, line
138</span>
14 <span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">crossover
</span>(
<span class=
"ruby-identifier">crossover_rate
</span>)
15 <span class=
"ruby-identifier">parents
</span> =
<span class=
"ruby-ivar">@individuals
</span>.
<span class=
"ruby-identifier">dup
</span> <span class=
"ruby-comment cmt"># genomes that haven't been parents yet
</span>
16 <span class=
"ruby-identifier">next_population
</span> =
<span class=
"ruby-constant">Population
</span>.
<span class=
"ruby-identifier">new
</span>(
<span class=
"ruby-value">0</span>)
17 <span class=
"ruby-keyword kw">while
</span> <span class=
"ruby-identifier">parents
</span>.
<span class=
"ruby-identifier">length
</span> <span class=
"ruby-operator">></span> <span class=
"ruby-value">1</span>
18 <span class=
"ruby-identifier">parent1
</span> =
<span class=
"ruby-identifier">parents
</span>.
<span class=
"ruby-identifier">delete_at
</span>(
<span class=
"ruby-identifier">rand
</span>(
<span class=
"ruby-identifier">parents
</span>.
<span class=
"ruby-identifier">length
</span>))
19 <span class=
"ruby-identifier">parent2
</span> =
<span class=
"ruby-identifier">parents
</span>.
<span class=
"ruby-identifier">delete_at
</span>(
<span class=
"ruby-identifier">rand
</span>(
<span class=
"ruby-identifier">parents
</span>.
<span class=
"ruby-identifier">length
</span>))
20 <span class=
"ruby-keyword kw">if
</span> <span class=
"ruby-identifier">rand
</span> <span class=
"ruby-operator"><</span> <span class=
"ruby-identifier">crossover_rate
</span>
21 <span class=
"ruby-identifier">child1
</span>,
<span class=
"ruby-identifier">child2
</span> =
<span class=
"ruby-identifier">parent1
</span>.
<span class=
"ruby-identifier">crossover
</span> <span class=
"ruby-identifier">parent2
</span>,
<span class=
"ruby-identifier">rand
</span>(
<span class=
"ruby-identifier">parent1
</span>.
<span class=
"ruby-identifier">genome
</span>.
<span class=
"ruby-identifier">length
</span>)
22 <span class=
"ruby-identifier">next_population
</span>.
<span class=
"ruby-identifier">individuals
</span> <span class=
"ruby-operator"><<</span> <span class=
"ruby-identifier">child1
</span> <span class=
"ruby-operator"><<</span> <span class=
"ruby-identifier">child2
</span>
23 <span class=
"ruby-keyword kw">else
</span>
24 <span class=
"ruby-identifier">next_population
</span>.
<span class=
"ruby-identifier">individuals
</span> <span class=
"ruby-operator"><<</span> <span class=
"ruby-identifier">parent1
</span> <span class=
"ruby-operator"><<</span> <span class=
"ruby-identifier">parent2
</span>
25 <span class=
"ruby-keyword kw">end
</span>
26 <span class=
"ruby-keyword kw">end
</span>
27 <span class=
"ruby-identifier">next_population
</span> <span class=
"ruby-operator">+=
</span> <span class=
"ruby-identifier">parents
</span> <span class=
"ruby-comment cmt"># pick up any parents that didn't get a chance to mate
</span>
28 <span class=
"ruby-identifier">next_population
</span>
29 <span class=
"ruby-keyword kw">end
</span></pre>