DirectionPheromone.py 4.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, movement_molecules):
  9. pheromone_type, distance_from = movement_molecules
  10. # TODO: Ajouter l'age de la pheromone !
  11. context.pheromones().increment(point, [PHEROMON_DIRECTION, pheromone_type], distance=distance_from)
  12. context.metas.list.add(PHEROMON_POSITIONS, PHEROMON_POSITIONS, point, assert_not_in=False)
  13. @staticmethod
  14. def get_direction_for_point(context, point, pheromone_type):
  15. try:
  16. pheromone_info = context.pheromones().get_info(point, [PHEROMON_DIRECTION, pheromone_type])
  17. except KeyError:
  18. raise NoPheromone()
  19. # DEBUG: On se rettrouve avec un {} ...
  20. if not pheromone_info:
  21. raise NoPheromone()
  22. point_intensity = pheromone_info[1]
  23. point_distance = pheromone_info[0]
  24. arround_points = context.get_arround_points_of(point)
  25. arround_pheromones_points = []
  26. for arround_point in arround_points:
  27. arround_pheromone_info = context.pheromones().get_info(arround_point,
  28. [PHEROMON_DIRECTION, pheromone_type],
  29. allow_empty=True,
  30. empty_value={})
  31. if arround_pheromone_info and arround_pheromone_info[0] < point_distance:
  32. arround_pheromones_points.append((arround_point, arround_pheromone_info))
  33. if not arround_pheromones_points:
  34. raise NoPheromone()
  35. shuffle(arround_pheromones_points)
  36. arround_pheromones_sorted = sorted(arround_pheromones_points, key=lambda x: x[1][1], reverse=True)
  37. max_intensity = arround_pheromones_sorted[0][1][1]
  38. arround_pheromones_max = []
  39. for arround_pheromone_sorted in arround_pheromones_sorted:
  40. if arround_pheromone_sorted[1][1] == max_intensity:
  41. arround_pheromones_max.append(arround_pheromone_sorted)
  42. arround_pheromones_sorted_by_distance = sorted(arround_pheromones_max, key=lambda x: x[1][0], reverse=False)
  43. go_to_point = arround_pheromones_sorted_by_distance[0][0]
  44. direction_degrees = get_degree_from_north(point, go_to_point)
  45. direction = get_direction_for_degrees(direction_degrees)
  46. return direction
  47. # # 1: On melange
  48. # items = pheromone_info.items()
  49. # shuffle(items)
  50. # ph = OrderedDict(items)
  51. # foo = True
  52. # # 2: On trie par puissance
  53. # ph_sorted = sorted(ph.items(), key=lambda x: x[1])
  54. # # 3: On recupere les direction de la puissance max
  55. # max_intensity = ph_sorted[0][1][1]
  56. # max_directions = [direction_name for direction_name in pheromone_info
  57. # if pheromone_info[direction_name][1] == max_intensity]
  58. # # 4: On trie par age
  59. # # 5: On recupere les directions de l'age le plus court
  60. # # 6: On choisis une direction au hasard parmis elles (ou par rapport a direction precedente ??bug autre fois??)
  61. @staticmethod
  62. def get_best_pheromone_direction_in(context, reference_point, points, pheromone_type):
  63. arround_pheromones_points = []
  64. for arround_point in points:
  65. arround_pheromone_info = context.pheromones().get_info(arround_point,
  66. [PHEROMON_DIRECTION, pheromone_type],
  67. allow_empty=True,
  68. empty_value={})
  69. if arround_pheromone_info:
  70. arround_pheromones_points.append((arround_point, arround_pheromone_info))
  71. if not arround_pheromones_points:
  72. raise NoPheromone()
  73. shuffle(arround_pheromones_points)
  74. arround_pheromones_sorted = sorted(arround_pheromones_points, key=lambda x: x[1][1], reverse=True)
  75. go_to_point = arround_pheromones_sorted[0][0]
  76. direction_degrees = get_degree_from_north(reference_point, go_to_point)
  77. direction = get_direction_for_degrees(direction_degrees)
  78. return direction