Neuron 101

Nature-inspired optimization / Ant colony optimization

Türkçe

Plate63

Unit 6 · Topic 9 of 11

Ant colony optimization

Medium, 9 min, 9 steps

1Istanbul2Izmir3Antalya4Ankara5Kayseri6Samsun7Erzurum8Van9Diyarbakır10Gaziantep
Keyboard: left and right arrows change the step, space plays and pauses.

Step 1 of 9 Pheromone

How ants find the way

Real ants leave a scented trail called pheromone as they walk and choose paths with a strong trail more often. An ant on a short path comes back sooner and refreshes its trail more often, so the short path's trail grows stronger. The algorithm imitates this: between every pair of cities there is a brass pheromone trail, all equal at the start, 0.0027.

Open in Colab
  1. import random
  2. # d(a, b): iki il arası uzaklık, km
  3. # L(t): turun uzunluğu, km
  4. random.seed(1)
  5. a, b, rho = 1, 2.0, 0.5
  6. # 3638: en yakın komşu turu, km
  7. tau = [[10 / 3638] * 10
  8. for _ in range(10)]
  9. en_iyi = list(range(10))
  10. for tur in range(20):
  11. turlar = []
  12. for k in range(10):
  13. t = [k]
  14. while len(t) < 10:
  15. i = t[-1]
  16. js = [j for j in range(10)
  17. if j not in t]
  18. w = [tau[i][j] ** a
  19. * (1 / d(i, j)) ** b
  20. for j in js]
  21. t += random.choices(js, w)
  22. turlar.append(t)
  23. for satir in tau:
  24. for j in range(10):
  25. satir[j] *= 1 - rho
  26. for t in turlar:
  27. for i in range(10):
  28. x, y = t[i - 1], t[i]
  29. tau[x][y] += 1 / L(t)
  30. tau[y][x] += 1 / L(t)
  31. en_iyi = min(turlar + [en_iyi],
  32. key=L)
  33. print(en_iyi, L(en_iyi))