Created
November 27, 2017 13:39
-
-
Save fedden/d0193fe387682762a7bc9e93c011bc5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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