-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver_assistant.py
549 lines (417 loc) · 17 KB
/
driver_assistant.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
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
import geographic_agent
import map
import time
import networkx as nx
import numpy as np
import osmnx as ox
class driver_assistant(geographic_agent.geographic_agent):
def __init__(self, origin, route, initial_battery, max_battery_capacity, battery_threshold, battery_spent, map, is_priority, id, time_u, price_u, distance_u, simulation):
#Generic Attributes
self.id = id
self.name = "driver assistant"
self.is_priority = is_priority
self.is_charging = False
self.time_to_wait = 0
self.need_charge = False
self.simulation = simulation
#Battery stuff
self.battery = initial_battery
self.battery_threshold = battery_threshold * max_battery_capacity
self.max_battery_capacity = max_battery_capacity
self.battery_spent_per_tick = self.max_battery_capacity * battery_spent
self.battery_needed = self.max_battery_capacity - self.battery
self.died = False
#Agent has the map
self.map = map
self.G = map.get_map()
#Route and Coordenates
self.list_route = route
self.lng = self.G.nodes[origin]['x']
self.lat = self.G.nodes[origin]['y']
if is_priority:
geographic_agent.geographic_agent.__init__(self, self.lat, self.lng, 'b', 'o', 20, 2)
else:
geographic_agent.geographic_agent.__init__(self, self.lat, self.lng, 'g', 'o', 20, 2)
#For Animation
self.route_idx = 0
self.current_route = self.list_route[0]
self.current_node = origin
self.destination = self.current_route[-1]
self.last_destination = self.current_route[-1]
#Environment View
self.da_list = [] #List of Driver Assistants
self.ch_list = [] #List of Charger Handlers
self.emotional_dict = {}
self.options = []
self.charging_station = None #CH of choice
self.proposals = []
#Deliberative and Emotional
self.time_u = time_u
self.price_u = price_u
self.distance_u = distance_u
self.desires = ['On route', 'Go charge', 'Charging', 'Waiting', 'Stop']
self.actions = ['move', 'change route', 'charge', 'wait']
self.plan = []
self.intention = 'On route' #Car must start with battery
self.current_desires = []
#
#
# I N T I T I A L I Z E R S
#
#
#Initialize the Driver Assistants List
def init_da_list(self, da):
self.da_list = da
#Initialize the Charger Handlers List
def init_ch_list(self, ch):
self.ch_list = ch
self.init_emotional_dict()
#Initialize the emotional dict
def init_emotional_dict(self):
#emotions are a list of values between 0 and 1
emotions = self.generate_emotions()
i = 0
for ch in self.ch_list:
self.emotional_dict[ch.id] = emotions[i]
i += 1
def generate_emotions(self):
emotions = np.random.randint(low=0, high=5, size=len(self.ch_list))
return emotions
#
#
# A C T
#
#
def act(self):
self.updateBeliefs()
print('DA'+str(self.id)+': My battery = '+str(self.battery))
if len(self.plan)>0 and self.succeededIntention() and not self.impossibleIntention():
action = self.plan[0]
if self.isPlanSound(action):
print("DA "+str(self.id)+": My action " + action)
action = self.plan.pop(0)
self.execute(action)
else:
self.rebuildPlan(action)
if self.reconsider():
self.deliberate()
self.buildPlan()
else:
self.deliberate()
self.buildPlan()
self.agentReactiveDecision()
#
#
# D E L I B A R A T I V E A R Q U I T E C T U R E
#
#
def updateBeliefs(self):
if self.is_car_charging():
self.time_to_wait = self.ask_for_time()
else:
if self.battery <= 0:
self.died = True
else:
self.died = False
self.battery -= self.battery_spent_per_tick
self.battery_needed = self.max_battery_capacity - self.battery
if self.low_battery():
print("DA "+str(self.id)+": BATTERY IS LOW")
self.need_charge = True
else:
self.need_charge = False
def deliberate(self):
self.current_desires = []
print("DA "+str(self.id)+":Deliberate")
#TOP Priority
if any(self.proposals) and not self.is_car_charging() and self.need_charge and not self.died:
ch_Id = self.charging_station.id()
new_ch = change_station()
if new_ch == None:
self.current_desires.append('Continue')
elif new_ch.id() != ch_Id: #There is a better option
self.charging_station = new_ch
self.proposals.clear()
self.current_desires.append('Change station')
elif self.need_charge and self.died:
self.current_desires.append('Die')
elif self.need_charge:
print("DA "+str(self.id)+":My desire to charge")
self.current_desires.append('Go charge')
elif not self.need_charge:
self.current_desires.append('On route')
self.intention = self.current_desires[0]
def succeededIntention(self): #Always successfull
return True
def impossibleIntention(self): #Always possible
return False
def buildPlan(self):
if self.intention == 'Continue':
return
self.plan = []
if self.intention == 'Go charge':
self.plan.append('decide station')
self.plan.append('go to station')
self.plan.append('move')
self.plan.append('arrived')
self.plan.append('wait')
self.plan.append('return')
self.plan.append('move')
self.plan.append('resume route')
elif self.intention == 'Change station':
#Already changed station
self.plan.append('go to station')
self.plan.append('move')
self.plan.append('arrived')
self.plan.append('wait')
self.plan.append('return')
self.plan.append('move')
self.plan.append('resume route')
elif self.intention == 'On route':
self.plan.append('move normaly')
elif self.intention == 'Die':
self.plan.append('stop')
def isPlanSound(self, action):
if action == 'arrived':
return agent_has_arrived(self.current_node, self.destination)
elif action == 'go to station':
if self.charging_station == None:
return False
else:
return True
elif action == 'return':
return not self.is_car_charging()
elif action == 'resume route':
return agent_has_arrived(self.current_node, self.destination)
else:
return True
def execute(self, action):
if action == 'move normaly':
self.teleport(action)
elif action == 'move':
self.teleport(action)
elif action == 'decide station':
print("DA "+str(self.id)+": Deciding station")
self.options = self.check_options()
self.charging_station = self.decide()
if self.charging_station != None:
self.update_time_travel()
self.charging_station.add_da_to_queue_inc(self)
elif action == 'go to station':
self.teleport(action)
elif action == 'arrived':
self.charging_station.remove_da_to_queue_inc(self)
self.charging_station.add_da_to_queue(self)
elif action == 'wait':
self.map.add_points_to_print((self.get_longitude(),self.get_latitude()),'y','+',20)
self.simulation.number_cars_charging[self.simulation.current_step] += 1
print("DA id: %d is charging on station %d" , self.id, self.charging_station.id)
elif action == 'return':
self.charging_station = None
self.init_emotional_dict()
self.teleport(action)
elif action == 'resume route':
self.route_idx += 1
self.current_route = self.list_route[self.route_idx % 3]
self.destination = self.current_route[-1]
elif action == 'stop':
self.map.add_points_to_print((self.get_longitude(),self.get_latitude()),'k','o',20)
self.simulation.number_cars_without_energy[self.simulation.current_step] += 1
self.teleport(action)
def rebuildPlan(self, action):
new_action = 'action'
if action == 'arrived' or action == 'resume route':
self.plan.insert(0, 'move')
elif action == 'return':
self.plan.insert(0, 'wait')
elif action == 'go to station':
#Couldnt find a station
self.plan.clear()
self.plan.append('move normaly')
def reconsider(self):
if self.died:
return True
if any(self.proposals) and not self.is_car_charging():
return True
if self.need_charge and self.charging_station == None:
print('DA: needs to charge')
return True
return False
def agentReactiveDecision(self):
self.teleport('move normaly')
#
#
# A N I M A T E
#
#
def teleport(self, action):
if action == 'stop':
print('DA: Out of Battery')
return
#Car is back on normal route
if (self.current_node == self.destination and action == 'move normaly'):
self.route_idx += 1
self.current_route = self.list_route[self.route_idx % 3]
self.destination = self.current_route[-1]
elif self.current_node == self.destination and action == 'move':
return
if action == 'go to station':
self.last_destination = self.destination
ch_node = self.charging_station.get_node_position()
route = nx.shortest_path(self.G, self.current_node, ch_node, weight='length')
self.current_route = route
self.destination = self.current_route[-1]
elif action == 'return':
route = nx.shortest_path(self.G, self.current_node, self.last_destination, weight='length')
self.current_route = route
self.destination = self.current_route[-1]
#Move the car accordingly to a route
self.lng, self.lat = get_coordinates_of_node(self.G, self.current_node)
self.set_latitude(self.lat)
self.set_longitude(self.lng)
try:
self.current_route = self.current_route[1:]
self.current_node = self.current_route[0]
except:
self.current_node = self.destination
def low_battery(self):
return self.battery <= self.battery_threshold
def update_charged(self, state):
self.is_charging = state
def is_car_charging(self):
return self.is_charging
#
# Decide Station and Change Station
#
def decide(self):
#Wort Wort Wort
worst_time_to_wait = 1
worst_distance = 1
worst_price = 1
#opt = (Time, Node, Price, CH_id)
for opt in self.options:
#Calulate worst time
if opt[0] >= worst_time_to_wait:
worst_time_to_wait = opt[0]
dist = calculate_distance(self.G, opt[1], self.current_node)
if dist >= worst_distance:
worst_distance = opt[1]
if opt[2] >= worst_price:
worst_price = opt[2]
ch_ratings = {}
for opt in self.options:
relative_time_to_wait = 1/((opt[0]+1)/worst_time_to_wait)
dist = calculate_distance(self.G, opt[1], self.current_node)
relative_distance = 1/(dist/worst_distance)
relative_price = 1/(opt[2]/worst_price)
station_rating = self.time_u * relative_time_to_wait + self.price_u * relative_price + self.distance_u * relative_distance + self.emotional_dict[opt[3]]
if self.is_possible_to_arrive(dist):
ch_ratings[opt[3]] = station_rating
elif self.is_possible_to_arrive(dist) and (dist == -1):
ch_ratings[opt[3]] = -1
else: #Not enough battery it will die
ch_ratings[opt[3]] = 0
best_rating = -1
best_id = 0
for id in ch_ratings.keys():
if ch_ratings[id] >= best_rating:
best_rating = ch_ratings[id]
best_id = id
if best_rating == -1:
return None #Impossivel de calcular rota
ch = None
for i in range(len(self.ch_list)):
if best_id == self.ch_list[i].id:
ch = self.ch_list[i]
return ch
def change_station(self):
#Wort Wort Wort
worst_time_to_wait = 1
worst_distance = 1
worst_price = 1
#opt = (Time, Node, Price, CH_id)
for opt in self.proposals:
#Calulate worst time
if opt[0] >= worst_time_to_wait:
worst_time_to_wait = opt[0]
dist = calculate_distance(self.G, opt[1], self.current_node)
if dist >= worst_distance:
worst_distance = opt[1]
if opt[2] >= worst_price:
worst_price = opt[2]
ch_ratings = {}
for opt in self.proposals:
relative_time_to_wait = 1/(opt[0]/worst_time_to_wait)
dist = calculate_distance(self.G, opt[1], self.current_node)
relative_distance = 1/(dist/worst_distance)
relative_price = 1/(opt[2]/worst_price)
station_rating = self.time_u * relative_time_to_wait + self.price_u * relative_price + self.distance_u * relative_distance + self.emotional_dict[opt[3]]
if self.is_possible_to_arrive(dist):
ch_ratings[opt[3]] = station_rating
elif self.is_possible_to_arrive(dist) and (dist != -1):
ch_ratings[opt[3]] = -1
else: #Not enough battery it will die
ch_ratings[opt[3]] = 0
best_rating = -1
best_id = 0
for id in ch_ratings.keys():
if ch_ratings[id] >= best_rating:
best_rating = ch_ratings[id]
best_id = id
if best_rating == -1:
return None #Impossivel de calcular rota
ch = self.charging_station
if best_id != ch.id():
for i in range(len(self.proposals)):
if best_id == self.proposals[i].id():
ch = self.proposals[i]
return ch
#
# Communication with Charger Handlers
#
def ask_for_time(self):
total_time = 0
for i in range(len(self.ch_list)):
total_time += self.ch_list[i].get_time_of_wait() + self.ch_list[i].calculate_time_to_charge()
return total_time
def check_options(self):
options = []
for i in range(len(self.ch_list)):
options.append(self.ch_list[i].get_option())
return options
def update_time_travel(self):
node = self.charging_station.get_node_position()
distance = calculate_distance(self.G, node, self.current_node)
time = calculate_time(distance)
self.time_of_travel = time
def receive_proposal(self, proposal):
self.proposals.append(proposal)
def is_possible_to_arrive(self, dist):
if dist == -1:
return False
else:
time = calculate_time(dist)
battery_required = self.battery_spent_per_tick * time
if self.battery >= battery_required:
return True
else:
return False
return False
#
# A U X I L I A R F U N C T I O N S
#
def get_coordinates_of_node(G, node):
coord = np.array([G.nodes[node]['x'], G.nodes[node]['y']])
return coord
def agent_has_arrived(node1, node2):
return node1 == node2
def calculate_distance(G, node1, node2):
try:
route = nx.shortest_path(G, node1, node2, weight='length')
return len(route)
except nx.exception.NodeNotFound:
return -1
except nx.exception.NetworkXNoPath:
return -1
def calculate_time(distance):
return distance #Since agent travels one node per tick