-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_env.py
452 lines (393 loc) · 18.5 KB
/
map_env.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
import random
import pdb
import time
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import tkinter as tk
from utils import create_map, label_speed, graph_from_gdfs
from gym import spaces
from random import randint
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
DAYS = 2
gdf_edges, gdf_nodes, G, gdf_cs, s_df, result = create_map(DAYS)
NUMBER_OF_PASSENGERS = 10
MAX_NUMBER_OF_CONNECTIONS = int((max(sorted((d for _, d in G.degree()), reverse= True)))/2)
NUMBER_OF_CS = len(gdf_cs)
NUMBER_OF_ROADS = G.number_of_edges()
NUMBER_OF_EV = 1
MAX_SOC = 54
DISCHARGE_RATE = 0.2
CHARGING_RATE = 4.5
MAX_STEPS = 288
TIME_STEP_DURATION = int(60 / (MAX_STEPS / 24))
ALPHA = 18 * 2 # Check later
BETA = 6
OUT_OFF_BATTERY_PENALTY = MAX_STEPS - (MAX_SOC / DISCHARGE_RATE)
class Env(tk.Tk):
def __init__(self, normal_render = False, show = False):
super().__init__()
''' Define the action size and the observation space'''
self.action_size = NUMBER_OF_CS + MAX_NUMBER_OF_CONNECTIONS + NUMBER_OF_PASSENGERS
self.observation_space = spaces.Box(
low = np.array([0.] * (NUMBER_OF_ROADS + (NUMBER_OF_EV * 2) + (NUMBER_OF_PASSENGERS * 3) + NUMBER_OF_CS)),
high = np.array([6.] * (NUMBER_OF_ROADS + (NUMBER_OF_EV * 2) + (NUMBER_OF_PASSENGERS * 3) + NUMBER_OF_CS), dtype=np.float32),
dtype = np.float32
)
self.graph = G
self.intersections = gdf_nodes
self.cs = gdf_cs
self.cs_nodes = self.cs['NODE_ID'].values
self.roads = gdf_edges
self.aux_road_result = result.drop(columns=result.geometry.name)
self.mask_101 = (self.aux_road_result['ROAD_RANK'] == '101').values
self.mask_103 = ((self.aux_road_result['ROAD_RANK'] == '103') | (self.aux_road_result['ROAD_RANK'] == '106')).values
self.mask_107 = (self.aux_road_result['ROAD_RANK'] == '107').values
self.color_mapper = {'green': (0.0, 0.8, 0.0, 1.0), 'yellow': (0.8, 0.8, 0.0, 1.0), 'red': (0.8, 0.0, 0.0, 1.0)}
self.day = 1
self.max_days = DAYS
self.traffic = s_df[self.day].groupby('ts')
self.traffic_time = iter(list(self.traffic.groups.keys()))
self.daily_graph = {}
self.daily_roads = {}
for d in range(1, self.max_days+1):
self.daily_graph[d] = {}
self.daily_roads[d] = {}
self.title('Jeonju')
self.create_plot()
self.canvas = self._build_canvas()
self.normal_render = normal_render
self.show = show
self.counter = 0
self.aux_counter = 0
self.traffic_counter = 0
self.rewards = []
self.passenger = False
self.user_counter = 0
self.waiting_time = 0
self.wt_rectifier = 0
self.cs_waiting_time = 0
self.charging_counter = 0
self.info = {}
self.info['waiting_time'] = []
self.info['cs'] = {}
self.set_cs()
self.set_users()
self.canvas.get_tk_widget().pack(side='top',fill='both', expand=True)
def color_roads(self):
return [self.color_mapper[v] for v in self.roads['TC'].values]
def create_plot(self):
self.fig, self.ax = plt.subplots(figsize=(10,7))
self.ax.get_xaxis().set_visible(False)
self.ax.get_yaxis().set_visible(False)
self.ax = self.roads['geometry'].plot(ax=self.ax, color=self.color_roads(), lw=1, alpha=None, zorder=-1)
self.roads = self.roads.drop(columns=self.roads.geometry.name)
# self.ax.scatter(self.intersections['geometry'].x, self.intersections['geometry'].y, s = 2, zorder=-1, color='k')
self.intersections = self.intersections.drop(columns=self.intersections.geometry.name)
# self.ax.set(xlim = (127.05693, 127.19335), ylim = (35.81962, 35.88782))
self.ax.set(xlim = (127.09, 127.15), ylim = (35.83, 35.86))
self.fig.tight_layout()
self.ax.scatter(self.cs['geometry'].x, self.cs['geometry'].y, color='xkcd:brown')
def set_cs(self):
self.cs_info = self.cs.T.to_dict()
for k in self.cs_info.keys():
self.cs_info[k]['waiting_time'] = (random.randrange(0, 80, TIME_STEP_DURATION))
self.info['cs'][k] = 0
def update_cs(self):
for k,v in self.cs_info.items():
cs_state = v['waiting_time']
if cs_state > 5:
self.cs_info[k]['waiting_time'] = cs_state - TIME_STEP_DURATION
elif cs_state <= 0:
self.cs_info[k]['waiting_time'] = random.randrange(0,80,TIME_STEP_DURATION)
def random_location(self):
picked_node = random.choice(self.intersections.index.values)
while (picked_node in self.cs_nodes) or (picked_node in self.taken_nodes):
picked_node = random.choice(self.intersections.index.values)
loc = [self.intersections.at[picked_node, 'x'], self.intersections.at[picked_node, 'y']]
self.taken_nodes.append(picked_node)
return loc, picked_node
def _build_canvas(self):
# self.attributes('-fullscreen', True)
canvas = FigureCanvasTkAgg(self.fig, self)
self.ev_info = {}
self.taken_nodes = []
for i in range(NUMBER_OF_EV):
self.ev_info[i] = {}
location, node = self.random_location()
self.ev_info[i]['location'] = location
self.ev_info[i]['NODE_ID'] = node
self.ev_info[i]['SOC'] = MAX_SOC
self.ev_info[i]['on_duty'] = False
self.ev_info[i]['stand_by'] = True
self.ev_info[i]['scatter'] = self.ax.scatter(self.ev_info[i]['location'][0], self.ev_info[i]['location'][1], color='b', marker='s')
return canvas
def set_user_info(self, i):
source, source_node = self.random_location()
self.user_info[i]['source'] = source
self.user_info[i]['NODE_ID_source'] = source_node
self.user_info[i]['scatter_source'] = self.ax.scatter(self.user_info[i]['source'][0], self.user_info[i]['source'][1], color='m', marker='H')
destination, dest_node = self.random_location()
self.user_info[i]['destination'] = destination
self.user_info[i]['NODE_ID_destination'] = dest_node
self.user_info[i]['scatter_destination'] = self.ax.scatter(self.user_info[i]['destination'][0], self.user_info[i]['destination'][1], color='m', marker='X')
self.user_info[i]['waiting_time'] = 0
# self.info['waiting_time'][i] = 0
def set_users(self):
self.user_info = {}
for i in range(NUMBER_OF_PASSENGERS):
self.user_info[i] = {}
self.set_user_info(i)
# @profile
def update_roads(self, groups, date):
if self.traffic_counter in self.daily_roads[self.day].keys():
self.roads = self.daily_roads[self.day][self.traffic_counter]
else:
s = groups.get_group(date).set_index('LINK_ID')['speed']
self.aux_road_result['speed'] = self.aux_road_result['LINK_ID'].map(s).fillna(self.aux_road_result['speed'])
self.roads = label_speed(self.aux_road_result, self.mask_101, self.mask_103, self.mask_107)
self.daily_roads[self.day][self.traffic_counter] = self.roads
# @profile
def update_graph(self):
if self.counter >= self.traffic_counter + 1:
time = next(self.traffic_time)
self.update_roads(self.traffic, time)
if self.traffic_counter in self.daily_graph[self.day].keys():
self.graph = self.daily_graph[self.day][self.traffic_counter]
else:
roads = self.roads.iloc[:, [2,5]]
self.graph = graph_from_gdfs(roads)
self.daily_graph[self.day][self.traffic_counter] = self.graph
if self.show:
self.ax.collections[0].set_color(self.color_roads())
self.traffic_counter += 1
def discharge_ev(self, index):
if self.ev_info[index] != 0.4:
self.ev_info[index]['SOC'] = (int(self.ev_info[index]['SOC']*10) - int((DISCHARGE_RATE)*10))/10
else:
self.ev_info[index]['SOC'] = 0
def move(self, index, target, action):
s = target['NODE_ID']
sample = self.graph.neighbors(s)
s_ = next((x for i,x in enumerate(sample) if i==action), None)
if s_ is not None:
self.ev_info[index]['NODE_ID'] = s_
self.ev_info[index]['location'] = [self.intersections.at[s_, 'x'], self.intersections.at[s_, 'y']]
self.ev_info[index]['scatter'].set_offsets(
np.c_[self.ev_info[index]['location'][0], self.ev_info[index]['location'][1]])
self.discharge_ev(index)
else:
s_ = s
return s_
def get_state(self):
states = []
states.append(self.ev_info[0]['SOC'])
states.append(int(self.ev_info[0]['NODE_ID']))
for v in self.user_info.values():
states.append(int(v['NODE_ID_source']))
states.append(int(v['NODE_ID_destination']))
states.append(v['waiting_time'])
for v in self.cs_info.values():
states.append(v['waiting_time'])
for edge in self.graph.edges():
for v in self.graph.get_edge_data(edge[0], edge[1]).values():
states.append(v['state'])
return np.array(states)
# @profile
def check_done(self):
self.counter += 1
for k in self.user_info.keys():
self.user_info[k]['waiting_time'] = self.counter if self.aux_counter == 0 else self.counter - self.aux_counter
self.update_cs()
self.update_graph()
def reset_rewards(self, index):
self.rewards.clear()
for k in self.user_info.keys():
self.user_info[k]['waiting_time'] = 0
self.aux_counter = self.counter
self.wt_rectifier = 0
self.passenger = False
self.ev_info[index]['on_duty'] = False
self.taken_nodes = [self.ev_info[index]['NODE_ID']]
for k in self.user_info.keys():
source, source_node = self.random_location()
self.user_info[k]['source'] = source
self.user_info[k]['NODE_ID_source'] = source_node
self.user_info[k]['scatter_source'].set_offsets(
np.c_[self.user_info[k]['source'][0], self.user_info[k]['source'][1]])
destination, dest_node = self.random_location()
self.user_info[k]['destination'] = destination
self.user_info[k]['NODE_ID_destination'] = dest_node
self.user_info[k]['scatter_destination'].set_offsets(
np.c_[self.user_info[k]['destination'][0], self.user_info[k]['destination'][1]])
self.user_info[k]['waiting_time'] = 0
# @profile
def check_if_reward(self, index, next_node):
check_list = {}
check_list['if_done'] = False
check_list['if_goal'] = False
rewards = 0
found = False
if not self.passenger:
for k in self.user_info.keys():
self.user_info[k]['waiting_time'] = self.counter if self.aux_counter == 0 else self.counter - self.aux_counter
# pdb.set_trace()
if self.ev_info[index]['on_duty'] and self.user_info[self.picked_user]['NODE_ID_source'] == next_node:
self.passenger = True
self.user_info[self.picked_user]['waiting_time'] -= self.wt_rectifier
self.info['waiting_time'].append(self.user_info[self.picked_user]['waiting_time']*TIME_STEP_DURATION)
if self.passenger and self.user_info[self.picked_user]['NODE_ID_destination'] == next_node:
self.user_counter += 1
rewards += ((ALPHA * self.user_counter* 4/5) if self.pre_pickup == 0 else
(ALPHA * self.user_counter* 4/5) / self.pre_pickup)
check_list['if_goal'] = True
self.passenger = False
self.ev_info[index]['on_duty'] = False
if ((not self.passenger) and (not self.ev_info[index]['stand_by']) and
self.cs_info[self.picked_cs]['NODE_ID'] == next_node):
self.cs_waiting_time = self.cs_info[self.picked_cs]['waiting_time']
for _ in range(int(self.cs_waiting_time/TIME_STEP_DURATION)):
if self.counter == MAX_STEPS:
found = True
break
self.check_done()
if not found:
self.charging_counter += 1
self.info['cs'][self.picked_cs] += 1
charging_time = int(np.ceil((MAX_SOC - self.ev_info[index]['SOC'])/CHARGING_RATE))
self.cs_info[self.picked_cs]['waiting_time'] = charging_time * TIME_STEP_DURATION
for _ in range(charging_time):
if self.counter == MAX_STEPS:
found = True
break
self.check_done()
self.ev_info[index]['SOC'] = min(self.ev_info[index]['SOC'] + CHARGING_RATE, MAX_SOC)
self.ev_info[index]['stand_by'] = True
# pdb.set_trace()
if self.ev_info[index]['SOC'] < DISCHARGE_RATE or self.counter == MAX_STEPS:
check_list['if_done'] = True
rewards -= (MAX_STEPS - self.counter) * 4
if check_list['if_goal']:
self.reset_rewards(index)
elif not self.ev_info[index]['on_duty'] and self.user_info[0]['waiting_time'] >= BETA:
rewards -= self.user_info[0]['waiting_time'] * 3
self.info['waiting_time'].append(self.user_info[0]['waiting_time']*TIME_STEP_DURATION)
self.reset_rewards(index)
check_list['rewards'] = rewards
# pdb.set_trace()
return check_list
# @profile
def move_aux(self, index, next_node):
self.update_cs()
self.update_graph()
if self.show:
self.render()
self.ev_info[index]['NODE_ID'] = next_node
# node = self.intersections.loc[next_node]
# self.ev_info[index]['location'] = [node['geometry'].x, node['geometry'].y]
# self.ev_info[index]['location'] = [node['x'], node['y']]
self.ev_info[index]['location'] = [self.intersections.at[next_node, 'x'], self.intersections.at[next_node, 'y']]
self.ev_info[index]['scatter'].set_offsets(
np.c_[self.ev_info[index]['location'][0], self.ev_info[index]['location'][1]])
self.discharge_ev(index)
# pdb.set_trace()
return self.check_if_reward(index, next_node)
# @profile
def move_path(self, index, target, action):
car_node = target['NODE_ID']
pass_action = MAX_NUMBER_OF_CONNECTIONS
cs_action = pass_action + NUMBER_OF_PASSENGERS
# print(f'{action=}, {self.counter=}')
if action >= pass_action and action <= (cs_action - 1):
self.picked_user = action % pass_action
pass_node = self.user_info[self.picked_user]['NODE_ID_source']
dest_node = self.user_info[self.picked_user]['NODE_ID_destination']
l_car_to_pass, r_car_to_pass = nx.single_source_dijkstra(self.graph, car_node, pass_node, weight='state')
self.pre_pickup = len(r_car_to_pass) - 1
l_car_to_dest, r_car_to_dest = nx.single_source_dijkstra(self.graph, pass_node, dest_node, weight='state')
self.wt_rectifier = round(l_car_to_dest/TIME_STEP_DURATION)
if len(r_car_to_pass) > 1:
r_car_to_pass = r_car_to_pass[1:]
final_route = np.concatenate([r_car_to_pass, r_car_to_dest[1:]])
final_length = round(l_car_to_pass/TIME_STEP_DURATION) + self.wt_rectifier
self.ev_info[index]['on_duty'] = True
elif action >= cs_action and action <= (cs_action + NUMBER_OF_CS - 1):
self.ev_info[index]['stand_by'] = False
self.picked_cs = action % cs_action
cs_node = self.cs_info[self.picked_cs]['NODE_ID']
l_car_to_cs, r_car_to_cs = nx.single_source_dijkstra(self.graph, car_node, cs_node, weight='state')
if len(r_car_to_cs) == 1:
final_route = r_car_to_cs
else:
final_route = r_car_to_cs[1:]
final_length = round(l_car_to_cs / TIME_STEP_DURATION)
# pdb.set_trace()
if self.counter + final_length >= MAX_STEPS:
self.wt_rectifier = 0
for i in range(final_length):
self.counter += 1
check = self.move_aux(index, final_route[i])
if target['SOC'] < DISCHARGE_RATE or self.counter == MAX_STEPS:
break
else:
self.counter += final_length
for i in final_route:
check = self.move_aux(index, i)
if target['SOC'] < DISCHARGE_RATE or self.counter == MAX_STEPS:
break
return check
# @profile
def step(self, action):
# print(self.ev_info)
# pdb.set_trace()
if action < MAX_NUMBER_OF_CONNECTIONS:
self.update_cs()
self.counter += 1
self.update_graph()
if self.show:
self.render()
for k,v in self.ev_info.items():
next_node = self.move(k, v, action)
check = self.check_if_reward(k, next_node)
else:
for k,v in self.ev_info.items():
check = self.move_path(k, v, action)
done = check['if_done']
if done:
self.info['served_users'] = self.user_counter
self.info['count'] = self.counter
self.info['to_charge'] = self.charging_counter
# print(self.ev_info)
reward = check['rewards']
s_ = self.get_state()
# pdb.set_trace()
return s_, reward, done, self.info
def reset(self):
if self.show:
self.render()
if self.day % self.max_days != 0:
self.day += 1
else:
self.day = 1
self.traffic = s_df[self.day].groupby('ts')
self.traffic_time = iter(list(self.traffic.groups.keys()))
self.ev_info[0]['SOC'] = MAX_SOC
self.counter = 0
self.traffic_counter = 0
self.user_counter = 0
self.charging_counter = 0
self.info.clear()
self.info['waiting_time'] = []
self.info['cs'] = {}
self.ev_info[0]['stand_by'] = True
for k in self.cs_info.keys():
self.cs_info[k]['waiting_time'] = (random.randrange(0,80,TIME_STEP_DURATION))
self.info['cs'][k] = 0
self.reset_rewards(0)
return self.get_state()
def render(self):
if self.normal_render:
time.sleep(1)
pass
self.update()
self.canvas.draw()