TestDirection.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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)}},
  58. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  59. },
  60. NORTH: {
  61. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  62. _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  63. },
  64. NORTH_EST: {
  65. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  66. _p(NORTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  67. },
  68. WEST: {
  69. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  70. _p(WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  71. },
  72. EST: {
  73. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  74. _p(EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  75. },
  76. SOUTH_WEST: {
  77. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  78. _p(SOUTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  79. },
  80. SOUTH: {
  81. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  82. _p(SOUTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  83. },
  84. SOUTH_EST: {
  85. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  86. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  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)}},
  99. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}},
  100. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1)}}
  101. },
  102. NORTH_WEST: {
  103. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  104. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}},
  105. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1)}},
  106. _p(SOUTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1)}}
  107. },
  108. NORTH_WEST: {
  109. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  110. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}},
  111. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1)}},
  112. _p(SOUTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (8, 1)}}
  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)}},
  125. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2)}},
  126. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (8, 1)}}
  127. },
  128. NORTH_WEST: {
  129. _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 2)}},
  130. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2)}},
  131. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (8, 1)}},
  132. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (5, 10)}} # 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)}}
  141. },
  142. NORTH: {
  143. _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2)}},
  144. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (9, 500)}} # 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)}},
  153. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4)}},
  154. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4)}}
  155. },
  156. NORTH: {
  157. _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 5)}},
  158. _p(WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (9, 500)}}, # An other molecule_type
  159. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4)}},
  160. _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4)}}
  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. try: # WTF ?
  168. self.assertRaises(NoMolecule, self._test_direction_for_points({}, -1))
  169. except NoMolecule:
  170. self.assertTrue(True)
  171. # Wrong molecule type
  172. try: # WTF ?
  173. self.assertRaises(NoMolecule, self._test_direction_for_points({
  174. _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (9, 5)}}
  175. }, -1))
  176. except NoMolecule:
  177. self.assertTrue(True)
  178. def test_appose(self):
  179. self._test_point_raise_no_molecule()
  180. self._test_points_raise_no_molecule()
  181. # Une molecule au centre
  182. DirectionMolecule.appose(self._context,
  183. _p(CENTER),
  184. self._get_molecule(PHEROMON_DIR_EXPLO, 2))
  185. # Ne permet pas de trouver une route
  186. self._test_point_raise_no_molecule(re_init=False)
  187. self._test_points_raise_no_molecule(re_init=False)
  188. # Une molecule au nord
  189. DirectionMolecule.appose(self._context,
  190. _p(NORTH),
  191. self._get_molecule(PHEROMON_DIR_EXPLO, 1))
  192. # le permet
  193. self._test_direction_for_points({}, NORTH, re_init=False)
  194. self._test_direction_for_point({}, NORTH, re_init=False)
  195. def _test_point_raise_no_molecule(self, molecules={}, direction=-1, molecule_type=PHEROMON_DIR_EXPLO,
  196. reference_point=_p(CENTER), re_init=True):
  197. try: # WTF ?
  198. self._test_direction_for_point(molecules, direction, re_init=re_init)
  199. except NoMolecule:
  200. self.assertTrue(True)
  201. def _test_points_raise_no_molecule(self, molecules={}, direction=-1, molecule_type=PHEROMON_DIR_EXPLO,
  202. reference_point=_p(CENTER), re_init=True):
  203. try: # WTF ?
  204. self._test_direction_for_points(molecules, direction, re_init=re_init)
  205. except NoMolecule:
  206. self.assertTrue(True)
  207. def _get_molecule(self, type, distance):
  208. return Molecule(MOLECULES_DIRECTION, type, distance=distance)