Skip to content

Instantly share code, notes, and snippets.

@Poincare
Created July 20, 2012 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Poincare/3151578 to your computer and use it in GitHub Desktop.
Save Poincare/3151578 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment