advanced_scouter.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import random
  2. import numpy as np
  3. from simulation.logic.sensing_scouter import SensingScouter
  4. class AdvancedScouter(SensingScouter):
  5. """
  6. Knowledge used:
  7. - ["Scouting"]["Global Explore Probability"] : (float, between 0 and 1) Set the ratio between exploring
  8. globally and exploring locally
  9. - ["Scouting"]["Search Locally on Food"] : when stepping on food, automatically search locally
  10. """
  11. def __init__(self, board, knowledge, x, y, use_diagonal=False, sightline=3, light_compute=True):
  12. SensingScouter.__init__(self, board, knowledge, x, y, use_diagonal, sightline, light_compute)
  13. self.state = 0
  14. def choose_goal(self):
  15. if self.state == 0:
  16. if not (self.board.has_food(self.x, self.y) and self.knowledge["Scouting"]["Search Locally on Food"]) \
  17. and self.knowledge["Scouting"]["Global Explore Probability"] < random.random():
  18. self.state = 1
  19. return self.choose_local_goal()
  20. else:
  21. if self.knowledge["Scouting"]["Global Explore Probability"] >= random.random():
  22. self.state = 0
  23. return self.choose_global_goal()
  24. def choose_local_goal(self):
  25. return SensingScouter.choose_goal(self)
  26. def choose_global_goal(self):
  27. x0, y0 = max(0, self.x - self.sight_see), max(0, self.y - self.sight_see)
  28. x1, y1 = min(self.board.width, self.x + self.sight_see + 1), min(self.board.height, self.y + self.sight_see + 1)
  29. scores = np.zeros((x1 - x0, y1 - y0), dtype=float)
  30. for x in range(x1 - x0):
  31. for y in range(y1 - y0):
  32. local_x0, local_y0 = max(x0, x0 + x - self.sight_see), max(y0, y0 + y - self.sight_see)
  33. local_x1, local_y1 = min(x1, x0 + x + self.sight_see + 1), min(y1, y0 + y + self.sight_see + 1)
  34. scores[x, y] = np.sum(self.board.dropped_blob[local_x0:local_x1, local_y0:local_y1])
  35. total_area = (y1-y0) * (x1-x0)
  36. scores[x, y] = scores[x, y] / total_area
  37. min_indices = np.where(scores == np.min(scores))
  38. if len(min_indices[0]) == 0:
  39. return None
  40. else:
  41. i = np.random.randint(len(min_indices[0]))
  42. return min_indices[0][i] + x0, min_indices[1][i] + y0
  43. def move(self):
  44. if self.board.has_food(self.x, self.y) and self.knowledge["Scouting"]["Search Locally on Food"] \
  45. and self.state == 1:
  46. self.goal = None
  47. self.state = 0 # Food found, search locally
  48. SensingScouter.move(self)