Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from igraph import *
- from random import randint
- import random
- def move(g, ants):
- for ant in ants:
- neighbors = g.vs[ant].neighbors()
- pheromoneSum = 0
- for n in neighbors:
- eid = g.get_eid(ant, n.index)
- pheromoneSum += g.es[eid]["pheromone"]
- rand = randint(1, pheromoneSum)
- for n in neighbors:
- eid = g.get_eid(ant, n.index)
- rand -= g.es[eid]["pheromone"]
- if rand <= 0:
- g.es[eid]["pheromone"] += 1
- ant = n.index
- break
- return ants
- 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)]
- g = Graph(edges)
- g.es["pheromone"] = [1] * len(edges)
- ants = []
- for i in range(1000):
- ants.append(random.randint(0, 8))
- for i in range(0, 1000):
- ants = move(g, ants)
- g.vs["label"] = range(0, 9)
- g.es["label"] = g.es["pheromone"]
- plot(g)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement