Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. def make_child(w1, w2)
  2. v1 = w1.path_vector
  3. v2 = w2.path_vector
  4. child1 = Path.new
  5. child2 = Path.new
  6.  
  7. splitwood = v1.length / $cross
  8. slices_v1 = v1.each_slice(splitwood).to_a
  9. splitwood = v2.length / $cross
  10. slices_v2 = v2.each_slice(splitwood).to_a
  11.  
  12. v1par = slices_v1.select.each_with_index { |v, i| i.even? }.reverse
  13. v1impar = slices_v1.select.each_with_index { |v, i| i.odd? }.reverse
  14.  
  15. v2par = slices_v2.select.each_with_index { |v, i| i.even? }.reverse
  16. v2impar = slices_v2.select.each_with_index { |v, i| i.odd? }.reverse
  17.  
  18. # cross
  19. while v1par.length > 0 || v2impar.length > 0 do
  20. if v1par.length > 0
  21. child1.path_vector << v1par.pop
  22. # pp = child1.pos
  23. # break if pp.x == 9 and pp.y == 0
  24. end
  25. if v2impar.length > 0
  26. child1.path_vector << v2impar.pop
  27. # pp = child1.pos
  28. # break if pp.x == 9 and pp.y == 0
  29. end
  30. end
  31. while v2par.length > 0 || v1impar.length > 0 do
  32. if v2par.length > 0
  33. child2.path_vector << v2par.pop
  34. # pp = child2.pos
  35. # break if pp.x == 9 and pp.y == 0
  36. end
  37. if v1impar.length > 0
  38. child2.path_vector << v1impar.pop
  39. # pp = child2.pos
  40. # break if pp.x == 9 and pp.y == 0
  41. end
  42. end
  43.  
  44. # mutações
  45. mutations = []
  46. child1.path_vector.each_with_index do |d, i|
  47. if $random.rand(101) < $muta # 10% de chance
  48. mutations << {d: d, i: i}
  49. child1.path_vector[i] = $random.rand(0..3)
  50. end
  51. end
  52. child2.path_vector.each_with_index do |d, i|
  53. if $random.rand(101) < $muta # 10% de chance
  54. child2.path_vector[i] = $random.rand(0..3)
  55. end
  56. end
  57.  
  58. return child1, child2
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement