Module: UniformCrossover

Defined in:
lib/charlie/list/list_crossover.rb

Overview

Uniform crossover, returns two children.

Instance Method Summary collapse

Instance Method Details

#cross(parent1, parent2) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/charlie/list/list_crossover.rb', line 37

def cross(parent1,parent2)
  c1 = []; c2=[]
  g1 = parent1.genes; g2 = parent2.genes
  g1.each_with_index{|e,i|
    if rand(2).zero?
      c1 << e; c2 << g2[i]
    else
      c2 << e; c1 << g2[i]
    end
  }
  [c1,c2].map{|x| from_genes(x) }
end