DirectionPheromone.py 4.8KB

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