Neuron 101

Nature-inspired optimization / Selection methods

Türkçe

Plate59

Unit 6 · Topic 5 of 11

Selection methods

Medium, 8 min, 8 steps

0.92A0.55B0.40C0.25D0.12E0.06F
Keyboard: left and right arrows change the step, space plays and pauses.

Step 1 of 8 Population

A population of six

The height of each bar is the fitness of an individual. A is clearly the best, with fitness 0.92; F is the weakest. Selection is the rule for drawing parents from this population. Good individuals should be chosen more often, but if the weak ones are never chosen the population loses its diversity.

Open in Colab
  1. import random
  2. U = [0.92, 0.55, 0.40,
  3. 0.25, 0.12, 0.06]
  4. random.seed(8)
  5. def rulet(p):
  6. r, birikim = random.random(), 0
  7. for i, pi in enumerate(p):
  8. birikim += pi
  9. if r < birikim:
  10. return i
  11. return len(p) - 1
  12. p_rulet = [u / sum(U) for u in U]
  13. print(rulet(p_rulet))
  14. sira = sorted(range(6),
  15. key=lambda i: -U[i])
  16. p_sira = [0] * 6
  17. for s, i in enumerate(sira):
  18. p_sira[i] = (6 - s) / 21
  19. print(rulet(p_sira))
  20. aday = random.choices(range(6),
  21. k=2)
  22. print(max(aday, key=lambda i: U[i]))
toplam
2.30
uA
0.92
pRulet
0.40
pSira
0.29
pTurnuva
0.31
k
2
n
6
r1
0.16
r2
0.63
rs
0.30