public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
class Chromosome
...
def cross(mate)
cs = crossover_site = rand(@bitstring.length-1)+1
mate.bitstring[cs..-1], @bitstring[cs..-1] = @bitstring[cs..-1], mate.bitstring[cs..-1]
end
 
def self.do_crossings(chrs, mates)
i = 0
completed = []
 
#not very idiomatic, but, the i variable is important
while i < chrs.length
 
#due to the symmetric nature of the data structure we're using
if completed.include? i
i += 1
next
end
chrs[i].cross(chrs[mates[i]])
 
completed.push(mates[i])
i += 1
end
return chrs
 
end
...
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.