Skip to content

Instantly share code, notes, and snippets.

@fedden
Created November 27, 2017 13:39
Show Gist options
  • Save fedden/d0193fe387682762a7bc9e93c011bc5e to your computer and use it in GitHub Desktop.
Save fedden/d0193fe387682762a7bc9e93c011bc5e to your computer and use it in GitHub Desktop.
def crossover(dna1, dna2):
# Sanity check.
assert len(dna1) == len(dna2)
# The child is now a copy of dna1.
child = np.copy(dna1)
# A random list of zeros and ones, signifying which
# of the child's indices will become dna2 values (one)
# and which of the indices will remain as dna1's
# values (zero).
dna2_indices = np.random.randint(2, size=child.size)
indices = np.where(dna2_indices)
# Transfer dna2 genetic material to child
child[indices] = dna2[indices]
return child
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment