Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. def runSim(timesteps, pathlen, numants, decay, phval):
  2. # Helper functions (abstract, abstract, abstract!)
  3. def foodSourceA(world):
  4. return world[0]
  5. def foodSourceB(world):
  6. return world[pathlen * 2 + 2]
  7. def colony(world):
  8. return world[pathlen + 1]
  9. def firstIndex(world):
  10. return 0
  11. def middleMindex(world):
  12. return pathlen + 1
  13. def lastIndex(world):
  14. return pathlen * 2 + 2
  15. def cellOfAnt(ant):
  16. return world[ant.location]
  17. def cellLeftOfAnt(ant):
  18. return world[leftOf(ant.location)]
  19. def cellRightOfAnt(ant):
  20. return world[rightOf(ant.location)]
  21. def leftOf(index):
  22. return index - 1 if index != firstIndex(world) else index
  23. def rightOf(index):
  24. return index + 1 if index != lastIndex(world) else index
  25. # Initialize the world
  26. world = [Cell(i) for i in range(pathlen * 2 + 3)]
  27. ants = [Ant(pathlen + 1) for i in range(numants)]
  28. for t in range(timesteps):
  29. for cell in world:
  30. cell.pheromone *= 1 - decay
  31. # Iterate through ants
  32. for ant in ants:
  33. # Deposit pheromone
  34. cellOfAnt(ant).pheromone += phval
  35. # Take into account special conditions for movement
  36. if(ant.location == foodSourceA(world)):
  37. ant.hasFood = True
  38. ant.location = rightOf(ant.location)
  39. break
  40. if(ant.location == foodSourceB(world)):
  41. ant.hasFood = True
  42. ant.location = leftOf(ant.location)
  43. break
  44. if(ant.location == colony(world)):
  45. ant.hasFood = False
  46. break
  47. # Normal movement pattern
  48. leftWeight = cellLeftOfAnt(ant).pheromone + 1
  49. rightWeight = cellRightOfAnt(ant).pheromone + 1
  50. ant.location = weightedChoice([ (cellLeftOfAnt(ant).index, 0.50) , (cellRightOfAnt(ant).index, 0.50) ])
  51. return ants
Add Comment
Please, Sign In to add comment