-
Notifications
You must be signed in to change notification settings - Fork 1
/
fitness_evaluator.py
executable file
·451 lines (318 loc) · 15.7 KB
/
fitness_evaluator.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
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
import pickle
import numpy as np
from shapely.ops import cascaded_union
from shapely.geometry import Point, MultiPolygon
class Evaluator(object):
def __init__(self, parametric=True):
self._parametric=parametric
def _exploration_fit(self, case, return_components = False):
if case.agents[0].sensors.get("Coverage") is None:
if return_components:
return {}
else:
return 0.
squares = []
coverage = 0.
for i in xrange(10):
for j in xrange(10):
squares.append(case.blackboard["Coverage"][i,j])
squares = sorted(squares)
num_agents = len(case.agents)
max_velocity = case.agents[0].platform.max_velocity
max_time = case.config["config_simulator"]["max_time"]
max_squares = float(num_agents * max_velocity * max_time) / (100.)
max_square_median = max_squares/ (10.*10.)
median_frequency_normalized = float(squares[len(squares)/2]) / max_square_median
if return_components:
components = {}
components["median"] = median_frequency_normalized
return components
else:
fitness = median_frequency + coverage
return fitness
def _exploration_fit_live_delta(self, case1, case2, dt, return_components = False):
if case2.agents[0].sensors.get("Coverage") is None:
if return_components:
return {}
else:
return 0.
squares1 = []
squares2 = []
for i in xrange(10):
for j in xrange(10):
if case1 is not None:
squares1.append(case1.blackboard["Coverage"][i,j])
squares2.append(case2.blackboard["Coverage"][i,j])
num_agents = len(case2.agents)
max_velocity = case2.agents[0].platform.max_velocity
max_squares = float(num_agents * max_velocity) / (100.)
max_square_median = max_squares/ (10.*10.)
median_frequency_normalized = float(squares2[len(squares2)/2]) / max_square_median
coverage_delta = max(0.,sum(squares2)-sum(squares1))
normalized_coverage_delta = (coverage_delta) / (max_squares * dt)
if return_components:
components = {}
components["average"] = normalized_coverage_delta
return components
else:
raise NotImplemented()
def _localization_fit(self, case, return_components = False):
if case.agents[0].sensors.get("Localization") is None:
if return_components:
return {}
else:
return 0.
location_estimates = []
for agent in case.agents:
location_estimates.extend(agent.sensors.get("Localization").history)
location_estimate_variance = 0.
location_mean = np.array([0.,0.])
actual_position = case.agents[0].sensors.get("Localization")._emitter_true_location
for estimate in location_estimates:
location_mean = location_mean + estimate
location_mean = location_mean/len(location_estimates)
for estimate in location_estimates:
distance = np.linalg.norm(estimate-location_mean)
location_estimate_variance += distance
location_estimate_variance = location_estimate_variance/len(location_estimates)
if return_components:
components = {}
components["variance"] = min(1.,location_estimate_variance/500.)
return components
else:
fitness = min(1.,location_estimate_variance/500.)
return fitness
def _network_fit(self, case1, case2, return_components = False):
if case1.agents[0].sensors.get("Relay") is None:
if return_components:
return {}
else:
return 0.
covered_area_avg = 0.
for case in [case1,case2]:
connection_sets = []
for agent in case.agents:
member_of = []
for i,connection_group in enumerate(connection_sets):
connected_to = False
for connected_agent in agent.sensors.get("Relay").connections:
if connected_agent in connection_group:
connected_to = True
if connected_to:
member_of.append((i,connection_group))
if len(member_of) == 0:
new_set = set()
new_set.add(agent)
connection_sets.append(new_set)
elif len(member_of) == 1:
member_of[0][1].add(agent)
else:
new_set = set()
for i,connection_group in member_of:
new_set.update(connection_group)
member_of.reverse()
for i,_ in member_of:
connection_sets.pop(i)
connection_sets.append(new_set)
largest_set = max(connection_sets, key=lambda s: len(s))
polygons = []
com_range = 0.
for agent in largest_set:
x,y = agent.platform.position
com_range = agent.sensors.get("Relay")._range
p = Point(x,y).buffer(com_range)
polygons.append(p)
covered_area = cascaded_union(MultiPolygon(polygons)).area
max_covered_area = (len(case.agents)*3.14*com_range*com_range)/2.0
covered_area_percentage = covered_area / max_covered_area
covered_area_avg += covered_area_percentage
if return_components:
components = {}
components["covered"] = min(1.,covered_area_avg/2.0)
return components
else:
raise Exception("Not properly implemented")
fitness = float(len(largest_set))/len(case.agents)
return fitness
def _network_fit_live(self, case, return_components = False):
if case.agents[0].sensors.get("Relay") is None:
if return_components:
return {}
else:
return 0.
connection_sets = []
for agent in case.agents:
member_of = []
for i,connection_group in enumerate(connection_sets):
connected_to = False
for connected_agent in agent.sensors.get("Relay").connections:
if connected_agent in connection_group:
connected_to = True
if connected_to:
member_of.append((i,connection_group))
if len(member_of) == 0:
new_set = set()
new_set.add(agent)
connection_sets.append(new_set)
elif len(member_of) == 1:
member_of[0][1].add(agent)
else:
new_set = set()
for i,connection_group in member_of:
new_set.update(connection_group)
member_of.reverse()
for i,_ in member_of:
connection_sets.pop(i)
connection_sets.append(new_set)
largest_set = max(connection_sets, key=lambda s: len(s))
polygons = []
com_range = 0.
for agent in largest_set:
x,y = agent.platform.position
com_range = agent.sensors.get("Relay")._range
p = Point(x,y).buffer(com_range)
polygons.append(p)
covered_area = cascaded_union(MultiPolygon(polygons)).area
max_covered_area = (len(case.agents)*3.14*com_range*com_range)/2.0
covered_area_percentage = covered_area / max_covered_area
if return_components:
components = {}
components["covered"] = min(1.,covered_area_percentage)
return components
else:
raise Exception("Not properly implemented")
fitness = float(len(largest_set))/len(case.agents)
return fitness
def _base_fit(self, case1, case2, return_components=False):
movement = 0.
for agent_start, agent_end in zip(case1.agents, case2.agents):
distance = np.linalg.norm(agent_start.platform.position-agent_end.platform.position)
movement += min(1., distance/100.)
if return_components:
components = {}
components["movement"] = movement/len(case1.agents)
return components
else:
fitness = movement/len(case1.agents)
return 1. + fitness
def post_evaluator_loggers(self, list_of_loggers):
summed = 1.
for logger in list_of_loggers:
with logger as open_logger:
summed *= self._case_evaluator(open_logger._simulation_log)
return summed
def post_evaluator_shelves(self, list_of_shelves):
summed = 1.
for shelve_t in list_of_shelves:
summed *= self._case_evaluator(shelve_t)
return summed
def _case_evaluator(self, run_log):
ticks = sorted(map(int,run_log.keys()))
start_case_id = str(min(ticks))
end_case_id = str(max(ticks))
start_case = run_log[start_case_id]
end_case = run_log[end_case_id]
fit_base = self._base_fit(start_case, end_case)
fit_net = self._network_fit(end_case)
fit_loc = self._localization_fit(end_case)
fit_exp = self._exploration_fit(end_case)
fitness = fit_base + fit_net + fit_loc + fit_exp
return fitness
def fitness_components(self, list_of_shelves):
components = {}
for shelve_t in list_of_shelves:
components[case_name] = self._case_components(shelve_t)
return components
def _case_components(self, shelve):
ticks = sorted(map(int,shelve.keys()))
start_case_id = str(min(ticks))
middle_case_id = str((max(ticks)-min(ticks))/2)
end_case_id = str(max(ticks))
start_case = shelve[start_case_id]
middle_case = shelve[middle_case_id]
end_case = shelve[end_case_id]
case_name = str(start_case)
case_component = {}
case_component["network"] = self._network_fit(middle_case, end_case, return_components=True)
case_component["localization"] = self._localization_fit(end_case, return_components=True)
case_component["exploration"] = self._exploration_fit(end_case, return_components=True)
return case_component
def fitness_map_elites(self, list_of_loggers):
test_fitness = 0.
characteristics = {}
for logger in list_of_loggers:
with logger as open_logger:
case_components = self._case_components(open_logger._simulation_log)
for application, application_component in case_components.items():
for measure, value in application_component.items():
characteristics_name = "_".join([application,measure])
if characteristics.get(characteristics_name) is None:
characteristics[characteristics_name] = value
else:
characteristics[characteristics_name] += value
config = open_logger._simulation_log['0'].config
platform_name = config["platform_templates"].keys()[0]
test_fitness += 2./(1.+np.linalg.norm(config["platform_templates"][platform_name]["config_behavior"]["weights"])+np.linalg.norm(config["platform_templates"][platform_name]["config_behavior"]["scale"]))
for key,value in characteristics.items():
characteristics[key] = value/len(list_of_loggers)
test_fitness = test_fitness/len(list_of_loggers)
return test_fitness, characteristics
def fitness_from_simlog(self, simulation_log):
characteristics = {}
try:
case_components = self._case_components(simulation_log)
except:
return None
for application, application_component in case_components.items():
for measure, value in application_component.items():
characteristics_name = "_".join([application,measure])
characteristics[characteristics_name] = value
config = simulation_log['0'].config
platform_name = config["platform_templates"].keys()[0]
test_fitness = 2./(1.+np.linalg.norm(config["platform_templates"][platform_name]["config_behavior"]["weights"])+np.linalg.norm(config["platform_templates"][platform_name]["config_behavior"]["scale"]))
return test_fitness, characteristics
def live_fitness(self, case1, case2, dt):
characteristics = {}
case_components = {}
case_components["network"] = self._network_fit_live(case2, return_components=True)
case_components["localization"] = self._localization_fit(case2, return_components=True)
case_components["exploration"] = self._exploration_fit_live_delta(case1, case2, dt, return_components=True)
for application, application_component in case_components.items():
for measure, value in application_component.items():
characteristics_name = "_".join([application,measure])
if characteristics.get(characteristics_name) is None:
characteristics[characteristics_name] = value
else:
characteristics[characteristics_name] += value
config = case2.config
platform_name = config["platform_templates"].keys()[0]
test_fitness = 2./(1.+np.linalg.norm(config["platform_templates"][platform_name]["config_behavior"]["weights"])+np.linalg.norm(config["platform_templates"][platform_name]["config_behavior"]["scale"]))
return test_fitness, characteristics
class LiveFitness(object):
def __init__(self):
self._evaluator = Evaluator()
self._previous_case = None
self._last_time = -1
def evaluate(self, current_time, case):
if self._previous_case is None:
upcase = None
else:
upcase = pickle.loads(self._previous_case)
r = self._evaluator.live_fitness(upcase, case, current_time-self._last_time)
self._previous_case = pickle.dumps(case)
self._last_time = current_time
return r
if __name__=="__main__":
import shelve, cPickle
import argparse
shelve.Pickler = cPickle.Pickler
shelve.Unpickler = cPickle.Unpickler
def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument("filename", nargs=1, type=str)
return parser
parser = create_parser()
args = parser.parse_args()
eva = Evaluator()
cases = shelve.open(args.filename[0])
print eva._case_components(cases)