Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. # mutation
  2. def mutate(lineup, score, prob=0.9):
  3. gene = deepcopy(lineup.gene)
  4. # if probability not greater than 0.9
  5. # flip 0s
  6. if np.random.random(1)[0] <= prob:
  7. zero_idx = np.where(gene==0)[0]
  8. if len(zero_idx) > 0:
  9. idx = np.random.choice(zero_idx)
  10. gene[idx] = 1
  11. return Lineup(score=score, gene=gene)
  12. else:
  13. # if not enough 0s to flip
  14. # return original
  15. return lineup
  16. else:
  17. none_zero_idx = np.where(gene==1)[0]
  18. if len(none_zero_idx) > 1:
  19. idx = np.random.choice(none_zero_idx)
  20. gene[idx] = 1
  21. return Lineup(score=score, gene=gene)
  22. else:
  23. return lineup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement