Skip to content

Instantly share code, notes, and snippets.

@fyr91
Last active October 3, 2019 10:33
Show Gist options
  • Save fyr91/3fb699f786e884cc3c84a9776d649b37 to your computer and use it in GitHub Desktop.
Save fyr91/3fb699f786e884cc3c84a9776d649b37 to your computer and use it in GitHub Desktop.
# check breedable
def breedable(parents):
# add two gene sequences
# if two gene at the same index is different
# the sum will be 1
# if more than 1 position is different
# then they are breedable
x = parents[0].gene + parents[1].gene
if np.count_nonzero(x == 1) >= 2:
return True
else:
return False
# breed
def breed(parents):
score = min(parents[0].score, parents[1].score)
x = parents[0].gene + parents[1].gene
# get swap index
idx = np.random.choice(np.where(x==1)[0])
# in order not to confuse, create copies
offspring_gene1 = deepcopy(parents[0].gene)
offspring_gene2 = deepcopy(parents[1].gene)
offspring_gene1[idx], offspring_gene2[idx] = offspring_gene2[idx], offspring_gene1[idx]
# new lineup score will inherite from the lowest parent score
# born with a silver spoon in the mouth
lineup1 = Lineup(score=score, gene=offspring_gene1)
lineup2 = Lineup(score=score, gene=offspring_gene2)
return lineup1, lineup2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment