-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRebalancingManager.py
260 lines (191 loc) · 9.6 KB
/
RebalancingManager.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
import os
import logging
from numpy.lib.function_base import select
import pandas as pd
import numpy as np
from scipy.optimize import linprog
from .Location import Location
class RebalancingManager:
# makes recharging decisions
def __init__(self, env, config, graph, ui):
self.env = env
self.config = config
self.graph = graph
self.ui = ui
self.update_every = 15 # [min]
self.predict_ahead = 15 # [min]
self.predict_window = 45 # [min]
self.update_every = config["REBALANCING_EVERY"] # [min]
self.predict_ahead = config["REBALANCING_AHEAD"] # [min]
self.predict_window = config["REBALANCING_WINDOW"] # [min]
self.battery_min_level = config["BATTERY_MIN_LEVEL"] # [%]
print("Loading Rebalancing")
path = os.path.join("data", "demand_grid.csv")
self.demand = pd.read_csv(path, index_col=0)
self.grid = self.demand.copy()
self.grid = self.demand.drop_duplicates(["group_lon", "group_lat"]).reset_index()
self.grid = self.grid.drop(columns=["ts", "unix", "lon", "lat"])
self.n = len(self.grid)
# self.demand = self.demand.reset_index()
# self.demand.set_index(["group_lon", "group_lat"], drop=False)
self.idx = pd.MultiIndex.from_arrays([self.grid.group_lon, self.grid.group_lat])
self.routing = Routing(self.grid, self.graph)
print("Done Rebalancing")
def set_bikes(self, bikes):
self.bikes = bikes
def start(self):
self.env.process(self.predictive_demand())
def predictive_demand(self):
print("Rebalancing ongoing")
while True:
logging.info("[%.2f] Demand check" % (self.env.now))
window_start = self.env.now + self.predict_ahead * 60
window_stop = window_start + self.predict_window * 60
# data = pd.DataFrame({
# "demand": np.zeros(self.n),
# "bikes": np.zeros(self.n)
# }, index = self.idx)
bikes_vector = np.zeros(self.n, dtype=np.int)
subset = self.demand.loc[window_start:window_stop]
subset = pd.concat([subset, self.grid], axis=0, join="outer", ignore_index=True, sort=False)
demand_vector = subset.groupby(["group_lon", "group_lat"]).size().values - 1
# subset = subset.set_index(["group_lon", "group_lat"])
# subset = subset.merge(data, how="right", left_index=True, right_index=True)
# demand_vector = subset.groupby(level=[0,1]).size().values-1
# self.bikes_location = np.array([[bike.location.lon, bike.location.lat, bike.location.node] for bike in self.bikes])
bikes_id = []
bikes_cell = []
for bike in self.bikes:
if not bike.busy and bike.battery.level > self.battery_min_level:
lon = bike.location.lon
lat = bike.location.lat
cond = (lon > self.grid.lon_lb.values) & (lon < self.grid.lon_ub.values) & (lat > self.grid.lat_lb.values) & (lat < self.grid.lat_ub.values)
row = self.grid.index[cond].tolist()
if len(row) > 0:
# bike.grid_id = row[0]
bikes_id.append(bike.id)
bikes_cell.append(row[0])
bikes_vector[row[0]] += 1
bikes_id = np.array(bikes_id)
bikes_cell = np.array(bikes_cell)
s = self.routing.optimize(demand_vector, bikes_vector)
cell_from_id, cell_to_id = np.where(s > 0)
num_bikes = s[s > 0]
num_rebalances = len(num_bikes)
# print(self.env.now, "numbikes rebalancing", num_bikes, np.sum(num_bikes))
# logging.info(
# "[%.2f] Rebalancing info: " % (self.env.now) + "\n" +
# "demand_vector: " + str(demand_vector) + "\n" +
# "bikes_vector: " + str(bikes_vector) + "\n" +
# "Bikes ID: " + str(bikes_id) + "\n" +
# "Bikes Cell: " + str(bikes_cell) + "\n" +
# "Cell from: " + str(cell_from_id) + "\n" +
# "Cell to: " + str(cell_to_id) + "\n" +
# "Num bikes: " + str(num_bikes) + "\n" +
# "Num rebalances: " + str(num_rebalances)
# )
chosen = -np.ones_like(bikes_id, dtype=int)
for i in range(num_rebalances):
allowed = bikes_id[chosen == -1][bikes_cell[chosen == -1] == cell_from_id[i]]
if len(allowed) > num_bikes[i]:
selected = np.random.choice(allowed, num_bikes[i], replace=False)
else:
selected = allowed
for j in selected:
chosen[bikes_id == j] = i
for i in range(len(bikes_id)):
if chosen[i] != -1:
bike_id = bikes_id[i]
cell_to = self.grid.iloc[cell_to_id[chosen[i]]]
lon = np.random.uniform(cell_to.lon_lb, cell_to.lon_ub)
lat = np.random.uniform(cell_to.lat_lb, cell_to.lat_ub)
# lon = (cell_to.lon_lb + cell_to.lon_ub)/2
# lat = (cell_to.lat_lb + cell_to.lat_ub)/2
node = self.graph.network.get_node_ids([lon], [lat])[0]
destination = Location(lon, lat, node)
if not self.bikes[bike_id].busy:
logging.info("[%.2f] Rebalancing bike %d" % (self.env.now, bike_id))
self.env.process(self.ui.autonomous_drive(bike_id, destination, user_id=-1, magic=False, rebalancing=True, liberate=True, charge=True))
# WITH ROUTING OPTIMIZATION
# for i in range(num_rebalances):
# selected = []
# allowed_bikes = bikes_id[bikes_cell == cell_from_id[i]]
# allowed_bikes = np.unique(allowed_bikes)
# # WITHOUT ROUTING OPTIMIZATION
# # allowed_bikes = bikes_id
# if len(allowed_bikes) > num_bikes[i]:
# selected = np.random.choice(allowed_bikes, num_bikes[i], replace=False)
# else:
# selected = allowed_bikes
# for j in selected:
# cell_to = self.grid.iloc[cell_to_id[i]]
# lon = np.random.uniform(cell_to.lon_lb, cell_to.lon_ub)
# lat = np.random.uniform(cell_to.lat_lb, cell_to.lat_ub)
# lon = (cell_to.lon_lb + cell_to.lon_ub)/2
# lat = (cell_to.lat_lb + cell_to.lat_ub)/2
# node = self.graph.network.get_node_ids([lon], [lat])[0]
# destination = Location(lon, lat, node)
# if not self.bikes[j].busy:
# logging.info("[%.2f] Rebalancing bike %d" % (self.env.now, j))
# self.env.process(self.ui.autonomous_drive(j, destination, user_id=-1, magic=False, rebalancing=True, liberate=True))
# self.env.process(self.ui.bike_charge(j))
yield self.env.timeout(self.update_every * 60) # check every update_every mins
class Routing:
def __init__(self, grid, graph):
self.grid = grid
self.graph = graph
self.n = len(grid)
print("Loading Routing")
self.grid["lon"] = (self.grid.lon_lb + self.grid.lon_ub)/2
self.grid["lat"] = (self.grid.lat_lb + self.grid.lat_ub)/2
self.grid["node"] = self.graph.network.get_node_ids(self.grid.lon, self.grid.lat)
self.grid["location"] = None
self.grid["location"] = self.grid.apply(lambda x: Location(x.lon, x.lat, x.node), axis=1)
# for i in range(len(self.grid)):
# self.grid["location"][i] = Location(self.grid.lon[i], self.grid.lat[i], self.grid.node[i])
self.dist = self.compute_distances()
self.slack = np.ones(self.n)*1e6
self.cost = np.concatenate([self.dist.flatten(), self.slack])
self.A = self.get_A()
print("Done Routing")
def get_A(self):
n = self.n
A = np.zeros((2*n, n**2 + n))
for i in range(n):
for j in range(n):
A[i, i*n + j] = 1
A[n+i, j*n + i] = -1
A[n+i, n**2 + i] = -1
return A
def get_b(self, bikes, demand):
n = self.n
b = np.zeros((2*n))
for i in range(n):
b[i] = bikes[i]
b[n+i] = -demand[i]
return b
def compute_distances(self):
n = self.n
dist = np.empty((n, n))
for i in range(n):
for j in range(n):
if i == j:
dist[i,j] = 0
else:
a = self.grid.location[i]
b = self.grid.location[j]
# dist[i,j] = np.sqrt((a.lon-b.lon)**2 + (a.lat-b.lat)**2)
dist[i,j] = self.graph.shortest_path_length(a, b)/1000
return dist
def optimize(self, demand, bikes):
n = self.n
cost = self.cost
A = self.A
b = self.get_b(bikes, demand)
res = linprog(c=cost, A_ub = A, b_ub = b, method='highs')
s = np.round(res.x[:n**2]).reshape((self.n, self.n))
np.fill_diagonal(s, 0)
# print(np.sum(demand), np.sum(bikes), np.sum(s[s>0]))
# s[s>0]
# np.where(s>0)
return s.astype(int)