-
Notifications
You must be signed in to change notification settings - Fork 1
/
four_plus_truck_function.py
322 lines (277 loc) · 13.9 KB
/
four_plus_truck_function.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
from gurobipy import Model, GRB, quicksum
import numpy as np
import pandas as pd
# np.random.seed(42)
# Constants
B_TO_T = 10 # Bin to Truck
B_TO_B = 100 # Bin to Bin
def optimize(df, visit, distances, n_done, visitedNodes, count, NTaken, n_trucks = 1, w1 = 0.5, w2 = 0.5, m = 0, folder_Path = '', ward_name = '', t_name = '', t_no = None):
# Change to this for getting number of variables and constraints
# def optimize(df, visit, distances, n_done, visitedNodes, count, NTaken, n_trucks = 1, w1 = 0.5, w2 = 0.5, m = 0, folder_Path = '', ward_name = '', t_name = '', t_no = None, NVarRegion = 0, NConstrRegion = 0):
mdl = Model('CVRP')
# Initializations
fillPrevName = 'fill_ratio_' + str(m - 1)
fillNewName = 'fill_ratio_' + str(m)
fillpmNewName = 'fill_per_m_' + str(m)
startNode = []
objs = []
fills = []
active_arcs = []
Ns = []
Vs = []
As = []
Cs = []
Xs = []
Ys = []
Us = []
notStarted = [False] * len(n_done)
flag = [t_no, False]
(visit1, visit2, visit3) = (None, None, None)
visits = [visit1, visit2, visit3]
for i in range(n_trucks):
visits[i] = visit[i]
visits[i].Node = visits[i].Node.astype('int')
startNode.append(visits[i].iloc[-1, 0])
objs.append(0)
for k in range(len(n_done)):
Ns.append(None)
Vs.append(None)
As.append(None)
Cs.append(None)
Xs.append(None)
Ys.append(None)
Us.append(None)
active_arcs.append(None)
fills.append(None)
if n_done[k] == 0:
dist = distances.iloc[startNode[k], df.index.tolist()].tolist()
distName = 'distance_from_' + str(startNode[k]) + '_' + str(count) + '_' + str(k)
if distName not in df.columns.tolist() :
temp = df.copy()
temp[distName] = dist
df = temp.copy()
# if distName not in df.columns.tolist() : df.insert(df.shape[1], distName, dist)
fillpmNewName1 = fillpmNewName + '_' + str(startNode[k]) + '_' + str(count) + '_' + str(k)
temp = df.copy()
temp[fillpmNewName1] = B_TO_B * temp.loc[:, fillNewName] / temp.loc[:, distName]
df = temp.copy()
# df.insert(df.shape[1], fillpmNewName1, B_TO_B * df.loc[:, fillNewName] / df.loc[:, distName])
df = df.sort_values(by = fillpmNewName1, ascending = False)
fills[k] = pd.DataFrame(
{'fill' : df.loc[:, fillNewName].tolist() + [0.0]}, index = df.index.tolist() + [0]
)
N = []
for i in df.index.tolist():
if (i not in visitedNodes) and (i not in NTaken) and (i != 0) and ( df.loc[i, fillNewName] + sum(df.loc[N, fillNewName]) ) * B_TO_T <= 100 - sum(visits[k].iloc[:, 1]) * B_TO_T:
N.append(i)
NTaken.append(i)
if N == [] and startNode[k] != 0:
n_done[k] = -1
flag[1] = True
elif N == [] and startNode[k] == 0 :
notStarted[k] = True
n_done[k] = -1
if len(visitedNodes) == df.shape[0]:
n_done[k] = -1
flag[1] = True
else:
Ns[k] = N
Vs[k] = calc_V(N, m, startNode[k])
As[k] = [(p, q) for p in Vs[k] for q in Vs[k] if p != q]
Cs[k] = {(p, q) : distances.iloc[p, q] for p,q in As[k]}
Xs[k] = mdl.addVars(As[k], vtype = GRB.BINARY)
Ys[k] = mdl.addVars(Vs[k], vtype = GRB.BINARY)
Us[k] = mdl.addVars(Ns[k], vtype = GRB.CONTINUOUS)
objs[k] = quicksum(
(w1 * Xs[k][p, q] * Cs[k][(p, q)]) - (w2 * Ys[k][p] * fills[k].loc[p, 'fill'] * B_TO_T) for p,q in As[k]
)
# Optimization Function defination
mdl.modelSense = GRB.MINIMIZE
mdl.setObjective(sum(objs))
# Constraints
for k in range(len(n_done)):
if n_done[k] == 0 and not notStarted[k]:
mdl.addConstrs(
quicksum( Xs[k][i, j] for j in Vs[k] if j != i ) == 1 for i in Ns[k]
)
mdl.addConstrs(
quicksum( Xs[k][i, j] for i in Vs[k] if i != j ) == 1 for j in Ns[k]
)
mdl.addConstr(
quicksum( Ys[k][i] * fills[k].loc[i, 'fill'] * B_TO_T for i in Ns[k] ) <= ( 100 - sum(visits[k].iloc[:, 1]) * B_TO_T )
)
mdl.addConstr(
quicksum( Xs[k][startNode[k], j] for j in Ns[k]) == 1
)
mdl.addConstr(
quicksum( Xs[k][j, 0] for j in Ns[k] ) == 1
)
if startNode[k] != 0:
mdl.addConstr(
quicksum( Xs[k][0, j] for j in Ns[k]) == 0
)
if startNode[k] != 0:
mdl.addConstr(
quicksum( Xs[k][j, startNode[k]] for j in Ns[k]) == 0
)
mdl.addConstrs(
(Xs[k][i, j] == 1) >> (Us[k][i] + fills[k].loc[j, 'fill'] * B_TO_T == Us[k][j]) for i,j in As[k] if i not in [0, visits[k].iloc[-1, 0]] and j not in [0, visits[k].iloc[-1, 0]]
)
mdl.addConstrs(
Us[k][i] >= (fills[k].loc[i, 'fill'] * B_TO_T) for i in Ns[k]
)
mdl.addConstrs(Us[k][i] <= (100 - np.sum(visits[k].iloc[:, 1] * B_TO_T)) for i in Ns[k])
# Model Restrictions
mdl.Params.MIPGap = 0.1
mdl.Params.TIMELimit = 900
# Optimization
mdl.optimize()
objValue = mdl.getObjective().getValue()
# Uncomment if you need number of variables and constraints
# NVarRegion += mdl.NumVars
# NConstrRegion += mdl.NumConstrs + mdl.NumGenConstrs
for k in range(len(n_done)):
if n_done[k] == 0 and Vs == [None]:
active_arcs[k] = [()]
elif n_done[k] == 0 and not notStarted[k] and len(Vs[k]) != 2:
active_arcs[k] = [a for a in As[k] if Xs[k][a].x > 0.99]
elif n_done[k] == 0 and len(Vs[k]) == 2:
active_arcs[k] = [(startNode[k], 0)]
# mdl.reset(0)
for k in range(len(n_done)):
if n_done[k] == -1 : n_done[k] == 0
return objValue, df, active_arcs, NTaken, flag
# Change if you need number of variables and constraints
# return objValue, df, active_arcs, NTaken, flag, NVarRegion, NConstrRegion
def update_fill(data, m):
fillPrevName = 'fill_ratio_' + str(m - 1)
fillpmNewName = 'fill_per_m_' + str(m)
fillNewName = 'fill_ratio_' + str(m)
if m == 0:
fillRatio = [np.random.rand() for _ in range(data.shape[0])]
else:
fillRatio = data.loc[:, fillPrevName].tolist()
for k in range(len(fillRatio)):
if fillRatio[k] != 0.0 and np.random.rand() < 0.80:
fillRatio[k] = fillRatio[k] + np.random.uniform(0, 1 - fillRatio[k]) / 10
data.insert(data.shape[1], fillNewName, fillRatio)
data.insert(data.shape[1], fillNewName + '_' + str(m), fillRatio)
# data[fillNewName] = fillRatio
return data
def calc_V(N, m, st):
if m == 0 or st == 0:
V = N + [0]
else:
V = [st] + N + [0]
return V
def dyn_multi_opt(df, visit, distances, t_name, n_done, visitedNodes, n_trucks = 1, folder_Path = '', ward_name = '', w1 = 0.5, w2 = 0.5, m = 0, obj_value = 0, truck_fill = {}):
# Change if you need number of variables and constraints
# def dyn_multi_opt(df, visit, distances, t_name, n_done, visitedNodes, n_trucks = 1, folder_Path = '', ward_name = '', w1 = 0.5, w2 = 0.5, m = 0, obj_value = 0, NVarRegion = 0, NConstrRegion = 0):
SPEED = 13.88
# EACHRUNPOSSIBLE = 1
# EACHRUNPOSSIBLE = 3 # For 5 Truck run
truck_fill[m] = []
# Initializations
# timesToRun = int(np.ceil(len(n_done) / EACHRUNPOSSIBLE))
fillPrevName = 'fill_ratio_' + str(m - 1)
fillNewName = 'fill_ratio_' + str(m)
fillpmNewName = 'fill_per_m_' + str(m)
df = update_fill(df, m)
arcs = []
startNode = []
counts = []
NTaken = []
completedDuringRun = [None]*len(n_done)
for k in range(len(n_done)):
startNode.append(int(visit[k].iloc[-1, 0]))
for K in range(len(n_done)):
objValue, df, active_arcs, NTaken, flag = optimize(df = df, visit = [visit[K]], distances = distances, n_done = [n_done[K]], n_trucks = 1, w1 = w1, w2 = w2, m = m, visitedNodes = visitedNodes, count = K, NTaken = NTaken, folder_Path = folder_Path, ward_name = ward_name, t_name = t_name, t_no = K)
# Change to get number of variables and constraints
# objValue, df, active_arcs, NTaken, flag, NVarRegion, NConstrRegion = optimize(df = df, visit = [visit[K]], distances = distances, n_done = [n_done[K]], n_trucks = 1, w1 = w1, w2 = w2, m = m, visitedNodes = visitedNodes, count = K, NTaken = NTaken, folder_Path = folder_Path, ward_name = ward_name, t_name = t_name, t_no = K, NVarRegion = NVarRegion, NConstrRegion = NConstrRegion)
if flag[1] == True:
n_done[flag[0]] = -1
completedDuringRun[K] = active_arcs
for a in active_arcs:
arcs.append(a)
for _ in range(len([n_done[K]])):
counts.append(str(K))
obj_value += objValue
# Time Simulation
for k in range(len(n_done)):
if n_done[k] == 0 and arcs[k] != None:
p = False
TIME = 900
arc = arcs[k]
imp = 1
next_element = next(
y for x, y in arc if x == visit[k].iloc[-1 ,0]
)
unnormalize = np.max(pd.read_csv('Data/distance.csv').drop('Unnamed: 0', axis = 1).iloc[startNode[k], :])
while TIME - (unnormalize * distances.iloc[int(visit[k].iloc[-1, 0]), next_element]) / SPEED >= 0 and next_element != 0:
if next_element == 71:
p = True
print(f"\n\n\n{71 in visitedNodes}\n\n\n. It is added to {k}")
print(visit[0].head())
TIME -= unnormalize * distances.iloc[int(visit[k].iloc[-1, 0]), next_element] / SPEED
visit[k].loc[len(visit[k])] = [next_element, df.loc[next_element, fillNewName]]
df.loc[next_element, [fillNewName, fillpmNewName + '_' + str(startNode[k]) + '_' + str(k) + '_' + str(0)]] = [0.0, 0.0]
visitedNodes.add(next_element)
next_element = next(
y for x, y in arc if x == visit[k].iloc[-1 ,0]
)
imp = 0
if imp == 1 and next_element != 0:
if next_element == 71:
p = True
print(f"\n\n\n{71 in visitedNodes}\n\n\n. It is added to {k}")
# ------------------------------------
print('Forcefully entering value.')
# ------------------------------------
TIME = 0
visit[k].loc[len(visit[k])] = [next_element, df.loc[next_element, fillNewName]]
df.loc[next_element, [fillNewName, fillpmNewName + '_' + str(startNode[k]) + '_' + str(k) + '_' + str(0)]] = [0.0, 0.0]
visitedNodes.add(next_element)
next_element = next(
y for x, y in arc if x == visit[k].iloc[-1 ,0]
)
if next_element == 0 and (sum(visit[k].iloc[:, 1]) > 9.8 or len(visitedNodes) == df.shape[0]):
visit[k].loc[len(visit[k])] = [next_element, sum(visit[k].iloc[:, 1])]
# ----------------------------------------
print(f'Optimization done for truck {k}')
# ----------------------------------------
fileName = folder_Path + 'Visited ' + ward_name + '/visited_' + t_name + '_' + str(k + 1) + '_' + str(w1) + '_' + str(w2) + '.csv'
visit[k].to_csv(fileName, index = False)
n_done[k] = 1
if p:
print(71 in visitedNodes)
print(f"Arc : {arc}")
if n_done[k] == -1:
visit[k].loc[len(visit[k])] = [0, sum(visit[k].iloc[:, 1])]
# ----------------------------------------
print(f'Optimization done for truck {k}')
# ----------------------------------------
fileName : str = folder_Path + 'Visited ' + ward_name + '/visited_' + t_name + '_' + str(k + 1) + '_' + str(w1) + '_' + str(w2) + '.csv'
visit[k].to_csv(fileName, index = False)
n_done[k] = 1
for k in range(len(n_done)):
if visit[k].iloc[-1, 0] == 0:
truck_fill[m].append(visit[k].iloc[-1, 1]*B_TO_T)
else :
truck_fill[m].append(np.sum(visit[k].iloc[:, 1])*B_TO_T)
print(f"Done Status : {n_done}")
m += 1
if n_done == [1] * len(n_done):
# -----------------------------------------------
print('Done Computation')
# -----------------------------------------------
fileName = folder_Path + ward_name + ' Data/' + t_name + '_multi_' + str(w1) + '_' + str(w2) + '.csv'
df.to_csv(fileName, index = True)
return obj_value, truck_fill
# Change to get number of variables and constraints
# return obj_value, NVarRegion, NConstrRegion
# Recursive call
obj_value = dyn_multi_opt(df =df, visit = visit, visitedNodes = visitedNodes, distances = distances, t_name = t_name, n_done = n_done, w1 = w1, w2 = w2, n_trucks = n_trucks, folder_Path = folder_Path, ward_name = ward_name, obj_value = obj_value, m = m, truck_fill = truck_fill)
return obj_value, truck_fill
# Change to get number of variables and constraints
# obj_value = dyn_multi_opt(df =df, visit = visit, visitedNodes = visitedNodes, distances = distances, t_name = t_name, n_done = n_done, w1 = w1, w2 = w2, n_trucks = n_trucks, folder_Path = folder_Path, ward_name = ward_name, obj_value = obj_value, m = m, NVarRegion = NVarRegion, NConstrRegion = NConstrRegion)
# return obj_value, NVarRegion, NConstrRegion