fsm_ant.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from simulation.board import Board
  2. from simulation.logic.dumb_scouter import DumbScouter
  3. from simulation.logic.gatherer import Gatherer
  4. from simulation.logic.sensing_scouter import SensingScouter
  5. from simulation.logic.advanced_scouter import AdvancedScouter
  6. class FSMAnt(DumbScouter):
  7. """
  8. Knowledge used:
  9. - ["Harvesting"]["Min"]: (float) Min value an ant has to store to stop being starved
  10. - ["Harvesting"]["Max"]: (float) Max value an ant can carry
  11. - ["Harvesting"]["Eat"]: (float) Value an ant eat to do a step
  12. - ["Harvesting"]["Collect"]: (float) Value an ant collect by stepping on food
  13. - ["Gathering"]/["Scouting"]["Diagonal Moves"]: (bool) Allow ants to use diagonals to travel
  14. """
  15. # Multiplication factor for drop value (see blob variable) when an ant is starving
  16. RATIO_DROP_STARVE = 2
  17. def __init__(self, board, knowledge, x, y):
  18. """
  19. :type board: Board
  20. :type knowledge: dict
  21. :type x: int
  22. :type y: int
  23. """
  24. DumbScouter.__init__(self, board, knowledge, x, y)
  25. self.gatherer_logic = Gatherer(board, knowledge, x, y, self.knowledge["Gathering"]["Diagonal Moves"],
  26. self.knowledge["Gathering"]["Sightline"],
  27. self.knowledge["Gathering"]["Light Compute"])
  28. self.scouting_logic = AdvancedScouter(board, knowledge, x, y, self.knowledge["Scouting"]["Diagonal Moves"],
  29. self.knowledge["Scouting"]["Sightline"],
  30. self.knowledge["Scouting"]["Light Compute"])
  31. self.stored = self.knowledge["Harvesting"]["Min"]
  32. self.starving = False
  33. def move(self):
  34. if self.starving:
  35. self.gatherer_logic.move()
  36. self.x = self.gatherer_logic.x
  37. self.y = self.gatherer_logic.y
  38. else:
  39. self.scouting_logic.move()
  40. self.x = self.scouting_logic.x
  41. self.y = self.scouting_logic.y
  42. def init_gathering(self):
  43. self.gatherer_logic.reset()
  44. self.gatherer_logic.x = self.x
  45. self.gatherer_logic.y = self.y
  46. def init_scouting(self):
  47. self.scouting_logic.reset()
  48. self.scouting_logic.x = self.x
  49. self.scouting_logic.y = self.y
  50. def update(self):
  51. eat_ratio = self.knowledge["Harvesting"]["Eat"] * (Board.MAX_BLOB - self.board.get_blob(self.x, self.y)) \
  52. / Board.MAX_BLOB
  53. self.drop = self.knowledge["Scouters"]["Drop by eat"] * eat_ratio
  54. DumbScouter.update(self)
  55. if not self.starving:
  56. self.stored -= eat_ratio
  57. self.stored = max(0, self.stored)
  58. if self.board.has_food(self.x, self.y):
  59. if len(self.knowledge['food']) == 1:
  60. wanted = min(self.knowledge["Harvesting"]["Min"], self.knowledge["Harvesting"]["Max"] - self.stored)
  61. else:
  62. wanted = min(self.knowledge["Harvesting"]["Collect"], self.knowledge["Harvesting"]["Max"] - self.stored)
  63. received, finished = self.board.eat_food(self.x, self.y, wanted)
  64. self.stored += received
  65. if finished:
  66. self.knowledge['food'].remove((self.x, self.y))
  67. if self.stored == 0 and not self.starving:
  68. self.starving = True
  69. self.init_gathering()
  70. if self.stored >= self.knowledge["Harvesting"]["Min"] and self.starving:
  71. self.starving = False
  72. self.init_scouting()