|
@@ -1,8 +1,11 @@
|
1
|
1
|
|
2
|
2
|
import multiprocessing
|
3
|
3
|
|
|
4
|
+from synergine2.config import Config
|
|
5
|
+from synergine2.log import SynergineLogger
|
4
|
6
|
from synergine2.processing import ProcessManager
|
5
|
|
-from synergine2.simulation import Subject, SimulationMechanism, SimulationBehaviour
|
|
7
|
+from synergine2.simulation import SimulationMechanism
|
|
8
|
+from synergine2.simulation import SimulationBehaviour
|
6
|
9
|
from synergine2.simulation import Simulation
|
7
|
10
|
from synergine2.simulation import SubjectBehaviour
|
8
|
11
|
from synergine2.simulation import SubjectMechanism
|
|
@@ -13,6 +16,8 @@ from synergine2.utils import ChunkManager
|
13
|
16
|
class CycleManager(object):
|
14
|
17
|
def __init__(
|
15
|
18
|
self,
|
|
19
|
+ config: Config,
|
|
20
|
+ logger: SynergineLogger,
|
16
|
21
|
simulation: Simulation,
|
17
|
22
|
process_manager: ProcessManager=None,
|
18
|
23
|
):
|
|
@@ -22,6 +27,8 @@ class CycleManager(object):
|
22
|
27
|
chunk_manager=ChunkManager(multiprocessing.cpu_count()),
|
23
|
28
|
)
|
24
|
29
|
|
|
30
|
+ self.config = config
|
|
31
|
+ self.logger = logger
|
25
|
32
|
self.simulation = simulation
|
26
|
33
|
self.process_manager = process_manager
|
27
|
34
|
self.current_cycle = -1
|
|
@@ -34,17 +41,26 @@ class CycleManager(object):
|
34
|
41
|
self.first_cycle = False
|
35
|
42
|
self.current_cycle += 1
|
36
|
43
|
|
|
44
|
+ self.logger.info('Process cycle {}'.format(self.current_cycle))
|
|
45
|
+
|
37
|
46
|
events = []
|
38
|
47
|
|
39
|
48
|
|
40
|
49
|
events.extend(self._get_subjects_events())
|
41
|
50
|
events.extend(self._get_simulation_events())
|
42
|
51
|
|
|
52
|
+ self.logger.info('Cycle {} generate {} events'.format(
|
|
53
|
+ str(self.current_cycle),
|
|
54
|
+ str(len(events)),
|
|
55
|
+ ))
|
43
|
56
|
return events
|
44
|
57
|
|
45
|
58
|
def _get_simulation_events(self) -> [Event]:
|
46
|
59
|
events = []
|
47
|
60
|
results = {}
|
|
61
|
+
|
|
62
|
+ self.logger.info('Process simulation events')
|
|
63
|
+
|
48
|
64
|
results_by_processes = self.process_manager.execute_jobs(
|
49
|
65
|
data=self.simulation,
|
50
|
66
|
job_maker=self.simulation_computing,
|
|
@@ -57,28 +73,64 @@ class CycleManager(object):
|
57
|
73
|
results.get(behaviour_class),
|
58
|
74
|
)
|
59
|
75
|
|
|
76
|
+ self.logger.info('Simulation generate {} behaviours'.format(len(results)))
|
|
77
|
+
|
60
|
78
|
|
61
|
79
|
for behaviour_class, behaviour_data in results.items():
|
62
|
|
- events.extend(self.simulation.behaviours[behaviour_class].action(behaviour_data))
|
|
80
|
+ behaviour_events = self.simulation.behaviours[behaviour_class].action(behaviour_data)
|
|
81
|
+ self.logger.info('{} behaviour generate {} events'.format(
|
|
82
|
+ str(behaviour_class),
|
|
83
|
+ str(len(behaviour_events)),
|
|
84
|
+ ))
|
|
85
|
+
|
|
86
|
+ if self.logger.is_debug:
|
|
87
|
+ self.logger.debug('{} behaviour generated events: {}'.format(
|
|
88
|
+ str(behaviour_class),
|
|
89
|
+ str([e.repr_debug() for e in behaviour_events]),
|
|
90
|
+ ))
|
63
|
91
|
|
|
92
|
+ events.extend(behaviour_events)
|
|
93
|
+
|
|
94
|
+ self.logger.info('Simulation behaviours generate {} events'.format(len(events)))
|
64
|
95
|
return events
|
65
|
96
|
|
66
|
97
|
def _get_subjects_events(self) -> [Event]:
|
67
|
98
|
events = []
|
|
99
|
+ results = {}
|
|
100
|
+
|
|
101
|
+ self.logger.info('Process subjects events')
|
|
102
|
+
|
68
|
103
|
results_by_processes = self.process_manager.chunk_and_execute_jobs(
|
69
|
104
|
data=self.simulation.subjects,
|
70
|
105
|
job_maker=self.subjects_computing,
|
71
|
106
|
)
|
72
|
|
- results = {}
|
|
107
|
+
|
73
|
108
|
for process_results in results_by_processes:
|
74
|
109
|
results.update(process_results)
|
75
|
|
- for subject in self.simulation.subjects[:]:
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+ for subject in self.simulation.subjects[:]:
|
76
|
113
|
for behaviour_class, behaviour_data in results.get(subject.id, {}).items():
|
77
|
114
|
|
78
|
115
|
|
79
|
|
- actions_events = subject.behaviours[behaviour_class].action(behaviour_data)
|
80
|
|
- events.extend(actions_events)
|
|
116
|
+ behaviour_events = subject.behaviours[behaviour_class].action(behaviour_data)
|
|
117
|
+
|
|
118
|
+ self.logger.info('{} behaviour for subject {} generate {} events'.format(
|
|
119
|
+ str(behaviour_class),
|
|
120
|
+ str(subject.id),
|
|
121
|
+ str(len(behaviour_events)),
|
|
122
|
+ ))
|
|
123
|
+
|
|
124
|
+ if self.logger.is_debug:
|
|
125
|
+ self.logger.debug('{} behaviour for subject {} generated events: {}'.format(
|
|
126
|
+ str(behaviour_class),
|
|
127
|
+ str(subject.id),
|
|
128
|
+ str([e.repr_debug() for e in behaviour_events]),
|
|
129
|
+ ))
|
81
|
130
|
|
|
131
|
+ events.extend(behaviour_events)
|
|
132
|
+
|
|
133
|
+ self.logger.info('Subjects behaviours generate {} events'.format(len(events)))
|
82
|
134
|
return events
|
83
|
135
|
|
84
|
136
|
def simulation_computing(
|
|
@@ -87,19 +139,49 @@ class CycleManager(object):
|
87
|
139
|
process_number,
|
88
|
140
|
process_count,
|
89
|
141
|
):
|
|
142
|
+ self.logger.info('Simulation computing')
|
|
143
|
+
|
90
|
144
|
|
91
|
145
|
mechanisms = self.get_mechanisms_to_compute(simulation)
|
92
|
146
|
mechanisms_data = {}
|
93
|
147
|
behaviours_data = {}
|
94
|
148
|
|
|
149
|
+ self.logger.info('{} mechanisms to compute'.format(str(len(mechanisms))))
|
|
150
|
+ if self.logger.is_debug:
|
|
151
|
+ self.logger.debug('Mechanisms are: {}'.format(
|
|
152
|
+ str([m.repr_debug() for m in mechanisms])
|
|
153
|
+ ))
|
|
154
|
+
|
95
|
155
|
for mechanism in mechanisms:
|
96
|
|
- mechanisms_data[type(mechanism)] = mechanism.run(
|
|
156
|
+ mechanism_data = mechanism.run(
|
97
|
157
|
process_number=process_number,
|
98
|
158
|
process_count=process_count,
|
99
|
159
|
)
|
100
|
160
|
|
101
|
|
- for behaviour in self.get_behaviours_to_compute(simulation):
|
|
161
|
+ if self.logger.is_debug:
|
|
162
|
+ self.logger.debug('{} mechanism product data: {}'.format(
|
|
163
|
+ type(mechanism).__name__,
|
|
164
|
+ str(mechanism_data),
|
|
165
|
+ ))
|
|
166
|
+
|
|
167
|
+ mechanisms_data[type(mechanism)] = mechanism_data
|
|
168
|
+
|
|
169
|
+ behaviours = self.get_behaviours_to_compute(simulation)
|
|
170
|
+ self.logger.info('{} behaviours to compute'.format(str(len(behaviours))))
|
|
171
|
+
|
|
172
|
+ if self.logger.is_debug:
|
|
173
|
+ self.logger.debug('Behaviours are: {}'.format(
|
|
174
|
+ str([b.repr_debug() for b in behaviours])
|
|
175
|
+ ))
|
|
176
|
+
|
|
177
|
+ for behaviour in behaviours:
|
102
|
178
|
behaviour_data = behaviour.run(mechanisms_data)
|
|
179
|
+ if self.logger.is_debug:
|
|
180
|
+ self.logger.debug('{} behaviour produce data: {}'.format(
|
|
181
|
+ type(behaviour).__name__,
|
|
182
|
+ behaviour_data,
|
|
183
|
+ ))
|
|
184
|
+
|
103
|
185
|
if behaviour_data:
|
104
|
186
|
behaviours_data[type(behaviour)] = behaviour_data
|
105
|
187
|
|
|
@@ -112,17 +194,67 @@ class CycleManager(object):
|
112
|
194
|
process_count=None,
|
113
|
195
|
):
|
114
|
196
|
results = {}
|
|
197
|
+ self.logger.info('Subjects computing: {} subjects to compute'.format(str(len(subjects))))
|
|
198
|
+
|
115
|
199
|
for subject in subjects:
|
116
|
200
|
mechanisms = self.get_mechanisms_to_compute(subject)
|
|
201
|
+ if not mechanisms:
|
|
202
|
+ break
|
|
203
|
+
|
|
204
|
+ self.logger.info('Subject {}: {} mechanisms'.format(
|
|
205
|
+ str(subject.id),
|
|
206
|
+ str(len(mechanisms)),
|
|
207
|
+ ))
|
|
208
|
+
|
|
209
|
+ if self.logger.is_debug:
|
|
210
|
+ self.logger.info('Subject {}: mechanisms are: {}'.format(
|
|
211
|
+ str(subject.id),
|
|
212
|
+ str([m.repr_debug for m in mechanisms])
|
|
213
|
+ ))
|
|
214
|
+
|
117
|
215
|
mechanisms_data = {}
|
118
|
216
|
behaviours_data = {}
|
119
|
217
|
|
120
|
218
|
for mechanism in mechanisms:
|
121
|
|
- mechanisms_data[type(mechanism)] = mechanism.run()
|
|
219
|
+ mechanism_data = mechanism.run()
|
|
220
|
+ if self.logger.is_debug:
|
|
221
|
+ self.logger.info('Subject {}: {} mechanisms produce data: {}'.format(
|
|
222
|
+ str(subject.id),
|
|
223
|
+ type(mechanism).__name__,
|
|
224
|
+ str(mechanism_data),
|
|
225
|
+ ))
|
|
226
|
+
|
|
227
|
+ mechanisms_data[type(mechanism)] = mechanism_data
|
|
228
|
+
|
|
229
|
+ if self.logger.is_debug:
|
|
230
|
+ self.logger.info('Subject {}: mechanisms data are: {}'.format(
|
|
231
|
+ str(subject.id),
|
|
232
|
+ str(mechanisms_data),
|
|
233
|
+ ))
|
|
234
|
+
|
|
235
|
+ subject_behaviours = self.get_behaviours_to_compute(subject)
|
|
236
|
+
|
|
237
|
+ self.logger.info('Subject {}: have {} behaviours'.format(
|
|
238
|
+ str(subject.id),
|
|
239
|
+ str(len(subject_behaviours)),
|
|
240
|
+ ))
|
|
241
|
+
|
|
242
|
+ for behaviour in subject_behaviours:
|
|
243
|
+ self.logger.info('Subject {}: run {} behaviour'.format(
|
|
244
|
+ str(subject.id),
|
|
245
|
+ str(type(behaviour)),
|
|
246
|
+ ))
|
122
|
247
|
|
123
|
|
- for behaviour in self.get_behaviours_to_compute(subject):
|
124
|
248
|
|
125
|
249
|
behaviour_data = behaviour.run(mechanisms_data)
|
|
250
|
+
|
|
251
|
+ if self.logger.is_debug:
|
|
252
|
+ self.logger.debug('Subject {}: behaviour {} produce data: {}'.format(
|
|
253
|
+ str(type(behaviour)),
|
|
254
|
+ str(subject.id),
|
|
255
|
+ str(behaviour_data),
|
|
256
|
+ ))
|
|
257
|
+
|
126
|
258
|
if behaviour_data:
|
127
|
259
|
behaviours_data[type(behaviour)] = behaviour_data
|
128
|
260
|
|
|
@@ -157,6 +289,10 @@ class CycleManager(object):
|
157
|
289
|
simulation_actions = simulation_actions or []
|
158
|
290
|
subject_actions = subject_actions or []
|
159
|
291
|
events = []
|
|
292
|
+ self.logger.info('Apply {} simulation_actions and {} subject_actions'.format(
|
|
293
|
+ len(simulation_actions),
|
|
294
|
+ len(subject_actions),
|
|
295
|
+ ))
|
160
|
296
|
|
161
|
297
|
for subject_id, behaviours_and_data in subject_actions:
|
162
|
298
|
subject = self.simulation.subjects.index.get(subject_id)
|
|
@@ -165,12 +301,46 @@ class CycleManager(object):
|
165
|
301
|
simulation=self.simulation,
|
166
|
302
|
subject=subject,
|
167
|
303
|
)
|
168
|
|
- events.extend(behaviour.action(behaviour_data))
|
|
304
|
+ self.logger.info('Apply {} behaviour on subject {}'.format(
|
|
305
|
+ str(behaviour_class),
|
|
306
|
+ str(subject_id),
|
|
307
|
+ ))
|
|
308
|
+ if self.logger.is_debug:
|
|
309
|
+ self.logger.debug('{} behaviour data is {}'.format(
|
|
310
|
+ str(behaviour_class),
|
|
311
|
+ str(behaviour_data),
|
|
312
|
+ ))
|
|
313
|
+ behaviour_events = behaviour.action(behaviour_data)
|
|
314
|
+ self.logger.info('{} events from behaviour {} from subject {}'.format(
|
|
315
|
+ len(behaviour_events),
|
|
316
|
+ str(behaviour_class),
|
|
317
|
+ str(subject_id),
|
|
318
|
+ ))
|
|
319
|
+ if self.logger.is_debug:
|
|
320
|
+ self.logger.debug('Events from behaviour {} from subject {} are: {}'.format(
|
|
321
|
+ str(behaviour_class),
|
|
322
|
+ str(subject_id),
|
|
323
|
+ str([e.repr_debug() for e in behaviour_events])
|
|
324
|
+ ))
|
|
325
|
+ events.extend(behaviour_events)
|
169
|
326
|
|
170
|
327
|
for behaviour_class, behaviour_data in simulation_actions:
|
171
|
328
|
behaviour = behaviour_class(
|
172
|
329
|
simulation=self.simulation,
|
173
|
330
|
)
|
174
|
|
- events.extend(behaviour.action(behaviour_data))
|
175
|
331
|
|
|
332
|
+ self.logger.info('Apply {} simulation behaviour'.format(
|
|
333
|
+ str(behaviour_class),
|
|
334
|
+ ))
|
|
335
|
+
|
|
336
|
+ behaviour_events = behaviour.action(behaviour_data)
|
|
337
|
+ if self.logger.is_debug:
|
|
338
|
+ self.logger.debug('Events from behaviour {} are: {}'.format(
|
|
339
|
+ str(behaviour_class),
|
|
340
|
+ str([e.repr_debug() for e in behaviour_events])
|
|
341
|
+ ))
|
|
342
|
+
|
|
343
|
+ events.extend(behaviour_events)
|
|
344
|
+
|
|
345
|
+ self.logger.info('{} events generated'.format(len(events)))
|
176
|
346
|
return events
|