DirectionPheromone.py 4.6KB

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