TestDirection.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. from intelligine.core.exceptions import NoMolecule
  2. from intelligine.simulation.molecule.Molecule import Molecule
  3. from intelligine.simulation.molecule.MoleculeFlavour import MoleculeFlavour
  4. from intelligine.tests.simulation.molecule.Base import Base
  5. from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
  6. from intelligine.core.Context import Context
  7. from intelligine.cst import MOLECULES_DIRECTION, PHEROMON_DIR_EXPLO, PHEROMON_DIR_NONE
  8. from intelligine.synergy.event.move.direction import NORTH, NORTH_EST, EST, SOUTH_EST, SOUTH, SOUTH_WEST, WEST, \
  9. NORTH_WEST, CENTER
  10. from intelligine.synergy.event.move.direction import get_position_with_direction_decal as _p
  11. class TestDirection(Base):
  12. def __init__(self, *args, **kwargs):
  13. super().__init__(*args, **kwargs)
  14. self._context = Context()
  15. def setUp(self):
  16. self._context = Context()
  17. def _set_up_molecules(self, molecules, re_init=True):
  18. if re_init:
  19. self._context = Context()
  20. for position in molecules:
  21. self._context.molecules().set_flavour(position, MoleculeFlavour.new_from_raw_data(molecules[position]))
  22. def _test_direction_for_point(self, molecules, direction, molecule_type=PHEROMON_DIR_EXPLO,
  23. reference_point=_p(CENTER), re_init=True):
  24. """
  25. :param molecules:
  26. :param direction:
  27. :param molecule_type:
  28. :param reference_point:
  29. :return:
  30. """
  31. self._set_up_molecules(molecules, re_init=re_init)
  32. direction_tested = DirectionMolecule.get_direction_for_point(self._context, reference_point, molecule_type)
  33. self.assertEqual(direction, direction_tested, "Direction must be %s" % direction)
  34. def _test_direction_for_points(self, molecules, direction, molecule_type=PHEROMON_DIR_EXPLO,
  35. reference_point=_p(CENTER), re_init=True):
  36. """
  37. :param molecules:
  38. :param direction:
  39. :param molecule_type:
  40. :param reference_point:
  41. :return:
  42. """
  43. self._set_up_molecules(molecules, re_init=re_init)
  44. around_points = self._context.get_around_points_of_point(reference_point)
  45. direction_tested = DirectionMolecule.get_best_molecule_direction_in(self._context,
  46. reference_point,
  47. around_points,
  48. molecule_type)
  49. self.assertEqual(direction, direction_tested, "Direction must be %s" % direction)
  50. def test_route_direct_route(self):
  51. """
  52. Test easy direction with 1 best molecules just near actual position
  53. :return:
  54. """
  55. test_data = {
  56. NORTH_WEST: {
  57. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  58. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
  59. },
  60. NORTH: {
  61. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  62. _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
  63. },
  64. NORTH_EST: {
  65. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  66. _p(NORTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
  67. },
  68. WEST: {
  69. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  70. _p(WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
  71. },
  72. EST: {
  73. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  74. _p(EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
  75. },
  76. SOUTH_WEST: {
  77. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  78. _p(SOUTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
  79. },
  80. SOUTH: {
  81. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  82. _p(SOUTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
  83. },
  84. SOUTH_EST: {
  85. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  86. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
  87. }
  88. }
  89. for direction_wanted in test_data:
  90. self._test_direction_for_point(test_data[direction_wanted], direction_wanted)
  91. def test_route_with_multiple_same_intensity(self):
  92. """
  93. Test find route in middle of multiple molecules
  94. :return:
  95. """
  96. test_data = {
  97. NORTH_WEST: {
  98. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  99. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}},
  100. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1, 0)}}
  101. },
  102. NORTH_WEST: {
  103. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  104. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}},
  105. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1, 0)}},
  106. _p(SOUTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1, 0)}}
  107. },
  108. NORTH_WEST: {
  109. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
  110. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}},
  111. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1, 0)}},
  112. _p(SOUTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (8, 1, 0)}}
  113. },
  114. }
  115. for direction_wanted in test_data:
  116. self._test_direction_for_point(test_data[direction_wanted], direction_wanted)
  117. def test_route_with_multiple_different_intensity(self):
  118. """
  119. Test find route in middle of multiple molecules
  120. :return:
  121. """
  122. test_data = {
  123. NORTH_WEST: {
  124. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 2, 0)}},
  125. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2, 0)}},
  126. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (8, 1, 0)}}
  127. },
  128. NORTH_WEST: {
  129. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 2, 0)}},
  130. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2, 0)}},
  131. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (8, 1, 0)}},
  132. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (5, 10, 0)}} # an other molecule type
  133. }
  134. }
  135. for direction_wanted in test_data:
  136. self._test_direction_for_point(test_data[direction_wanted], direction_wanted)
  137. def test_direction_direct(self):
  138. test_data = {
  139. NORTH: {
  140. _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2, 0)}}
  141. },
  142. NORTH: {
  143. _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2, 0)}},
  144. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (9, 500, 0)}} # An other molecule type
  145. }
  146. }
  147. for direction in test_data:
  148. self._test_direction_for_points(test_data[direction], direction)
  149. def test_direction_with_multiple_intensity(self):
  150. test_data = {
  151. NORTH: {
  152. _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 5, 0)}},
  153. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4, 0)}},
  154. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4, 0)}}
  155. },
  156. NORTH: {
  157. _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 5, 0)}},
  158. _p(WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (9, 500, 0)}}, # An other molecule_type
  159. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4, 0)}},
  160. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4, 0)}}
  161. }
  162. }
  163. for direction in test_data:
  164. self._test_direction_for_points(test_data[direction], direction)
  165. def test_no_molecules_around(self):
  166. # No molecule
  167. with self.assertRaises(NoMolecule):
  168. self._test_direction_for_points({}, -1)
  169. # Wrong molecule type
  170. with self.assertRaises(NoMolecule):
  171. self._test_direction_for_points({
  172. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (9, 5, 0)}}
  173. }, -1)
  174. def test_appose(self):
  175. self._test_point_raise_no_molecule()
  176. self._test_points_raise_no_molecule()
  177. # Une molecule au centre
  178. DirectionMolecule.appose(self._context,
  179. _p(CENTER),
  180. self._get_molecule(PHEROMON_DIR_EXPLO, 2))
  181. # Ne permet pas de trouver une route
  182. self._test_point_raise_no_molecule(re_init=False)
  183. self._test_points_raise_no_molecule(re_init=False)
  184. # Une molecule au nord
  185. DirectionMolecule.appose(self._context,
  186. _p(NORTH),
  187. self._get_molecule(PHEROMON_DIR_EXPLO, 1))
  188. # le permet
  189. self._test_direction_for_points({}, NORTH, re_init=False)
  190. self._test_direction_for_point({}, NORTH, re_init=False)
  191. def _test_point_raise_no_molecule(self, molecules={}, direction=-1, molecule_type=PHEROMON_DIR_EXPLO,
  192. reference_point=_p(CENTER), re_init=True):
  193. with self.assertRaises(NoMolecule):
  194. self._test_direction_for_point(molecules, direction, re_init=re_init)
  195. def _test_points_raise_no_molecule(self, molecules={}, direction=-1, molecule_type=PHEROMON_DIR_EXPLO,
  196. reference_point=_p(CENTER), re_init=True):
  197. with self.assertRaises(NoMolecule):
  198. self._test_direction_for_points(molecules, direction, re_init=re_init)
  199. def _get_molecule(self, type, distance):
  200. return Molecule(MOLECULES_DIRECTION, type, distance=distance)