Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. import random
  2. import math
  3. from pheromone import Pheromone
  4.  
  5. """
  6. Editors notes: I now need to create and deal with x and y positions for both ants and anthills.
  7.  
  8.  
  9. """
  10.  
  11. class Ant:
  12. def __init__(self, home):
  13. """Initialises the ant object"""
  14. self.max_ph_level = 18 # Every how many steps can we place a pheremone drop
  15. self.ph_time = 30 # How long do we keep direction after finding pheromones
  16. self.carryingFood = False # Indicate whether we have any food with us
  17. self.pheromoneLevel = self.max_ph_level # How much pheromone do we have right now
  18. self.foundLastPheromone = 0 # How well do we remember the last pheromone (larger number: more recent)
  19. self.max_speed = 3 # The maximum movement speed of the ant
  20. self.currentXspeed = 0
  21. self.currentYspeed = 0
  22. self.home = home # Sets the anthill that created the ant as home
  23.  
  24.  
  25. def live(self):
  26. """Does all the actions that an ant does"""
  27. if self.carryingFood:
  28. self.walkTowardsHome()
  29. self.handlePheromoneDrop()
  30. self.checkHome()
  31. else:
  32. self.searchForFood()
  33.  
  34. def randomChance(self, percent):
  35. """Returns true in exactly 'percent' number of calls (i.e randomChance(25) has a 25% chance of returning true"""
  36. return random.randint(100) < percent
  37.  
  38. def walkTowardsHome(self):
  39. """Tries to walk home. Added a little bit of randomness"""
  40. if not self.home: # If we don't have a home, we can't do anything
  41. return
  42. if self.randomChance(2): # Has a 2% chance of being true
  43. self.randomWalk() # Walk slightly off course
  44. else:
  45. self.headInGeneralDirection(self.home) # Turn in the general direction of home
  46. self.walk() # Walk in the current direction with current speed
  47.  
  48. def randomWalk(self):
  49. """Walk around randomly (random direction and speed)"""
  50. if self.randomChance(50):
  51. self.currentXspeed = self.adjustSpeed(self.currentXspeed)
  52. self.currentYspeed = self.adjustSpeed(self.currentYspeed)
  53.  
  54. def adjustSpeed(self, speed):
  55. """Adjusts the speed randomly (start moving, continue or slow down"""
  56. speed = speed + random.randint(2 * self.max_speed - 1) - self.max_speed + 1
  57. return self.capSpeed(speed)
  58.  
  59. def capSpeed(self, speed):
  60. """Makes sure the speed returned is in the range (-self.max_speed - self.max_speed"""
  61. if speed < -self.max_speed: # Returns the max backwards speed if speed is smaller than it
  62. return -self.max_speed
  63. elif speed > self.max_speed: # Returns the max speed if speed is larger than it
  64. return self.max_speed
  65. else: # Otherwise, the speed is within the range, so its fine.
  66. return speed
  67.  
  68. def headInGeneralDirection(self, target):
  69. """Adjusts the direction to heat somewhat towards the given coordinates."""
  70. distanceX = abs(self.x - target.x) # Finds the x distance between the ant and the target
  71. distanceY = abs(self.y - target.y) # Finds the y distance between the ant and the target
  72.  
  73. # The following code in the function is yet to be understood - further testing may be necessary
  74.  
  75. if distanceX > 0 and random.randint(distanceX + distanceY) < distanceX:
  76. moveX = True
  77. else:
  78. moveX = False
  79.  
  80. if distanceY > 0 and random.randint(distanceX + distanceY) < distanceY:
  81. moveY = True
  82. else:
  83. moveY = False
  84.  
  85. self.currentXspeed = self.computeHomeDelta(moveX, self.x, target.x)
  86. self.currentYspeed = self.computeHomeDelta(moveY, self.y, target.y)
  87.  
  88. def computeHomeDelta(self, move, ant, home):
  89. """Computes and returns the direction (delta) that the ant should steer in on its way home"""
  90. if move:
  91. if ant > home:
  92. return -self.max_speed
  93. else:
  94. return self.max_speed
  95. else:
  96. return 0
  97.  
  98. def walk(self):
  99. """Walk forward in the current direction with the current speed (doesn't change either)"""
  100. self.x += self.currentXspeed
  101. self.y += self.currentYspeed
  102. # The next line is not understood whatsoever and needs further testing. I will attempt to create a self.rotation
  103. # and use it to replicate the Java version.
  104. self.rotation = 180 * math.atan2(self.currentYspeed, self.currentXspeed) / math.pi
  105.  
  106. def checkHome(self):
  107. """Are we at home? Drop food if we are, and then leave to get some more"""
  108. if self.atHome():
  109. self.dropFood()
  110.  
  111. def atHome(self):
  112. if self.home != False:
  113. return abs(self.x - self.home.x) < 4 and abs(self.y - self.home.y) < 4
  114. else:
  115. return False
  116.  
  117. def dropFood(self):
  118. """Drops the food into the anthill"""
  119. self.carryingFood = False
  120. self.home.countFood()
  121. ### NEEDS TO UPDATE ITS IMAGE SOMEOHOW
  122.  
  123. def searchForFood(self):
  124. """Walk around in search of food"""
  125. if self.foundLastPheromone > 0:
  126. self.foundLastPheromone -= 1
  127. self.walkAwayFromHome()
  128. elif self.smellPheromone():
  129. self.walkTowardsPheromone()
  130. else:
  131. self.randomWalk()
  132. self.checkFood()
  133.  
  134. def walkAwayFromHome(self):
  135. """Try to walk away from home (with slightly random walking added)"""
  136. if not self.home:
  137. return
  138. elif self.randomChance(2):
  139. self.randomWalk()
  140. else:
  141. self.headInGeneralDirection(self.home)
  142. self.currentXspeed = -self.currentXspeed
  143. self.currentYspeed = -self.currentYspeed
  144. self.walk()
  145.  
  146. def smellPheromone(self):
  147. print("How will I 'smell' pheromones?")
  148.  
  149. def handlePheromoneDrop(self):
  150. """Checks whether we can drop some pheromone yet. If we can, do it"""
  151. if self.pheromoneLevel == self.max_ph_level:
  152. ph = Pheromone(self.x, self.y)
  153. # The above command should create a pheromone
  154. self.pheromoneLevel = 0
  155. else:
  156. self.pheromoneLevel += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement