DirectionPheromone.py 4.4KB

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