Neuron 101

Nature-inspired optimization / Artificial bee colony

Türkçe

Plate64

Unit 6 · Topic 10 of 11

Artificial bee colony

Medium, 9 min, 10 steps

Height01Best source
Keyboard: left and right arrows change the step, space plays and pauses.

Step 1 of 10 Sources

Food sources

The map shows a surface from above: darker tones are higher. 10 food sources were placed on the surface at random. The quality of a source is the height where it stands. The triangle marks the highest point; the colony does not know where it is.

Open in Colab
  1. import random
  2. # f(p): p noktasının yüksekliği
  3. # k(v): v'yi −5 ile 5 arasına çeker
  4. random.seed(1)
  5. limit = 10
  6. K = [[random.uniform(-5, 5)
  7. for _ in range(2)]
  8. for _ in range(10)]
  9. deneme = [0] * 10
  10. def komsu(i):
  11. j = random.randrange(2)
  12. m = random.randrange(9)
  13. m += m >= i
  14. fi = random.uniform(-1, 1)
  15. v = K[i][:]
  16. x = v[j]
  17. v[j] = k(x + fi * (x - K[m][j]))
  18. if f(v) > f(K[i]):
  19. K[i], deneme[i] = v, 0
  20. else:
  21. deneme[i] += 1
  22. en_iyi = max(K, key=f)
  23. for tur in range(40):
  24. for i in range(10):
  25. komsu(i)
  26. w = [f(p) for p in K]
  27. for _ in range(10):
  28. i = random.choices(
  29. range(10), w)[0]
  30. komsu(i)
  31. en_iyi = max(K + [en_iyi], key=f)
  32. i = deneme.index(max(deneme))
  33. if deneme[i] > limit:
  34. K[i] = [random.uniform(-5, 5)
  35. for _ in range(2)]
  36. deneme[i] = 0
  37. print(en_iyi, f(en_iyi))
tur
0
enIyiDeger
0.45