Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def runSim(timesteps, pathlen, numants, decay, phval):
- # Helper functions (abstract, abstract, abstract!)
- def foodSourceA(world):
- return world[0]
- def foodSourceB(world):
- return world[pathlen * 2 + 2]
- def colony(world):
- return world[pathlen + 1]
- def firstIndex(world):
- return 0
- def middleMindex(world):
- return pathlen + 1
- def lastIndex(world):
- return pathlen * 2 + 2
- def cellOfAnt(ant):
- return world[ant.location]
- def cellLeftOfAnt(ant):
- return world[leftOf(ant.location)]
- def cellRightOfAnt(ant):
- return world[rightOf(ant.location)]
- def leftOf(index):
- return index - 1 if index != firstIndex(world) else index
- def rightOf(index):
- return index + 1 if index != lastIndex(world) else index
- # Initialize the world
- world = [Cell(i) for i in range(pathlen * 2 + 3)]
- ants = [Ant(pathlen + 1) for i in range(numants)]
- for t in range(timesteps):
- for cell in world:
- cell.pheromone *= 1 - decay
- # Iterate through ants
- for ant in ants:
- # Deposit pheromone
- cellOfAnt(ant).pheromone += phval
- # Take into account special conditions for movement
- if(ant.location == foodSourceA(world)):
- ant.hasFood = True
- ant.location = rightOf(ant.location)
- break
- if(ant.location == foodSourceB(world)):
- ant.hasFood = True
- ant.location = leftOf(ant.location)
- break
- if(ant.location == colony(world)):
- ant.hasFood = False
- break
- # Normal movement pattern
- leftWeight = cellLeftOfAnt(ant).pheromone + 1
- rightWeight = cellRightOfAnt(ant).pheromone + 1
- ant.location = weightedChoice([ (cellLeftOfAnt(ant).index, 0.50) , (cellRightOfAnt(ant).index, 0.50) ])
- return ants
Add Comment
Please, Sign In to add comment