Created
December 11, 2017 00:22
-
-
Save fedden/d83f76386cc6208ed1807b5a042be48b 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 mutate(dna, mutation_rate): | |
for i in range(len(dna)): | |
if np.random.random_sample() < mutation_rate: | |
ri = np.random.randint(0, 10) | |
while ri == i: | |
ri = np.random.randint(0, 10) | |
dna[i], dna[ri] = dna[ri], dna[i] | |
return dna | |
def crossover(dna1, dna2): | |
cross_index = np.random.randint(1, 9) | |
new_dna1 = dna1[:cross_index].tolist() | |
new_dna2 = [d for d in dna2 if d not in new_dna1] | |
new_dna = np.array(new_dna1 + new_dna2) | |
assert len(new_dna) == len(dna1) | |
assert len(np.unique(new_dna)) == len(np.unique(dna2)) | |
return new_dna |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment