-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdynamic_weight_finding.py
185 lines (165 loc) · 7.87 KB
/
dynamic_weight_finding.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from four_plus_truck_function import dyn_multi_opt
# from show_routes import CreateMap
# Constants
B_TO_B = 100
B_TO_T = 10
N_WARDS = 3
N_TRUCKS = 3
# Set Random Seed
np.random.seed(42)
# Import Data
data = pd.read_csv('Data/Bin Locations.csv', index_col= 'id').sort_index()
distance = pd.read_csv('Data/distance.csv').drop('Unnamed: 0', axis = 1)
for i in range(distance.shape[0]):
distance.iloc[:, i] = distance.iloc[:, i]/np.max(distance.iloc[:, i])
# # Add Fill_ratio, distance and fill per meter
# fill_ratio = [0.0] + [np.random.rand() for i in range(data.shape[0] - 1)]
# distance_from_0 = distance.iloc[:, 0]
# data['fill_ratio'] = fill_ratio
# data['distance_from_0'] = distance_from_0
# fill_p_m = [0.0] + list(B_TO_B * data.loc[1:, 'fill_ratio'] / data.loc[1:, 'distance_from_0'])
# data['fill_p_m'] = fill_p_m
# Optimization
obj_values = []
for i in range(11):
# Set Random Seed
np.random.seed(42)
w1, w2 = round(i/10, 1), round(1 - i/10, 1)
print(f"\n----------------- Processing w1 : {w1} | w2 : {w2} -----------------")
data1 = data[data.Ward == 0]
data2 = data[data.Ward == 1]
data3 = data[data.Ward == 2]
# For Ward 1
visit1, visit2, visit3 = (
pd.DataFrame({'Node': pd.Series(0, dtype='int'), 'fill_ratio': pd.Series(0, dtype='float')}),
pd.DataFrame({'Node': pd.Series(0, dtype='int'), 'fill_ratio': pd.Series(0, dtype='float')}),
pd.DataFrame({'Node': pd.Series(0, dtype='int'), 'fill_ratio': pd.Series(0, dtype='float')}),
)
visitedNodes = set()
obj_value1, _ = dyn_multi_opt(data1, [visit1, visit2, visit3], visitedNodes = visitedNodes, distances = distance, ward_name = 'Truck 1', t_name = 'truck1', folder_Path = 'Data/Dynamic Data/Weight Finding/', w1 = w1, w2 = w2, n_done = [0] * N_TRUCKS, n_trucks = N_TRUCKS)
print(obj_value1)
# For Ward 2
visit1, visit2, visit3 = (
pd.DataFrame({'Node': pd.Series(0, dtype='int'), 'fill_ratio': pd.Series(0, dtype='float')}),
pd.DataFrame({'Node': pd.Series(0, dtype='int'), 'fill_ratio': pd.Series(0, dtype='float')}),
pd.DataFrame({'Node': pd.Series(0, dtype='int'), 'fill_ratio': pd.Series(0, dtype='float')}),
)
visitedNodes = set()
obj_value2, _ = dyn_multi_opt(data2, [visit1, visit2, visit3], visitedNodes = visitedNodes, distances = distance, ward_name = 'Truck 2', t_name = 'truck1', folder_Path = 'Data/Dynamic Data/Weight Finding/', w1 = w1, w2 = w2, n_done = [0] * N_TRUCKS, n_trucks = N_TRUCKS)
# For Ward 3
visit1, visit2, visit3 = (
pd.DataFrame({'Node': pd.Series(0, dtype='int'), 'fill_ratio': pd.Series(0, dtype='float')}),
pd.DataFrame({'Node': pd.Series(0, dtype='int'), 'fill_ratio': pd.Series(0, dtype='float')}),
pd.DataFrame({'Node': pd.Series(0, dtype='int'), 'fill_ratio': pd.Series(0, dtype='float')}),
)
visitedNodes = set()
obj_value3, _ = dyn_multi_opt(data3, [visit1, visit2, visit3], visitedNodes = visitedNodes, distances = distance, ward_name = 'Truck 3', t_name = 'truck1', folder_Path = 'Data/Dynamic Data/Weight Finding/', w1 = w1, w2 = w2, n_done = [0] * N_TRUCKS, n_trucks = N_TRUCKS)
obj_values.append([obj_value1, obj_value2, obj_value3])
# obj_value = dyn_opt(data1, data2, data3, distance, folder_path = 'Data/Dynamic Data/Weight Finding/', w1 = w1, w2 = w2, visit1 = visit1, visit2 = visit2, visit3 = visit3)
# obj_values.append(np.sum(obj_value))
# Plotting mean objective values VS W1
w1s = [round(i/10, 1) for i in range(0, 11)]
Figure = figure(figsize=(15, 15))
plt.scatter(w1s, [np.mean(values) for values in obj_values], c = 'blue')
plt.scatter([round(1 - i, 1) for i in w1s], [np.mean(values) for values in obj_values], c = 'red')
plt.title('Mean of Objective Values VS Weights')
plt.xlabel('Value of Weights')
plt.ylabel('Mean Objective Value')
plt.legend(['W1', 'W2'])
plt.show()
pd.DataFrame(np.array(obj_values), index = w1s, columns = ['Ward 1', 'Ward 2', 'Ward 3']).to_csv('Data/Dynamic Data/Weight Finding/Statistics New.csv')
# W1 = w1s[np.argmin(obj_values)]
# W2 = round(1 - W1, 1)
# print(f"Best w1 value is : {W1}.")
# distance = pd.read_csv('Data/distance.csv').drop('Unnamed: 0', axis = 1)
# # Storing statistics of best case
# v11 = pd.read_csv(f'Data/Dynamic Data/Weight Finding/Visited Truck 1/visited_truck1_{W1}_{W2}.csv')
# v21 = pd.read_csv(f'Data/Dynamic Data/Weight Finding/Visited Truck 2/visited_truck2_{W1}_{W2}.csv')
# v31 = pd.read_csv(f'Data/Dynamic Data/Weight Finding/Visited Truck 3/visited_truck3_{W1}_{W2}.csv')
# v11.Node = v11.Node.astype('int')
# v21.Node = v21.Node.astype('int')
# v31.Node = v31.Node.astype('int')
# path11 = []
# path21 = []
# path31 = []
# for i in range(len(v11) - 1):
# path11.append((v11.iloc[i, 0], v11.iloc[i + 1, 0]))
# for i in range(len(v21) - 1):
# path21.append((v21.iloc[i, 0], v21.iloc[i + 1, 0]))
# for i in range(len(v31) - 1):
# path31.append((v31.iloc[i, 0], v31.iloc[i + 1, 0]))
# gar11 = v11.iloc[-1,1]*10
# gar21 = v21.iloc[-1,1]*10
# gar31 = v31.iloc[-1,1]*10
# dist11 = sum([distance.iloc[i,j] for i,j in path11])
# dist21 = sum([distance.iloc[i,j] for i,j in path21])
# dist31 = sum([distance.iloc[i,j] for i,j in path31])
# v1 = [v11]
# v2 = [v21]
# v3 = [v31]
# # Save Statistics
# print('--------------- SAVING STATISTICS ----------------------\n')
# stats = pd.DataFrame(
# {
# 'Fill Ward 1 (in %)' : [
# round(gar11, 4),
# '-'],
# 'Garbage Fill Ward 1 (in Litres)' : [
# round(gar11/10 * B_TO_B, 4),
# '-'],
# 'Distance Travelled Ward 1 (in m)' : [
# round(dist11, 4),
# '-'],
# 'Garbage per Meter Ward 1 (in KG/m)' : [
# round(gar11/dist11, 4),
# '-'],
# 'Percentage of Bins covered Ward 1 (in %)' : [
# round( 100 * (v11.shape[0] - 2)/ data[data.Ward == 0].shape[0], 4),
# round( 100 * np.sum([i.shape[0] - 2 for i in v1])/ data[data.Ward == 0].shape[0], 4)],
# 'Fill Ward 2 (in %)' : [
# round(gar21, 4),
# '-'],
# 'Garbage Fill Ward 2 (in Litres)' : [
# round(gar21/10 * B_TO_B, 4),
# '-'],
# 'Distance Travelled Ward 2 (in m)' : [
# round(dist21, 4),
# '-'],
# 'Garbage per Meter Ward 2 (in KG/m)' : [
# round(gar21/dist21, 4),
# '-'],
# 'Percentage of Bins covered Ward 2 (in %)' : [
# round( 100 * (v21.shape[0] - 2)/ data[data.Ward == 1].shape[0], 4),
# round( 100 * np.sum([i.shape[0] - 2 for i in v2])/ data[data.Ward == 1].shape[0], 4)],
# 'Fill Ward 3 (in %)' : [
# round(gar31, 4),
# '-'],
# 'Garbage Fill Ward 3 (in Litres)' : [
# round(gar31/10 * B_TO_B, 4),
# '-'],
# 'Distance Travelled Ward 3 (in m)' : [
# round(dist31, 4),
# '-'],
# 'Garbage per Meter Ward 3 (in KG/m)' : [
# round(gar31/dist31, 4),
# '-'],
# 'Percentage of Bins covered Ward 3 (in %)' : [
# round( 100 * (v31.shape[0] - 2)/ data[data.Ward == 2].shape[0], 4),
# round( 100 * np.sum([i.shape[0] - 2 for i in v3])/ data[data.Ward == 2].shape[0], 4)],
# }, index=['Truck 1', 'Total Percentage'])
# stats.to_csv('Data/Dynamic Data/Weight Finding/Statistics.csv')
# print('--------------- GENERATING MAP ----------------------')
# # Plotting routes
# map = CreateMap()
# map.createRoutes('Data/Dynamic Data/Weight Finding/', N_WARDS, N_TRUCKS, W1, W2)
# map.createLatLong('Data/Bin Locations.csv', N_WARDS)
# map.createRoutesDict(N_WARDS)
# map.addRoutesToMap(N_WARDS, N_TRUCKS)
# map.addDepot()
# map.addNodes('Data/Bin Locations.csv')
# map.saveMap('Data/Dynamic Data/Weight Finding/')
# map.displayMap('Data/Dynamic Data/Weight Finding/')