Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def make_child(w1, w2)
- v1 = w1.path_vector
- v2 = w2.path_vector
- child1 = Path.new
- child2 = Path.new
- splitwood = v1.length / $cross
- slices_v1 = v1.each_slice(splitwood).to_a
- splitwood = v2.length / $cross
- slices_v2 = v2.each_slice(splitwood).to_a
- v1par = slices_v1.select.each_with_index { |v, i| i.even? }.reverse
- v1impar = slices_v1.select.each_with_index { |v, i| i.odd? }.reverse
- v2par = slices_v2.select.each_with_index { |v, i| i.even? }.reverse
- v2impar = slices_v2.select.each_with_index { |v, i| i.odd? }.reverse
- # cross
- while v1par.length > 0 || v2impar.length > 0 do
- if v1par.length > 0
- child1.path_vector << v1par.pop
- # pp = child1.pos
- # break if pp.x == 9 and pp.y == 0
- end
- if v2impar.length > 0
- child1.path_vector << v2impar.pop
- # pp = child1.pos
- # break if pp.x == 9 and pp.y == 0
- end
- end
- while v2par.length > 0 || v1impar.length > 0 do
- if v2par.length > 0
- child2.path_vector << v2par.pop
- # pp = child2.pos
- # break if pp.x == 9 and pp.y == 0
- end
- if v1impar.length > 0
- child2.path_vector << v1impar.pop
- # pp = child2.pos
- # break if pp.x == 9 and pp.y == 0
- end
- end
- # mutações
- mutations = []
- child1.path_vector.each_with_index do |d, i|
- if $random.rand(101) < $muta # 10% de chance
- mutations << {d: d, i: i}
- child1.path_vector[i] = $random.rand(0..3)
- end
- end
- child2.path_vector.each_with_index do |d, i|
- if $random.rand(101) < $muta # 10% de chance
- child2.path_vector[i] = $random.rand(0..3)
- end
- end
- return child1, child2
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement