Browse Source

Implement behaviour selector

Bastien Sevajol 7 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,8 +1,9 @@
1 1
 # coding: utf-8
2
+import typing
2 3
 from random import choice
3 4
 
4 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 7
 from synergine2.simulation import Event
7 8
 from synergine2.utils import ChunkManager
8 9
 from synergine2.xyz import ProximitySubjectMechanism, DIRECTIONS, DIRECTION_SLIGHTLY
@@ -179,3 +180,20 @@ class Explore(SubjectBehaviour):
179 180
         if not self.subject.previous_direction:
180 181
             return choice(DIRECTIONS)
181 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,5 +1,5 @@
1 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 3
 from sandbox.engulf.const import COLLECTION_CELL, COLLECTION_ALIVE, COLLECTION_EATABLE, COLLECTION_GRASS
4 4
 from synergine2.simulation import Subject
5 5
 from synergine2.xyz import XYZSubjectMixin
@@ -17,6 +17,7 @@ class Cell(XYZSubjectMixin, Subject):
17 17
         Eat,
18 18
         Explore,
19 19
     ]
20
+    behaviour_selector_class = CellBehaviourSelector
20 21
 
21 22
 
22 23
 class Grass(XYZSubjectMixin, Subject):

+ 6 - 1
synergine2/cycle.py View File

@@ -110,7 +110,12 @@ class CycleManager(object):
110 110
 
111 111
         # Duplicate list to prevent conflicts with behaviours subjects manipulations
112 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 119
                 # TODO: Ajouter une etape de selection des actions a faire (genre neuronnal)
115 120
                 # (genre se cacher et fuir son pas compatibles)
116 121
                 behaviour_events = subject.behaviours[behaviour_class].action(behaviour_data)

+ 13 - 0
synergine2/simulation.py View File

@@ -1,5 +1,6 @@
1 1
 # coding: utf-8
2 2
 import collections
3
+import typing
3 4
 
4 5
 from synergine2.base import BaseObject
5 6
 from synergine2.utils import get_mechanisms_classes
@@ -8,12 +9,16 @@ from synergine2.utils import get_mechanisms_classes
8 9
 class Subject(object):
9 10
     collections = []
10 11
     behaviours_classes = []
12
+    behaviour_selector_class = None  # type: typing.Type[SubjectBehaviourSelector]
11 13
 
12 14
     def __init__(self, simulation: 'Simulation'):
13 15
         self.id = id(self)  # We store object id because it's lost between process
14 16
         self.simulation = simulation
15 17
         self.behaviours = {}
16 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 23
         for collection in self.collections:
19 24
             self.simulation.collections[collection].append(self)
@@ -212,3 +217,11 @@ class SimulationBehaviour(BaseObject):
212 217
         Return events will be give to terminals
213 218
         """
214 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()