TestDirection.py 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. from os import getcwd
  2. from sys import path as ppath
  3. from intelligine.core.exceptions import NoPheromone
  4. ppath.insert(1,getcwd()+'/modules') # TODO: win32 compatibilite (python path)
  5. # TODO: load et launch des tests auto (avec bootstrap contenant ci dessus)
  6. from intelligine.tests.simulation.pheromone.Base import Base
  7. from intelligine.simulation.pheromone.DirectionPheromone import DirectionPheromone
  8. from intelligine.core.Context import Context
  9. from intelligine.cst import PHEROMON_DIRECTION, PHEROMON_DIR_EXPLO, PHEROMON_DIR_HOME
  10. class TestDirection(Base):
  11. def __init__(self, *args, **kwargs):
  12. super().__init__(*args, **kwargs)
  13. self._context = Context()
  14. def setUp(self):
  15. self._context = Context()
  16. def _set_up_pheromones(self, pheromones, re_init=True):
  17. if re_init:
  18. self._context = Context()
  19. for position in pheromones:
  20. self._context.pheromones().set_pheromones(position, pheromones[position])
  21. def _test_direction_for_point(self, pheromones, direction, pheromone_type=PHEROMON_DIR_EXPLO,
  22. reference_point=(0, 0, 0)):
  23. """
  24. :param pheromones:
  25. :param direction:
  26. :param pheromone_type:
  27. :param reference_point:
  28. :return:
  29. """
  30. self._set_up_pheromones(pheromones)
  31. direction_tested = DirectionPheromone.get_direction_for_point(self._context, reference_point, pheromone_type)
  32. self.assertEqual(direction, direction_tested, "Direction must be %s" % direction)
  33. def _test_direction_for_points(self, pheromones, direction, pheromone_type=PHEROMON_DIR_EXPLO,
  34. reference_point=(0, 0, 0)):
  35. """
  36. :param pheromones:
  37. :param direction:
  38. :param pheromone_type:
  39. :param reference_point:
  40. :return:
  41. """
  42. self._set_up_pheromones(pheromones)
  43. around_points = self._context.get_arround_points_of(reference_point)
  44. direction_tested = DirectionPheromone.get_best_pheromone_direction_in(self._context,
  45. reference_point,
  46. around_points,
  47. pheromone_type)
  48. self.assertEqual(direction, direction_tested, "Direction must be %s" % direction)
  49. def test_route_direct_route(self):
  50. """
  51. Test easy direction with 1 best pheromones just near actual position
  52. :return:
  53. """
  54. test_data = {
  55. 10: {
  56. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  57. (0, -1, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  58. },
  59. 11: {
  60. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  61. (0, 0, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  62. },
  63. 12: {
  64. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  65. (0, 1, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  66. },
  67. 13: {
  68. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  69. (0, -1, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  70. },
  71. 15: {
  72. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  73. (0, 1, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  74. },
  75. 16: {
  76. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  77. (0, -1, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  78. },
  79. 17: {
  80. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  81. (0, 0, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  82. },
  83. 18: {
  84. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  85. (0, 1, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}}
  86. }
  87. }
  88. for direction_wanted in test_data:
  89. self._test_direction_for_point(test_data[direction_wanted], direction_wanted)
  90. def test_route_with_multiple_same_intensity(self):
  91. """
  92. Test find route in middle of multiple pheromones
  93. :return:
  94. """
  95. test_data = {
  96. 10: {
  97. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  98. (0, -1, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}},
  99. (0, 1, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1)}}
  100. },
  101. 10: {
  102. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1)}},
  103. (0, -1, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1)}},
  104. (0, 1, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1)}},
  105. (0, 0, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1)}}
  106. },
  107. }
  108. for direction_wanted in test_data:
  109. self._test_direction_for_point(test_data[direction_wanted], direction_wanted)
  110. def test_route_with_multiple_different_intensity(self):
  111. """
  112. Test find route in middle of multiple pheromones
  113. :return:
  114. """
  115. test_data = {
  116. 10: {
  117. (0, 0, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 2)}},
  118. (0, -1, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2)}},
  119. (0, 1, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (8, 1)}}
  120. }
  121. }
  122. for direction_wanted in test_data:
  123. self._test_direction_for_point(test_data[direction_wanted], direction_wanted)
  124. def test_direction_direct(self):
  125. test_data = {
  126. 11: {
  127. (0, 0, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2)}}
  128. },
  129. 11: {
  130. (0, 0, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2)}},
  131. (0, -1, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_HOME: (9, 500)}} # An other pheromone type
  132. }
  133. }
  134. for direction in test_data:
  135. self._test_direction_for_points(test_data[direction], direction)
  136. def test_direction_with_multiple_intensity(self):
  137. test_data = {
  138. 11: {
  139. (0, 0, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 5)}},
  140. (0, 1, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4)}},
  141. (0, -1, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4)}}
  142. },
  143. 11: {
  144. (0, 0, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 5)}},
  145. (0, -1, 0): {PHEROMON_DIRECTION: {PHEROMON_DIR_HOME: (9, 500)}}, # An other pheromone_type
  146. (0, 1, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4)}},
  147. (0, -1, -1): {PHEROMON_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4)}}
  148. }
  149. }
  150. for direction in test_data:
  151. self._test_direction_for_points(test_data[direction], direction)
  152. def test_no_pheromones_around(self):
  153. # No pheromone
  154. try: # WTF ?
  155. self.assertRaises(NoPheromone, self._test_direction_for_points({}, -1))
  156. except NoPheromone:
  157. self.assertTrue(True)
  158. # Wrong pheromone type
  159. try: # WTF ?
  160. self.assertRaises(NoPheromone, self._test_direction_for_points({
  161. (0, 1, 1): {PHEROMON_DIRECTION: {PHEROMON_DIR_HOME: (9, 5)}}
  162. }, -1))
  163. except NoPheromone:
  164. self.assertTrue(True)