DirectionPheromone.py 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from intelligine.cst import PHEROMON_DIRECTION, PHEROMON_POSITIONS
  2. from intelligine.core.exceptions import NoPheromone
  3. from random import shuffle
  4. from xyzworld.geometry import get_degree_from_north
  5. from intelligine.synergy.event.move.direction import get_direction_for_degrees
  6. class DirectionPheromone():
  7. @staticmethod
  8. def appose(context, point, pheromone):
  9. context.pheromones().increment_with_pheromone(point, pheromone)
  10. context.metas.list.add(PHEROMON_POSITIONS, PHEROMON_POSITIONS, point, assert_not_in=False)
  11. @staticmethod
  12. def get_direction_for_point(context, point, pheromone_type):
  13. flavour = context.pheromones().get_flavour(point)
  14. pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
  15. distance = pheromone.get_distance()
  16. around_points = context.get_around_points_of(point)
  17. # TODO: Cet algo around a mettre ailleurs
  18. around_pheromones_points = []
  19. for around_point in around_points:
  20. flavour = context.pheromones().get_flavour(around_point)
  21. try:
  22. around_pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
  23. if around_pheromone.get_distance() < distance:
  24. around_pheromones_points.append((around_point, around_pheromone))
  25. except NoPheromone:
  26. pass # No pheromone, ok continue to sniff around
  27. if not around_pheromones_points:
  28. raise NoPheromone()
  29. shuffle(around_pheromones_points)
  30. around_pheromones_sorted = sorted(around_pheromones_points, key=lambda x: x[1].get_intensity(), reverse=True)
  31. max_intensity = around_pheromones_sorted[0][1].get_intensity()
  32. around_pheromones_max = []
  33. for around_pheromone_sorted in around_pheromones_sorted:
  34. if around_pheromone_sorted[1].get_intensity() == max_intensity:
  35. around_pheromones_max.append(around_pheromone_sorted)
  36. around_pheromones_sorted_by_distance = sorted(around_pheromones_max,
  37. key=lambda x: x[1].get_distance(),
  38. reverse=False)
  39. go_to_point = around_pheromones_sorted_by_distance[0][0]
  40. direction_degrees = get_degree_from_north(point, go_to_point)
  41. direction = get_direction_for_degrees(direction_degrees)
  42. return direction
  43. # # 1: On melange
  44. # items = pheromone_info.items()
  45. # shuffle(items)
  46. # ph = OrderedDict(items)
  47. # foo = True
  48. # # 2: On trie par puissance
  49. # ph_sorted = sorted(ph.items(), key=lambda x: x[1])
  50. # # 3: On recupere les direction de la puissance max
  51. # max_intensity = ph_sorted[0][1][1]
  52. # max_directions = [direction_name for direction_name in pheromone_info
  53. # if pheromone_info[direction_name][1] == max_intensity]
  54. # # 4: On trie par age
  55. # # 5: On recupere les directions de l'age le plus court
  56. # # 6: On choisis une direction au hasard parmis elles (ou par rapport a direction precedente ??bug autre fois??)
  57. @staticmethod
  58. def get_best_pheromone_direction_in(context, reference_point, points, pheromone_type):
  59. around_pheromones_points = []
  60. for around_point in points:
  61. flavour = context.pheromones().get_flavour(around_point)
  62. try:
  63. around_pheromone = flavour.get_pheromone(category=PHEROMON_DIRECTION, type=pheromone_type)
  64. around_pheromones_points.append((around_point, around_pheromone))
  65. except NoPheromone:
  66. pass # Ok, no pheromone, continue to sniff around
  67. if not around_pheromones_points:
  68. raise NoPheromone()
  69. shuffle(around_pheromones_points)
  70. around_pheromones_sorted = sorted(around_pheromones_points, key=lambda x: x[1].get_intensity(), reverse=True)
  71. go_to_point = around_pheromones_sorted[0][0]
  72. direction_degrees = get_degree_from_north(reference_point, go_to_point)
  73. direction = get_direction_for_degrees(direction_degrees)
  74. return direction