-
Notifications
You must be signed in to change notification settings - Fork 1
/
case.py
168 lines (114 loc) · 5.54 KB
/
case.py
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
import Queue, random
import yaml
import tinyarray as ta
import numpy as np
from agent import Agent
class Blackboard(object):
def __init__(self):
self.objects = {}
def get_object(self, name, generator=None):
if self.objects.get(name) is not None:
return self.objects.get(name)
else:
if generator is None:
return
else:
self.objects[name] = generator()
return self.objects[name]
def __getitem__(self, name):
return self.get_object(name)
class Case(object):
def __init__(self, config):
self.config = config
self.config["seed"] = random.random()
self._expanded = False
self._halted = False
self._previous_positions = None
def __str__(self):
case_name = ""
if self.config.get('epoch') is not None:
case_name += "Epoch %s/" % self.config.get('epoch')
if self.config.get('generation') is not None:
case_name += "Generation %s/" % self.config.get('generation')
if self.config.get('individual') is not None:
case_name += "I-%s " % self.config.get('individual')
case_name += self.config['name']
return case_name
def _init_agents(self, config_agents, platform_templates):
agents = []
for agent_type, agent_count in config_agents.items():
assert platform_templates.get(agent_type) is not None, "No suitable template could be found for %s" % agent_type
for i in range(agent_count):
agents.append(Agent(platform_templates[agent_type]))
return agents
def __enter__(self):
if self._expanded:
raise Exception("Case already expanded")
random.seed(self.config["seed"])
assert self.config.get("config_agents") is not None, "Configuration for agents was not found"
assert self.config.get("platform_templates") is not None, "Platform templates for agents was not found"
self.agents = self._init_agents(self.config["config_agents"], self.config["platform_templates"])
self._expanded = True
self._halted = False
self.priority_queue = Queue.PriorityQueue()
self.blackboard = Blackboard()
self.current_time = 0.
return self
def __exit__(self, type, value, traceback):
self._expanded = False
del self.blackboard
del self.priority_queue
for agent in self.agents:
del agent
return
def _init_events(self, logger, visualization):
for agent in self.agents:
for event_time, event in agent.bootstrap_events(0, self):
self.priority_queue.put((event_time, event))
if logger is not None:
logger.set_log_delay(self.config["config_simulator"]["log_delay"])
logger_event = logger.update(0, self)
self.priority_queue.put(logger_event[0])
if visualization is not None:
visualization_event = visualization.update(0, self)
self.priority_queue.put(visualization_event[0])
check_stalled_event = self._check_stalled_simulation(0.0, self)
self.priority_queue.put(check_stalled_event[0])
def _check_stalled_simulation(self, current_time, case):
new_positions = [agent.platform.position for agent in self.agents]
if self._previous_positions is not None:
distance_moved = 0.
for old_position, new_position in zip(self._previous_positions, new_positions):
delta = np.linalg.norm(old_position-new_position)
distance_moved += delta
if distance_moved < 0.1:
self._halted = True
print "Simulated stalled, exiting"
self._previous_positions = new_positions
return [(current_time + 10., self._check_stalled_simulation)]
def run(self, logger, visualization=None):
if not self._expanded:
raise Exception("Case not expanded")
assert self.config.get("config_simulator") is not None, "Configuration for simulator was not found"
#Initalize variables
random.seed(self.config["seed"])
max_time = self.config["config_simulator"]["max_time"]
current_time = 0.0
#Generate initial events for time 0.
self._init_events(logger, visualization)
#Process events while simulation durations limit is not reached and no other reason to halt has been found
while self.current_time < max_time and not self._halted:
#Get a new event to process
self.current_time, event = self.priority_queue.get()
#Process event (this can be anything from agent tick to logging or visalization
event_results = event(self.current_time, self)
#Put new resulting events into the queue
[self.priority_queue.put(new_event) for new_event in event_results]
def __getstate__(self):
state = self.__dict__.copy()
# Remove the unpicklable entries.
del state['priority_queue']
del state['_previous_positions']
return state
def get_dict(self):
return self.__getstate__()