-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenvironment.py
More file actions
951 lines (710 loc) · 36.5 KB
/
environment.py
File metadata and controls
951 lines (710 loc) · 36.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
from __future__ import annotations
from logging import Logger
from pprint import pprint
import population
import copy
import util
from random_inst import FixedRandom
DEBUG_OPERATION_OUTPUT = False
#access streetways between blocks
class EnvEdge():
""""Class representing a graph edge.
Currently not implemented. In the future this should represent different types of transportation.
"""
def __init__(self):
self.edge_type = ''
#point-of-interest
class EnvNode():
"""A point of interest in an Environment Graph.
Represents a physical location or abstracted space of the environment.
For example, a market (or all the markets) in a neighborhood.
Depending on abstraction level, an EnvNode can represent:
A school (finer level);
Residential blocks of a neighborhood;
A neighborhood;
A city;
A state;
A country (coarser level);
Ideally, this class should be constructed with the EnvNodeFactory class.
Attributes:
name: The name of the node.
contained_blobs: A list of the blobs currently occupying this space.
routine: The current time action Routine this region is implementing.
characteristics: The environment characteristics this node has.
"""
def __init__(self):
""""Inits EnvNode with an empty template."""
self.name = ''
self.contained_blobs:list[population.Blob] = []
self.routine: Routine = None
self.characteristics = {}
self.id = util.IDGen('nodes').get_id()
self.containing_region_name = None
def get_characteristic(self, key):
return self.characteristics[key]
def add_characteristic(self, key, value):
self.characteristics[key] = value
def process_routine(self, hour) -> list[TimeAction]:
"""Generates and returns the TimeAction list of the routine for a certain time.
Args:
time: The time slot to be processed.
Returns:
A list containing all TimeAction this EnvNode requires for the corresponding time.
"""
return self.routine.process_routine(hour)
def remove_blob(self, blob: population.Blob):
if isinstance(blob, population.Blob) and blob in self.contained_blobs:
self.contained_blobs.remove(blob)
def remove_blobs(self, blobs: list[population.Blob]):
for blob in blobs:
self.remove_blob(blob)
def add_blob(self, blob: population.Blob):
if isinstance(blob, population.Blob):
assert blob not in self.contained_blobs, "BLOB ALREADY HERE"
self.contained_blobs.append(blob)
def add_blobs(self, blobs: list[population.Blob]):
for blob in blobs:
self.add_blob(blob)
def get_population_size(self, population_template = None):
"""Gets the total population size contained in this EnvNode.
Gets the sum of each blob's get_population_size.
If a population_template is defined, gets the population size of the
population which matches that template.
Args:
population_template: A PopTemplate to be matched by this operation.
Returns:
The sum of each contained_blob's get_population_size, matching the population_template.
"""
count = 0
for blob in self.contained_blobs:
count += blob.get_population_size(population_template)
return count
def grab_population(self, quantity: int, template : population.PopTemplate = None) -> list[population.Blob]:
"""Gets and removes a population matching a template from this EnvNode.
The population removed is returned as a list of blobs, each with a unique mother_blob_id.
If quantity is larger than the current population size matching the tamplate,
this method returns the largest possible population.
Args:
quantity: The desired population size to be grabbed from this EnvNode.
template: The PopTemplate to be matched.,
Returns:
If there are enough population, a list containing the grabbed population. This list might have more than one Blob.
In such case, each blob is guaranteed to be from different mother_blob_id.
If there are not enough population, returns the available amount.
"""
total_available_population = self.get_population_size(template)
if total_available_population == 0:
return []
quantity = min(quantity, total_available_population)
if quantity <= 0:
return []
new_blobs = []
available_quantities = [blob.get_population_size(template) for blob in self.contained_blobs]
int_adjusted_quantities = util.weighted_int_distribution(available_quantities, quantity)
for x in range(len(self.contained_blobs)):
# quantity * ratio of this blobs contribution to the total
if int_adjusted_quantities[x] == 0:
continue
blob = self.contained_blobs[x]
adjusted_quantity = int_adjusted_quantities[x]
grabbed_blob = blob.grab_population(adjusted_quantity, template)
new_blobs.append(grabbed_blob)
self.remove_blobs(new_blobs)
return new_blobs
def change_blobs_traceable_property(self, key, value, quantity:int, template:population.PopTemplate = None):
_grabbed = self.grab_population(quantity, template)
for _blob in _grabbed:
_blob.traceable_properties[key] = value
_blob.previous_node = self.id
#if _blob.spawning_node is None:
# _blob.spawning_node = self.id
_blob.frame_origin_node = self.id
self.add_blobs(_grabbed)
def get_unique_name(self):
return f"{self.containing_region_name}//{self.name}"
def __str__(self):
return '{{\"name\" : \"{0}\", \"id\" : \"{1}\", \"routine\" : {2}, \"characteristics\" : {3}, \"blobs\" : {4}}}'.format(
self.name,
self.id,
self.routine,
self.characteristics,
self.contained_blobs)
def __repr__(self):
return '{{\"name\" : \"{0}\", \"id\" : \"{1}\", \"routine\" : {2}, \"characteristics\" : {3}, \"blobs\" : {4}}}'.format(
self.name,
self.id,
self.routine,
self.characteristics,
self.contained_blobs)
class EnvNodeTemplate():
"""Describes an EnvNode generation template.
EnvNodeFactory objects can generate EnvNodes based on templates.
TODO Use case.
"""
def __init__(self):
self.characteristics = {}
self.routine_template = {}
self.blob_descriptions = []
def add_characteristic(self, key, value):
self.characteristics[key] = value
def add_routine_template(self, hour, actions):
"""Adds a TimeAction to the designated time slot."""
self.routine_template[str(hour)] = actions
def add_blob_description(self, population, traceable_properties, description, blob_factory):
self.blob_descriptions.append((population, traceable_properties, description, blob_factory))
class EnvNodeFactory():
"""A factory to generate EnvNodes with a particular template.
Generates both an EnvNode and the respective Routine.
"""
def __init__(self, _node_template: EnvNodeTemplate):
self.template:EnvNodeTemplate = _node_template
def GenerateRoutine(self, routine_template)->Routine:
routine = Routine()
for k, v in routine_template.items():
routine.add_time_action(v, k)
return routine
def Generate(self, region:EnvRegion, _name)->EnvNode:
node = EnvNode()
node.name = _name
node.routine = self.GenerateRoutine(self.template.routine_template)
for k in self.template.characteristics.keys():
node.characteristics[k] = self.template.characteristics[k]
for (pop,trace,desc,factory) in self.template.blob_descriptions:
blob: population.Blob = factory.GenerateProfile(region.id, node.id, pop, desc, trace)
node.add_blob(blob)
return node
#region
class EnvRegion():
""""Represents a particular region of simulation.
A region is an abstraction for a set of points of interest and initial population.
It is assumed its initial population returns frequently.
EnvRegions are a possible abstraction for an aggregation of points-of-interest.
Depending on abstraction level, an EnvRegion can represent:
A neighborhood containing blocks, markets, schools;
A city containing neighborhoods or discretized regions;
A state containing cities and towns;
A country containing states;
Ideally, an EnvRegion is constructed using an EnvRegionFactory.
Attributes:
id: A unique integer region identifier.
position: The physical location of this region.
population: The total population size in this region.
node_list: Contained EnvNodes in this region.
neighbours: Currently unused. The relation between this region and each other region.
"""
def __init__(self, _position = (0.0, 0.0), _long_lat = (0.0, 0.0), population = 0, _id = None):
"""Initializes an empty EnvRegion"""
self.name:str = ''
self.id: int = _id
if _id is None:
self.id = util.IDGen("regions").get_id()
self.position:tuple[float, float] = _position
self.long_lat:tuple[float, float] = _long_lat
self.population:int = population
self.node_list:list[EnvNode] = []
self.node_dict:dict[str, EnvNode] = {}
self.neighbours = [[]]
def add_node(self, _node: EnvNode):
self.node_list.append(_node)
self.node_dict[_node.name] = _node
_node.containing_region_name = self.name
def get_population_size(self, template = None):
"""Gets the total population size contained in this EnvRegion.
Gets the sum of each EnvNode's get_population_size.
If a population_template is defined, gets the population size of the
population which matches that template.
Args:
population_template: A PopTemplate to be matched by this operation.
Returns:
The sum of each EnvNode in node_list's get_population_size, matching the population_template.
"""
count = 0
for node in self.node_list:
count += node.get_population_size(template)
return count
def get_blob_count(self)->int:
"""Gets the total number of Blobs contained in this EnvRegion."""
return sum([len(nd.contained_blobs) for nd in self.node_list])
def generate_action_list(self, hour: int):
"""Gets the TimeAction list for each EnvNode in this EnvRegion for this time slot.
Generate a TimeAction list which contais each TimeAction for EnvNodes in this EnvRegion.
Args:
time: The requested time slot.
Returns:
The concatenated TimeAction list.
"""
action_list = []
for node in self.node_list:
action_list += node.process_routine(hour)
return action_list
def get_node_by_name(self, name) -> EnvNode:
return self.node_dict[name]
## Not sure if its needed TODO
## maybe we only should do population grabs per node
def grab_population(self, quantity, pop_template):
"""Gets and removes a population matching a template from this EnvRegion.
The population removed is returned as a list of blobs, each with a unique mother_blob_id.
If quantity is larger than the current population size matching the tamplate,
this method returns the largest possible population.
Population is extracted evenly from EnvNodes.
TODO NOT IMPLEMENTED YET.
Args:
quantity: The desired population size to be grabbed from this EnvRegion.
template: The PopTemplate to be matched.,
Returns:
A list containing the grabbed population. This list might have more than one Blob.
In such case, each blob is guaranteed to be from different mother_blob_id.
"""
print("EnvRegion.grab_population not implemented yet")
pass
def __str__(self):
return '{{\"id\" : {0}, \"name\" : \"{1}\", \"position\" : {2}, \"nodes\" : {3}}}'.format(
self.id,
self.name,
self.position,
self.node_list
)
def __repr__(self):
return '{{\"id\" : {0}, \"name\" : \"{1}\", \"position\" : {2}, \"nodes\" : {3}}}'.format(
self.id,
self.name,
self.position,
self.node_list
)
class EnvRegionTemplate():
"""Describes a template for generating EnvRegions"""
def __init__(self):
self.template:list[tuple[str,EnvNodeTemplate]] = []
def add_template_node(self, node_name:str, _node_template:EnvNodeTemplate):
self.template.append((node_name, _node_template))
class EnvRegionFactory():
"""Generates EnvRegions based on a specific EnvRegionTemplate."""
def __init__(self, _template:EnvRegionTemplate):
self.region_template = _template
def Generate(self, _position:tuple[float,float]):
region = EnvRegion(_position)
for c in self.region_template.template:
node_name, node_template = c
factory = EnvNodeFactory(node_template)
node = factory.Generate(region,node_name)
region.add_node(node)
return region
class EnvironmentGraph():
"""Models the top level of the Crowd Dynamics simulator. Additionally, handles TimeAction and Routine logic.
The top level abstraction represents a group of EnvRegions. This can be used to model a city, country, etc.
The TimeAction and Routine logic is handled by the EnvironmentGraph, since TimeActions are abstractions for
command-pattern graph operations.
TimeActions are defined in the EnvironmentGraph as either being base actions or not.
Base actions translate directly into graph operations, moving population, for example.
Composite actions are to be decomposed into any number of base actions, depending on that it needs to achieve.
For example, a composite action could return every blob with mother_blob_id = 1 to its native EnvRegion, this
action translates into several move_poplation TimeActions.
Each time step update follows the following flow:
The EnvironmentGraph gathers the TimeActions for each EnvNode.
The EnvironmentGraph simpifies the TimeAction list into base actions.
The EnvironmentGraph balances the TimeAction list into base actions, this operation is also responsible for guaranteeing all time actions are valid currently.
Finally, each TimeAction is applied.
The same algorithm is applied for repeating_actions.
For each repeating action, the EnvironmentGraph check
timestep % cycle_length == 0
and processes that time action for each EnvNode.
The EnvGraph calls the repeating_action with the desired values, setting region and node for each node in the EnvGraph.
Default available action types are:
move_population
Moves population from one node to another. TODO Assumes a valid operation.
Params:
origin_region: origin region.
origin_node: origin node.
destination_region: destination region.
destination_node: destination node.
quantity: population size to be moved.
population_template: PopTemplate to be matched by the operation.
return_population_home
Moves each population native to a region back.
Params:
region: region calling people home
node: node calling people home
quantity: quantity of people to come home, currently not implemented TODO
population_template: PopTemplate of population to return
Attributes:
region_list: List of contained EnvRegions.
node_list: List of contained EnvNodes.
edge_table: Relationship between neighboring EnVRegions. Currently, unused.
time_action_map: Maps each TimeAction type to the relevant graph operation function. This should be extensible in a future version.
base_actions: A set containing each base action the EnvironmentGraph supports. This should be extensible in a future version.
self.global_actions : placeholder
self.repeating_global_actions : A list of pairs [cycle_length, time_action]. Each repeating action is repeated every cycle_length steps.
"""
def __init__(self):
self.region_list: list[EnvRegion] = []
self.region_dict: dict[str,EnvRegion] = {}
self.region_id_dict = {}
self.node_list: list[EnvNode] = []
self.node_id_dict = {}
self.node_dict: dict[str,EnvNode]= {}
self.edge_table = [[]]
self.routine_day_length = 24
self.time_action_map = { 'move_population' : self.move_population }
self.base_actions = {'move_population'}
self.loaded_plugins: list[TimeActionPlugin] = []
self.global_actions = set()
# repeating actions are a tuple of (cycle_length or frame_list, time_action)
self.repeating_global_actions = []
# Logging data
self.original_population_template = None
self.original_block_template: population.BlockTemplate = None
self.original_repeating_actions = None
# Queued actions
self.next_frame_queue = []
# Queued actions priority 'first' or 'last'
self.queued_action_priority = 'first'
def get_node_by_name(self, region_name, node_name):
return self.region_dict[region_name].get_node_by_name(node_name)
def get_node_by_id(self, _id) -> EnvNode:
return self.node_id_dict[_id]
def get_region_by_name(self, name) -> EnvRegion:
return self.region_dict[name]
def get_region_by_id(self, _id) -> EnvRegion:
return self.region_id_dict[_id]
# Action Invoke Modes
def process_routines(self, hour):
action_list = []
for region in self.region_list:
action_list += region.generate_action_list(hour)
return action_list
def direct_action_invoke(self, action, hour, time):
self.consume_time_action(action, hour, time)
def queue_next_frame_action(self, action):
self.next_frame_queue.append(action)
def process_queued_actions(self):
action_list = []
action_list += self.next_frame_queue
self.next_frame_queue.clear()
return action_list
def process_repeating_global_actions(self, hour):
action_list = []
# do repeating actions
for rga in self.repeating_global_actions:
if (type(rga[0]) is int and hour % rga[0] == 0) or (type(rga[0]) is list and hour in rga[0]):
for region in self.region_list:
for node in region.node_list:
if 'node_name' in rga[1].values and node.name != rga[1].values['node_name']:
continue
action = copy.deepcopy(rga[1])
if type(rga[0]) is list:
action.values['frames'] = rga[0]
action.values['region'] = region.name
action.values['node'] = node.name
action.values['node_id'] = node.id
action_list += [action]
return action_list
# END Action Invoke Modes
def get_population_size(self, population_template = None):
"""Gets the total population size contained in this EnvironmentGraph.
Gets the sum of each node's get_population_size.
If a population_template is defined, gets the population size of the
population which matches that template.
Args:
population_template: A PopTemplate to be matched by this operation.
Returns:
The sum of each node's get_population_size, matching the population_template.
"""
size = 0
for node in self.node_list:
size += node.get_population_size(population_template)
return size
def get_blob_count(self)->int:
"""Gets the total number of Blobs contained in this EnvironmentGraph."""
return sum([rg.get_blob_count() for rg in self.region_list])
def update_time_step(self, hour, time):
"""Updates a time step for a given time.
Updates Routines and Repeating Global Actions.
Applies every TimeAction which matches time argument.
"""
actions = self.generate_action_list(hour)
simplified_actions = self.simplify_action_list(actions, hour, time)
#balanced_actions = self.balance_action_list(simplified_actions)
#for action in balanced_actions:
# self.consume_time_action(action, hour, time)
for action in simplified_actions:
self.consume_time_action(action, hour, time)
#merge all nodes coming from the same origin
for node in self.node_list:
self.merge_node(node)
# set frame origin nodes for all blobs
self.set_frame_origin_nodes()
def generate_action_list(self, hour):
"""Generates the TimeAction list for every EnvRegion, for a given time slot and the Repeating Global Actions for this hour."""
action_list = []
if self.queued_action_priority == 'first':
action_list += self.process_queued_actions()
action_list += self.process_routines(hour)
action_list += self.process_repeating_global_actions(hour)
if self.queued_action_priority == 'last':
action_list += self.process_queued_actions()
return action_list
def consume_time_action(self, time_action, hour, time):
"""Applies the graph operations (moving population, etc) of a given TimeAction.
Args:
time_action: A TimeAction to be processed.
"""
action_type = time_action.type
values = time_action.values
if action_type in self.base_actions:
self.time_action_map[action_type](values, hour, time)
else:
simplified_action = self.time_action_map[action_type](values, hour, time)
for action in simplified_action:
#print('\n\naction ', action, '\n\n')
self.consume_time_action(action, hour, time)
def balance_action_list(self, action_list):
## corrects the quantities of agents flow to and from each region
## TODO not implemented yet
##print('EnvironmentGraph.balance_action_list not implemented yet')
# TODO probably wrong because of templates
totals = {}
# accumulate totals
for action in action_list:
og_node = action['origin_node']
pop_temp = action['population_template']
node_pop = og_node.get_population_size(pop_temp)
quantity = action['quantity']
interpreted_quantity = max(-1, min(quantity, node_pop))
totals[og_node] += interpreted_quantity
# correct quantities
for action in action_list:
og_node = action['origin_node']
node_pop = og_node.get_population_size(pop_temp)
quantity = action['quantity']
action['quantity'] = int((quantity / totals[og_node]) * min(node_pop, totals[og_node]))
return action_list
def apply_action_list(self, action_list):
for action in action_list:
self.time_action_map[action.type](action.values)
def simplify_action_list(self, action_list, hour, time):
#simp_list = []
while not all([x.type in self.base_actions for x in action_list]):
i = action_list.pop(0)
if i.type not in self.base_actions:
sub_list = self.time_action_map[i.type](i.values, hour, time)
action_list += sub_list
else:
action_list += [i]
# i = 0
# while i != len(action_list):
# item = action_list[i]
# if item.type not in self.base_actions:
# sub_list = self.time_action_map[item.type](item.values, hour, time)
# action_list.remove(item)
# action_list += sub_list
# else:
# i+=1
#print(f'All stuff is true in this bagaça at time {time} with len {len(action_list)} = {all([x.type in self.base_actions for x in action_list])}\n')
#print(f'Centro total movement = {sum([i.values["quantity"] if i.values["origin_region"] == "Centro" else 0 for i in action_list ])}')
return action_list
def add_region(self, _position, _template: EnvRegionTemplate, name):
factory = EnvRegionFactory(_template)
new_region = factory.Generate(_position)
new_region.name = name
new_region.population = new_region.get_population_size()
self.edge_table.append(['' for x in range(len(self.region_list))])
for node in new_region.node_list:
node.containing_region_name = new_region.name
self.node_list.append(node)
self.node_dict[node.get_unique_name()] = node
self.node_id_dict[node.id] = node
self.region_dict[name] = new_region
self.region_list.append(new_region)
self.region_id_dict[new_region.id] = new_region
def add_edge(self, region1, region2, _type):
self.edge_table[region1][region2] = _type
def set_global_action(self, action_type):
self.global_actions.add(action_type)
def set_repeating_action(self, cycle_length: int, action):
self.repeating_global_actions.append((int(cycle_length), action))
def set_repeating_action(self, frames: list[int], action):
self.repeating_global_actions.append((frames, action))
def remove_action(self, action_type):
self.time_action_map.pop(action_type)
def add_function(self, action_type, function, is_base):
self.time_action_map[action_type] = function
if is_base:
self.base_actions.add(action_type)
def LoadPlugin(self, plugin:TimeActionPlugin):
self.loaded_plugins.append(plugin)
for k, v in plugin.get_pairs().items():
self.time_action_map[k] = v
def has_plugin(self, _type:type) -> bool:
return any(isinstance(x, _type) for x in self.loaded_plugins)
def get_plugins(self, _type:type) -> list:
return [p for p in self.loaded_plugins if isinstance(p,_type)]
def get_first_plugin(self, _type:type):
for p in self.loaded_plugins:
if isinstance(p,_type): return p
return None
def merge_blobs(self):
"""Merges every blob with a compatible mother_blob_id in a given EnvNode."""
for node in self.node_list:
i = 0
blob_list = node.contained_blobs
while i < len(blob_list):
current_blob = blob_list[i]
for j in range(i+1, len(blob_list[i+1:])):
other_blob = blob_list[j]
node.remove_blob(other_blob)
#if current_blob.mother_blob_id == other_blob.mother_blob_id:
# current_blob.consume_blob(other_blob)
if current_blob.node_of_origin == other_blob.node_of_origin:
current_blob.consume_blob(other_blob)
i+=1
def add_blobs_traceable_property(self, key, value):
for node in self.node_list:
for blob in node.contained_blobs:
blob.traceable_properties[key] = value
# def lambda_blobs_traceable_property(self, key, lambda_funtion):
# for node in self.node_list:
# for blob in node.contained_blobs:
# blob.traceable_properties[key] = lambda_funtion(blob.traceable_properties[key])
#print("days", blob.traceable_properties[key])
def lambda_blobs_traceable_property(self, key, lambda_funtion):
for node in self.node_list:
for blob in node.contained_blobs:
blob.traceable_properties[key] = lambda_funtion(blob, blob.traceable_properties[key])
def merge_node(self, node: EnvNode):
i = 0
blob_list = node.contained_blobs
while i < len(blob_list):
current_blob: population.Blob = blob_list[i]
j = i+1
while j < len(blob_list):
other_blob: population.Blob = blob_list[j]
if current_blob.mother_blob_id == other_blob.mother_blob_id and current_blob.compare_traceable_properties_to_other(other_blob):
current_blob.consume_blob(other_blob)
node.remove_blob(other_blob)
else:
j+=1
i+=1
## time action functions
## Pre-condition: assumes move population operation is valid
def move_population(self, values, hour, time):
""" Move population. Base Operation.
Grabs population according to a population template. and moves between nodes.
quantity -1 moves every person matching template.
"""
if values['quantity'] == 0:
return
origin_region = values['origin_region']
if isinstance(origin_region, str):
origin_region = self.get_region_by_name(origin_region)
origin_node = values['origin_node']
if isinstance(origin_node, str):
origin_node = origin_region.get_node_by_name(origin_node)
destination_region = values['destination_region']
if isinstance(destination_region, str):
destination_region = self.get_region_by_name(destination_region)
destination_node = values['destination_node']
if isinstance(destination_node, str):
destination_node = destination_region.get_node_by_name(destination_node)
pop_template = values['population_template']
quantity = values['quantity']
if quantity == -1:
quantity = origin_node.get_population_size(pop_template)
if quantity == 0:
return
available_total = origin_node.get_population_size()
available = origin_node.get_population_size(pop_template)
grabbed_population = origin_node.grab_population(quantity, pop_template)
for grab_pop in grabbed_population:
#if grab_pop.previous_node != origin_node.id:
# grab_pop.previous_node = origin_node.id
grab_pop.frame_origin_node = origin_node.id
destination_node.add_blobs(grabbed_population)
def set_spawning_nodes(self):
for node in self.node_list:
for blob in node.contained_blobs:
if blob.node_of_origin is None:
print("WTF SPAWN")
blob.spawning_node = node.id
def set_frame_origin_nodes(self):
for node in self.node_list:
for blob in node.contained_blobs:
blob.frame_origin_node = node.id
## END time action functions
def __str__(self):
return "{\"graph\":" + str(self.region_list) + "}"
def __repr__(self):
return "{\"graph\":" + str(self.region_list) + "}"
# Defines a time action command
class TimeAction():
"""Describes a command-like TimeAction.
TimeActions should be modelled and described in a contract for the simulator.
Type and expectd values should be respected.
TimeActions are either base actiors or composite actions.
Each TimeAction is to be associated either:
With a graph operator, for base actions; or
A base TimeAction decomposition, for composite actions.
"""
def __init__(self, _type = '', _values = {}):
self.type = _type
self.values = _values
if "population_template" in self.values:
if isinstance(self.values["population_template"], dict):
self.values["population_template"] = population.PopTemplate(self.values["population_template"])
def __str__(self):
return '{{\"type\" : \"{0}\", \"values\" : {1}}}'.format(self.type,
self.values)
def __repr__(self):
return '{{\"type\" : \"{0}\", \"values\" : {1}}}'.format(self.type,
self.values)
class TimeActionPlugin():
"""Describes a set of action types and action function pairs.
This class is used to extend the functionality of the simulator with additional TimeActions.
Added functions should be
(string, dict) -> [TimeAction]
the dict is a dictionary of values to be read from the input json.
This parameters passed in the dictionary are defined by the plugin contracts.
"""
def __init__(self):
self.type_action_pairs = {}
self.logger = None
def set_pair(self, action_type, action_function):
self.type_action_pairs[action_type] = action_function
def get_pairs(self):
return self.type_action_pairs
def setup_logger(self,logger):
raise NotImplementedError("SubClass should implement the \"setup_logger\" method")
def log_data(self,logger):
raise NotImplementedError("SubClass should implement the \"log_data\" method")
def stop_logger(self,logger):
raise NotImplementedError("SubClass should implement the \"stop_logger\" method")
class Routine():
"""Describes a mapping of time slot -> TimeAction.
This mapping should describe the routine an EnvNode follows during the simulation period, cyclically.
The period of Routine repetition is part of simulation modelling.
Each time slot matches to one specific TimeAction, describing the requested operation for any given time slot.
TODO Make so time_actions maps time_slot -> [actions] instead of time_slot -> action
"""
def __init__(self):
# map of time -> time_action
self.time_actions = {}
self.label = None
def add_time_action(self, time_action, hour):
# if time not in self.time_actions:
# self.time_actions[time] = []
# self.time_actions[time].append(time_action)
self.time_actions[str(hour)] = time_action
def process_routine(self, hour) -> list[TimeAction]:
if str(hour) in self.time_actions:
return self.time_actions[str(hour)]
else:
return []
def __str__(self):
return '{{\"name\" : \"{0}\", \"actions\" : {1}}}'.format(
self.label,
self.time_actions
)
def __repr__(self):
return '{{\"name\" : \"{0}\", \"actions\" : {1}}}'.format(
self.label,
self.time_actions
)