Browse Source

Test switch to exploration when ant is on colony point

Bastien Sevajol 9 years ago
parent
commit
6f0894a767

+ 5 - 2
intelligine/synergy/Colony.py View File

@@ -8,8 +8,11 @@ from intelligine.synergy.event.CycleAction import CycleAction
8 8
 
9 9
 class Colony(SynergyCollection):
10 10
 
11
-
12 11
     def __init__(self, configuration):
13 12
         super().__init__(configuration)
14 13
         self._actions = [MoveAction, NearAttackableAction, TakeableAction, PutableAction,
15
-                         CycleAction]
14
+                         CycleAction]
15
+        self._start_position = configuration.get_start_position()
16
+
17
+    def get_start_position(self):
18
+        return self._start_position

+ 4 - 0
intelligine/synergy/ColonyConfiguration.py View File

@@ -10,6 +10,10 @@ class ColonyConfiguration(Configuration):
10 10
     _ant_class = Ant
11 11
     _ant_count = 50
12 12
 
13
+    @classmethod
14
+    def get_start_position(cls):
15
+        return cls._start_position
16
+
13 17
     def get_start_objects(self, collection, context):
14 18
       context.metas.value.set(POSITION, collection.get_id(), self._start_position)
15 19
 

+ 1 - 1
intelligine/synergy/event/move/MoveAction.py View File

@@ -120,7 +120,7 @@ class MoveAction(Action):
120 120
             self._appose_pheromone(obj, context)
121 121
 
122 122
             # TEST: le temps de tout tester
123
-            if self._move_to_point == (0, 5, 5) and obj.is_carrying():
123
+            if self._move_to_point == obj.get_colony().get_start_position() and obj.is_carrying():
124 124
                 obj_transported = obj.get_carried()
125 125
                 obj_transported.set_carried_by(None)
126 126
                 obj.put_carry(obj_transported, (-1, 0, 0))

+ 4 - 1
intelligine/synergy/object/ant/Ant.py View File

@@ -76,4 +76,7 @@ class Ant(Bug):
76 76
                                       self.get_position(),
77 77
                                       self.get_movement_pheromone_gland().get_movement_molecules())
78 78
         except BestPheromoneHere as best_pheromone_here:
79
-            pass
79
+            pass
80
+
81
+    def get_colony(self):
82
+        return self.get_collection()

+ 5 - 0
intelligine/tests/simulation/mode/Base.py View File

@@ -0,0 +1,5 @@
1
+from synergine.test.TestSimulation import TestSimulation as BaseTestSimulation
2
+
3
+
4
+class Base(BaseTestSimulation):
5
+    pass

+ 128 - 0
intelligine/tests/simulation/mode/TestChangeMode.py View File

@@ -0,0 +1,128 @@
1
+from os import getcwd
2
+from sys import path as ppath
3
+from intelligine.core.exceptions import NoPheromone
4
+
5
+ppath.insert(1,getcwd()+'/modules')
6
+
7
+
8
+from intelligine.synergy.object.Food import Food
9
+from intelligine.tests.simulation.mode.Base import Base
10
+from intelligine.synergy.Colony import Colony
11
+from intelligine.synergy.Simulation import Simulation
12
+from intelligine.synergy.ColonyConfiguration import ColonyConfiguration
13
+from intelligine.synergy.event.move.MoveAction import MoveAction
14
+from intelligine.synergy.event.move.direction import NORTH, SOUTH
15
+from intelligine.tests.src.event.MoveAction import MoveAction as TestMoveAction
16
+from synergine.synergy.collection.SynergyCollection import SynergyCollection
17
+from synergine.synergy.collection.Configuration import Configuration
18
+from intelligine.core.Context import Context
19
+from intelligine.cst import MOVE_MODE_EXPLO, MOVE_MODE_GOHOME, MOVE_MODE
20
+from intelligine.cst import PHEROMON_DIR_HOME, PHEROMON_DIR_EXPLO
21
+
22
+
23
+class TestChangeMode(Base):
24
+
25
+    def __init__(self, methodName='runTest'):
26
+        super().__init__(methodName)
27
+        self.ant = None
28
+        self.food = None
29
+        self._force_move = self._force_move
30
+
31
+    @staticmethod
32
+    def _force_move(self_move_action, context, object_point):
33
+        object_movement_mode = context.metas.value.get(MOVE_MODE, self_move_action._object_id)
34
+        if object_movement_mode == MOVE_MODE_GOHOME:
35
+            return SOUTH
36
+        return NORTH
37
+
38
+    def _get_set_up_simulation(self):
39
+        return Simulation([self._get_colony(), self._get_foods()])
40
+
41
+    def _get_colony(self):
42
+        test_case = self
43
+        class TestColony(Colony):
44
+            def __init__(self, configuration):
45
+                super().__init__(configuration)
46
+                self._actions.remove(MoveAction)
47
+                TestMoveAction.force_direction = test_case._force_move
48
+                self._actions.append(TestMoveAction)
49
+        return TestColony(self._get_colony_configuration())
50
+
51
+    def _get_colony_configuration(self):
52
+        test_case = self
53
+        class TestColonyConfiguration(ColonyConfiguration):
54
+            _start_position = (0, 0, 0)
55
+            _ant_count = 1
56
+            def get_start_objects(self, collection, context):
57
+                ants = super().get_start_objects(collection, context)
58
+                test_case.ant = ants[0]
59
+                return ants
60
+        return TestColonyConfiguration()
61
+
62
+    def _get_foods(self):
63
+        class Foods(SynergyCollection):
64
+            pass
65
+        return Foods(self._get_food_configuration())
66
+
67
+    def _get_food_configuration(self):
68
+        test_case = self
69
+        class FoodConfiguration(Configuration):
70
+            def get_start_objects(self, collection, context):
71
+                food = Food(collection, context)
72
+                food.set_position((0, 0, -3))
73
+                test_case.food = food
74
+                return [food]
75
+        return FoodConfiguration()
76
+
77
+    def _get_core_configuration(self, cycles, main_process=True):
78
+        config = super()._get_core_configuration(cycles, main_process)
79
+        config.update({
80
+            'app': {
81
+                'classes': {
82
+                    'Context': Context
83
+                }
84
+            }
85
+        })
86
+        return config
87
+
88
+    def test_from_exploration_to_go_home(self):
89
+        self._run_and_get_core(0)
90
+        self.assertEquals((0, 0, 0), self.ant.get_position())
91
+        self.assertEquals(MOVE_MODE_EXPLO, self.ant.get_brain().get_movement_mode())
92
+
93
+        self._run_and_get_core(1)
94
+        self.assertEquals((0, 0, -1), self.ant.get_position())
95
+        self.assertEquals(MOVE_MODE_EXPLO, self.ant.get_brain().get_movement_mode())
96
+
97
+        self._run_and_get_core(2)
98
+        self.assertEquals((0, 0, -2), self.ant.get_position())
99
+        self.assertEquals((PHEROMON_DIR_HOME, 2),
100
+                          self.ant.get_movement_pheromone_gland().get_movement_molecules())
101
+
102
+        # Ant has take Food piece
103
+        self._run_and_get_core(3)
104
+        self.assertEquals((0, 0, -3), self.ant.get_position())
105
+
106
+        self.assertTrue(self.ant.is_carrying())
107
+        self.assertIsNotNone(self.ant.get_carried())
108
+        self.assertEquals(self.food.__class__, self.ant.get_carried().__class__)
109
+        self.assertEquals(MOVE_MODE_GOHOME, self.ant.get_brain().get_movement_mode())
110
+        # Now it appose exploration pheromone
111
+        self.assertEquals((PHEROMON_DIR_EXPLO, 0),
112
+                          self.ant.get_movement_pheromone_gland().get_movement_molecules())
113
+
114
+        self._run_and_get_core(4)
115
+        self.assertEquals((0, 0, -2), self.ant.get_position())
116
+
117
+        self._run_and_get_core(5)
118
+        self.assertEquals((0, 0, -1), self.ant.get_position())
119
+
120
+        self._run_and_get_core(6)
121
+        self.assertEquals((0, 0, 0), self.ant.get_position())
122
+
123
+        # Ant has put his food piece
124
+        self._run_and_get_core(7)
125
+        self.assertEquals((0, 0, -1), self.ant.get_position())
126
+
127
+        self.assertFalse(self.ant.is_carrying())
128
+        self.assertEquals(MOVE_MODE_EXPLO, self.ant.get_brain().get_movement_mode())

+ 1 - 2
intelligine/tests/simulation/pheromone/TestDirection.py View File

@@ -2,8 +2,7 @@ from os import getcwd
2 2
 from sys import path as ppath
3 3
 from intelligine.core.exceptions import NoPheromone
4 4
 
5
-ppath.insert(1,getcwd()+'/modules') # TODO: win32 compatibilite (python path)
6
-# TODO: load et launch des tests auto (avec bootstrap contenant ci dessus)
5
+ppath.insert(1,getcwd()+'/modules')
7 6
 
8 7
 from intelligine.tests.simulation.pheromone.Base import Base
9 8
 from intelligine.simulation.pheromone.DirectionPheromone import DirectionPheromone

+ 13 - 0
intelligine/tests/src/event/MoveAction.py View File

@@ -0,0 +1,13 @@
1
+from intelligine.synergy.event.move.MoveAction import MoveAction as BaseMoveAction
2
+from intelligine.synergy.event.move.direction import NORTH
3
+
4
+
5
+class MoveAction(BaseMoveAction):
6
+
7
+    force_direction = lambda self, context, object_point: NORTH
8
+
9
+    def _get_direction_with_pheromones(self, context, object_point):
10
+        return self.force_direction(context, object_point)
11
+
12
+    # def _get_random_direction(self, context):
13
+    #     return self.force_direction