Browse Source

improve brain

Bastien Sevajol 9 years ago
parent
commit
631cc3aabd

+ 1 - 0
intelligine/cst.py View File

58
 COL_EATABLE = IncrementedNamedInt.get('intelligine.col.eatable')
58
 COL_EATABLE = IncrementedNamedInt.get('intelligine.col.eatable')
59
 COL_SMELL = IncrementedNamedInt.get('intelligine.col.smell')
59
 COL_SMELL = IncrementedNamedInt.get('intelligine.col.smell')
60
 
60
 
61
+BRAIN = IncrementedNamedInt.get('intelligine.brain')
61
 BRAIN_SCHEMA = IncrementedNamedInt.get('intelligine.brain_schema')
62
 BRAIN_SCHEMA = IncrementedNamedInt.get('intelligine.brain_schema')
62
 BRAIN_PART_MOVE = IncrementedNamedInt.get('intelligine.brain.part.move')
63
 BRAIN_PART_MOVE = IncrementedNamedInt.get('intelligine.brain.part.move')
63
 BRAIN_PART_TAKE = IncrementedNamedInt.get('intelligine.brain.part.take')
64
 BRAIN_PART_TAKE = IncrementedNamedInt.get('intelligine.brain.part.take')

+ 11 - 0
intelligine/simulation/molecule/MoleculeFlavour.py View File

36
             raise NoTypeInMolecule()
36
             raise NoTypeInMolecule()
37
         return types[type]
37
         return types[type]
38
 
38
 
39
+    def get_molecules(self, category):
40
+        """
41
+
42
+        :param category:
43
+        :return: Molecules dict or empty dict of no molecules
44
+        """
45
+        try:
46
+            return self.get_types(category)
47
+        except NoCategoryInMolecule:
48
+            return {}
49
+
39
     def get_types(self, category):
50
     def get_types(self, category):
40
         if category not in self._flavour:
51
         if category not in self._flavour:
41
             raise NoCategoryInMolecule()
52
             raise NoCategoryInMolecule()

+ 8 - 0
intelligine/simulation/object/brain/AntBrain.py View File

90
             if isinstance(obj, take_class):
90
             if isinstance(obj, take_class):
91
                 return self._taken_smell_matches[take_class]
91
                 return self._taken_smell_matches[take_class]
92
         raise NotFound()
92
         raise NotFound()
93
+
94
+    @classmethod
95
+    def get_home_smells(cls):
96
+        """
97
+        Note: Actually return all know smells. Not really HOME smells.
98
+        :return:
99
+        """
100
+        return cls._taken_smell_matches.values()

+ 11 - 8
intelligine/simulation/object/brain/part/move/AntMoveBrainPart.py View File

1
+from intelligine.shorcut.brain import get_brain_class
1
 from intelligine.simulation.object.brain.part.move.AntStar.ByPass import ByPass
2
 from intelligine.simulation.object.brain.part.move.AntStar.ByPass import ByPass
2
 from intelligine.simulation.object.brain.part.move.AntStar.Host import Host
3
 from intelligine.simulation.object.brain.part.move.AntStar.Host import Host
3
 from intelligine.simulation.object.brain.part.move.MoveBrainPart import MoveBrainPart
4
 from intelligine.simulation.object.brain.part.move.MoveBrainPart import MoveBrainPart
5
 from synergine_xyz.cst import POSITION
6
 from synergine_xyz.cst import POSITION
6
 from intelligine.core.exceptions import NoMolecule
7
 from intelligine.core.exceptions import NoMolecule
7
 from intelligine.cst import MOLECULE_SEARCHING, MOVE_MODE_EXPLO, MOVE_MODE_HOME, MOVE_MODE, MOVE_MODE_GOHOME, \
8
 from intelligine.cst import MOLECULE_SEARCHING, MOVE_MODE_EXPLO, MOVE_MODE_HOME, MOVE_MODE, MOVE_MODE_GOHOME, \
8
-    EXPLORATION_VECTOR, MOLECULES_DIRECTION, SMELL_FOOD, SMELL_EGG
9
+    EXPLORATION_VECTOR, MOLECULES_DIRECTION, SMELL_FOOD, SMELL_EGG, BRAIN_SCHEMA, BRAIN
9
 from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
10
 from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
10
 
11
 
11
 
12
 
123
     def _on_home_smell(cls, context, object_id):
124
     def _on_home_smell(cls, context, object_id):
124
         current_position = context.metas.value.get(POSITION, object_id)
125
         current_position = context.metas.value.get(POSITION, object_id)
125
         flavour = context.molecules().get_flavour(current_position)
126
         flavour = context.molecules().get_flavour(current_position)
126
-        # TODO: Idem, liste de smell_type ...
127
-        for smell_type in (SMELL_FOOD, SMELL_EGG):
128
-            try:
129
-                molecule = flavour.get_molecule(category=MOLECULES_DIRECTION, type=smell_type)
127
+        molecules = flavour.get_molecules(MOLECULES_DIRECTION)
128
+
129
+        if not molecules:
130
+            return False
131
+
132
+        brain_class = get_brain_class(context, object_id)
133
+        for smell_type in brain_class.get_home_smells():
134
+            if smell_type in molecules:
130
                 return True
135
                 return True
131
-            except NoMolecule:
132
-                pass  # C'est qu'elle y est pas ^^
136
+
133
         return False
137
         return False
134
 
138
 
135
     def _update_exploration_vector(self):
139
     def _update_exploration_vector(self):
136
-        # TODO: add tuple as vectors ?
137
         just_move_vector = directions_modifiers[self._host_brain.get_host().get_previous_direction()]
140
         just_move_vector = directions_modifiers[self._host_brain.get_host().get_previous_direction()]
138
         self._set_exploration_vector((self._exploration_vector[0] + just_move_vector[1],
141
         self._set_exploration_vector((self._exploration_vector[0] + just_move_vector[1],
139
                                       self._exploration_vector[1] + just_move_vector[2]))
142
                                       self._exploration_vector[1] + just_move_vector[2]))

+ 4 - 5
intelligine/synergy/object/BaseBug.py View File

1
 from intelligine.core.exceptions import BodyPartAlreadyExist
1
 from intelligine.core.exceptions import BodyPartAlreadyExist
2
 from intelligine.synergy.object.Transportable import Transportable
2
 from intelligine.synergy.object.Transportable import Transportable
3
-from intelligine.cst import COL_ALIVE, COLONY, ACTION_DIE
3
+from intelligine.cst import COL_ALIVE, COLONY, ACTION_DIE, BRAIN
4
 from intelligine.simulation.object.brain.Brain import Brain
4
 from intelligine.simulation.object.brain.Brain import Brain
5
 from intelligine.cst import ALIVE, ATTACKABLE
5
 from intelligine.cst import ALIVE, ATTACKABLE
6
 from synergine.core.Signals import Signals
6
 from synergine.core.Signals import Signals
9
 class BaseBug(Transportable):
9
 class BaseBug(Transportable):
10
 
10
 
11
     _body_parts = {}
11
     _body_parts = {}
12
+    _brain_class = Brain
12
 
13
 
13
     def __init__(self, collection, context):
14
     def __init__(self, collection, context):
14
         super().__init__(collection, context)
15
         super().__init__(collection, context)
18
         self._life_points = 10
19
         self._life_points = 10
19
         self._alive = True
20
         self._alive = True
20
         self._movements_count = -1
21
         self._movements_count = -1
21
-        self._brain = self._get_brain_instance()
22
+        self._brain = self._brain_class(self._context, self)
23
+        self._context.metas.value.set(BRAIN, self.get_id(), self._brain_class)
22
         self._parts = {}
24
         self._parts = {}
23
         self._init_parts()
25
         self._init_parts()
24
 
26
 
62
     def get_movements_count(self):
64
     def get_movements_count(self):
63
         return self._movements_count
65
         return self._movements_count
64
 
66
 
65
-    def _get_brain_instance(self):
66
-        return Brain(self._context, self)
67
-
68
     def get_brain(self):
67
     def get_brain(self):
69
         return self._brain
68
         return self._brain

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

13
     _body_parts = {
13
     _body_parts = {
14
         BODY_PART_PHEROMONE_GLAND: MovementMoleculeGland
14
         BODY_PART_PHEROMONE_GLAND: MovementMoleculeGland
15
     }
15
     }
16
+    _brain_class = AntBrain
16
 
17
 
17
     def __init__(self, collection, context):
18
     def __init__(self, collection, context):
18
         super().__init__(collection, context)
19
         super().__init__(collection, context)
33
         self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
34
         self._remove_col(COL_TRANSPORTER_CARRYING, allow_not_in=True)
34
         self._remove_col(COL_FIGHTER)
35
         self._remove_col(COL_FIGHTER)
35
 
36
 
36
-    def _get_brain_instance(self):
37
-        return AntBrain(self._context, self)
38
-
39
     def get_movement_molecule_gland(self):
37
     def get_movement_molecule_gland(self):
40
         return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
38
         return self.get_body_part(BODY_PART_PHEROMONE_GLAND)
41
 
39