Advertisement
Guest User

Untitled

a guest
Jan 1st, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from igraph import *
  4. from random import randint
  5. import random
  6.  
  7. def move(g, ants):
  8.     for ant in ants:
  9.         neighbors = g.vs[ant].neighbors()
  10.         pheromoneSum = 0
  11.         for n in neighbors:
  12.             eid = g.get_eid(ant, n.index)
  13.             pheromoneSum += g.es[eid]["pheromone"]
  14.         rand = randint(1, pheromoneSum)
  15.         for n in neighbors:
  16.             eid = g.get_eid(ant, n.index)
  17.             rand -= g.es[eid]["pheromone"]
  18.             if rand <= 0:
  19.                 g.es[eid]["pheromone"] += 1
  20.                 ant = n.index
  21.                 break
  22.     return ants
  23.  
  24. edges = [(0,1), (0,2), (0,3), (1,2), (1,3), (2,3), (3,8), (4,5), (4,6), (4,7), (4,8), (5,6), (5,7), (5,8), (6,7), (6,8), (7,8)]
  25. g = Graph(edges)
  26. g.es["pheromone"] = [1] * len(edges)
  27.  
  28. ants = []    
  29. for i in range(1000):    
  30.     ants.append(random.randint(0, 8))  
  31.  
  32. for i in range(0, 1000):
  33.     ants = move(g, ants)
  34.  
  35. g.vs["label"] = range(0, 9)
  36. g.es["label"] = g.es["pheromone"]
  37. plot(g)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement