TestDirection.py 10KB

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