Skip to content

Instantly share code, notes, and snippets.

@fyr91
Last active October 3, 2019 10:32
Show Gist options
  • Save fyr91/cd6c74609a3ba7fcf652a22f2360fc32 to your computer and use it in GitHub Desktop.
Save fyr91/cd6c74609a3ba7fcf652a22f2360fc32 to your computer and use it in GitHub Desktop.
# mutation
def mutate(lineup, score, prob=0.9):
gene = deepcopy(lineup.gene)
# if probability not greater than 0.9
# flip 0s
if np.random.random(1)[0] <= prob:
zero_idx = np.where(gene==0)[0]
if len(zero_idx) > 0:
idx = np.random.choice(zero_idx)
gene[idx] = 1
return Lineup(score=score, gene=gene)
else:
# if not enough 0s to flip
# return original
return lineup
else:
none_zero_idx = np.where(gene==1)[0]
if len(none_zero_idx) > 1:
idx = np.random.choice(none_zero_idx)
gene[idx] = 1
return Lineup(score=score, gene=gene)
else:
return lineup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment