Browse Source

Implement behaviour selector

Bastien Sevajol 8 years ago
parent
commit
0c596c4d8a
4 changed files with 40 additions and 3 deletions
  1. 19 1
      sandbox/engulf/behaviour.py
  2. 2 1
      sandbox/engulf/subject.py
  3. 6 1
      synergine2/cycle.py
  4. 13 0
      synergine2/simulation.py

+ 19 - 1
sandbox/engulf/behaviour.py View File

1
 # coding: utf-8
1
 # coding: utf-8
2
+import typing
2
 from random import choice
3
 from random import choice
3
 
4
 
4
 from sandbox.engulf.const import COLLECTION_GRASS
5
 from sandbox.engulf.const import COLLECTION_GRASS
5
-from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour
6
+from synergine2.simulation import SubjectBehaviour, SimulationMechanism, SimulationBehaviour, SubjectBehaviourSelector
6
 from synergine2.simulation import Event
7
 from synergine2.simulation import Event
7
 from synergine2.utils import ChunkManager
8
 from synergine2.utils import ChunkManager
8
 from synergine2.xyz import ProximitySubjectMechanism, DIRECTIONS, DIRECTION_SLIGHTLY
9
 from synergine2.xyz import ProximitySubjectMechanism, DIRECTIONS, DIRECTION_SLIGHTLY
179
         if not self.subject.previous_direction:
180
         if not self.subject.previous_direction:
180
             return choice(DIRECTIONS)
181
             return choice(DIRECTIONS)
181
         return choice(DIRECTION_SLIGHTLY[self.subject.previous_direction])
182
         return choice(DIRECTION_SLIGHTLY[self.subject.previous_direction])
183
+
184
+
185
+class CellBehaviourSelector(SubjectBehaviourSelector):
186
+    # If behaviour in sublist, only one be kept in sublist
187
+    behaviour_hierarchy = (  # TODO: refact it
188
+        (
189
+            Eat,
190
+            SearchFood,
191
+            Explore,
192
+        ),
193
+    )
194
+
195
+    def reduce_behaviours(
196
+        self,
197
+        behaviours: typing.Dict[typing.Type[SubjectBehaviour], dict],
198
+    ) -> typing.Dict[typing.Type[SubjectBehaviour], dict]:
199
+        return behaviours  # TODO: code it

+ 2 - 1
sandbox/engulf/subject.py View File

1
 # coding: utf-8
1
 # coding: utf-8
2
-from sandbox.engulf.behaviour import GrowUp, SearchFood, Eat, Explore
2
+from sandbox.engulf.behaviour import GrowUp, SearchFood, Eat, Explore, CellBehaviourSelector
3
 from sandbox.engulf.const import COLLECTION_CELL, COLLECTION_ALIVE, COLLECTION_EATABLE, COLLECTION_GRASS
3
 from sandbox.engulf.const import COLLECTION_CELL, COLLECTION_ALIVE, COLLECTION_EATABLE, COLLECTION_GRASS
4
 from synergine2.simulation import Subject
4
 from synergine2.simulation import Subject
5
 from synergine2.xyz import XYZSubjectMixin
5
 from synergine2.xyz import XYZSubjectMixin
17
         Eat,
17
         Eat,
18
         Explore,
18
         Explore,
19
     ]
19
     ]
20
+    behaviour_selector_class = CellBehaviourSelector
20
 
21
 
21
 
22
 
22
 class Grass(XYZSubjectMixin, Subject):
23
 class Grass(XYZSubjectMixin, Subject):

+ 6 - 1
synergine2/cycle.py View File

110
 
110
 
111
         # Duplicate list to prevent conflicts with behaviours subjects manipulations
111
         # Duplicate list to prevent conflicts with behaviours subjects manipulations
112
         for subject in self.simulation.subjects[:]:
112
         for subject in self.simulation.subjects[:]:
113
-            for behaviour_class, behaviour_data in results.get(subject.id, {}).items():
113
+            subject_behaviours = results.get(subject.id, {})
114
+            if subject.behaviour_selector:
115
+                # TODO: Looging
116
+                subject_behaviours = subject.behaviour_selector.reduce_behaviours(dict(subject_behaviours))
117
+
118
+            for behaviour_class, behaviour_data in subject_behaviours.items():
114
                 # TODO: Ajouter une etape de selection des actions a faire (genre neuronnal)
119
                 # TODO: Ajouter une etape de selection des actions a faire (genre neuronnal)
115
                 # (genre se cacher et fuir son pas compatibles)
120
                 # (genre se cacher et fuir son pas compatibles)
116
                 behaviour_events = subject.behaviours[behaviour_class].action(behaviour_data)
121
                 behaviour_events = subject.behaviours[behaviour_class].action(behaviour_data)

+ 13 - 0
synergine2/simulation.py View File

1
 # coding: utf-8
1
 # coding: utf-8
2
 import collections
2
 import collections
3
+import typing
3
 
4
 
4
 from synergine2.base import BaseObject
5
 from synergine2.base import BaseObject
5
 from synergine2.utils import get_mechanisms_classes
6
 from synergine2.utils import get_mechanisms_classes
8
 class Subject(object):
9
 class Subject(object):
9
     collections = []
10
     collections = []
10
     behaviours_classes = []
11
     behaviours_classes = []
12
+    behaviour_selector_class = None  # type: typing.Type[SubjectBehaviourSelector]
11
 
13
 
12
     def __init__(self, simulation: 'Simulation'):
14
     def __init__(self, simulation: 'Simulation'):
13
         self.id = id(self)  # We store object id because it's lost between process
15
         self.id = id(self)  # We store object id because it's lost between process
14
         self.simulation = simulation
16
         self.simulation = simulation
15
         self.behaviours = {}
17
         self.behaviours = {}
16
         self.mechanisms = {}
18
         self.mechanisms = {}
19
+        self.behaviour_selector = None  # type: SubjectBehaviourSelector
20
+        if self.behaviour_selector_class:
21
+            self.behaviour_selector = self.behaviour_selector_class()
17
 
22
 
18
         for collection in self.collections:
23
         for collection in self.collections:
19
             self.simulation.collections[collection].append(self)
24
             self.simulation.collections[collection].append(self)
212
         Return events will be give to terminals
217
         Return events will be give to terminals
213
         """
218
         """
214
         raise NotImplementedError()
219
         raise NotImplementedError()
220
+
221
+
222
+class SubjectBehaviourSelector(BaseObject):
223
+    def reduce_behaviours(
224
+        self,
225
+        behaviours: typing.Dict[typing.Type[SubjectBehaviour], dict],
226
+    ) -> typing.Dict[typing.Type[SubjectBehaviour], dict]:
227
+        raise NotImplementedError()