-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiments.py
More file actions
224 lines (196 loc) · 9.14 KB
/
experiments.py
File metadata and controls
224 lines (196 loc) · 9.14 KB
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
import networkx as nx
import sys
from src.lrmp import LinearRelaxedMasterProblem
from src.hc_vns import HillClimbingVariableNeighborhoodSearch
from src.fstsp import MultiAgentFlyingSidekickTSP
from src.lp import LinearProgramming
from problem import small_instance, multiagent_instance_on_manhattan, multiagent_instance_on_cambridge, manhattan
import numpy as np
import time
from tqdm import tqdm
from utils import haversine
def test_small_instance(num, size):
graph, _depots, _cities, distance = small_instance(num, 20, 2, size)
costs = {'lrmp': [], 'hc': [], 'stsp': [], 'lp': []}
times = {'lrmp': [], 'hc': [], 'stsp': [], 'lp': []}
for i in tqdm(range(num)):
depots, cities = _depots[i], _cities[i]
model = LinearRelaxedMasterProblem(graph, depots, cities, distance, 2)
start = time.time()
solution, cost = model.solve()
costs['lrmp'].append(cost)
times['lrmp'].append(time.time() - start)
print(f'LRMP gives solution with cost {sum(costs["lrmp"]) / num} in {sum(times["lrmp"]) / num}s')
for i in tqdm(range(num)):
depots, cities = _depots[i], _cities[i]
model = HillClimbingVariableNeighborhoodSearch(graph, depots, cities, distance, 2, rounds=1000)
start = time.time()
solution, cost = model.solve()
costs['hc'].append(cost)
times['hc'].append(time.time() - start)
print(f'Hill Climbing gives solution with cost {sum(costs["hc"]) / num} in {sum(times["hc"]) / num}s')
for i in tqdm(range(num)):
depots, cities = _depots[i], _cities[i]
model = MultiAgentFlyingSidekickTSP(graph, depots, cities, distance, 2)
start = time.time()
solution, cost = model.solve()
costs['stsp'].append(cost)
times['stsp'].append(time.time() - start)
print(f'Our algorithm gives solution with cost {sum(costs["stsp"]) / num} in {sum(times["stsp"]) / num}s')
for i in tqdm(range(num)):
depots, cities = _depots[i], _cities[i]
model = LinearProgramming(graph, depots, cities, distance, 2)
start = time.time()
solution, cost = model.solve()
costs['lp'].append(cost)
times['lp'].append(time.time() - start)
print(f'LP gives solution with cost {sum(costs["lp"]) / num} in {sum(times["lp"]) / num}s')
def test_manhattan(num, size):
graph, depots, cities, distance = multiagent_instance_on_manhattan(num, 5, size)
costs = {'hc': [], 'stsp': [], 'lp': []}
times = {'hc': [], 'stsp': [], 'lp': []}
for i in tqdm(range(num)):
depot, city = depots[i], cities[i]
model = HillClimbingVariableNeighborhoodSearch(graph, depot, city, distance, 3, rounds=5000)
start = time.time()
solution, cost = model.solve()
times['hc'].append(time.time() - start)
costs['hc'].append(cost)
print(f'Hill Climbing gives solution with cost {sum(costs["hc"]) / num} in {sum(times["hc"])/ num}')
for i in tqdm(range(num)):
depot, city = depots[i], cities[i]
model = MultiAgentFlyingSidekickTSP(graph, depot, city, distance, 3, theta=(0.5, 0.5))
start = time.time()
solution, cost = model.solve()
times['stsp'].append(time.time() - start)
costs['stsp'].append(cost)
print(f'Our algorithm gives solution with cost {sum(costs["stsp"]) / num} in {sum(times["stsp"]) / num}s')
def test_cambridge(num, size):
graph, depots, cities, distance = multiagent_instance_on_cambridge(num, 10, size)
costs = {'hc': [], 'stsp': [], 'lp': []}
times = {'hc': [], 'stsp': [], 'lp': []}
for i in tqdm(range(num)):
depot, city = depots[i], cities[i]
model = HillClimbingVariableNeighborhoodSearch(graph, depot, city, distance, 3, rounds=5000)
start = time.time()
solution, cost = model.solve()
times['hc'].append(time.time() - start)
costs['hc'].append(cost)
print(f'Hill Climbing gives solution with cost {sum(costs["hc"]) / num} in {sum(times["hc"]) / num}')
for i in tqdm(range(num)):
depot, city = depots[i], cities[i]
model = MultiAgentFlyingSidekickTSP(graph, depot, city, distance, 3, theta=(0.5, 0.5))
start = time.time()
solution, cost = model.solve()
times['stsp'].append(time.time() - start)
costs['stsp'].append(cost)
print(f'Our algorithm gives solution with cost {sum(costs["stsp"]) / num} in {sum(times["stsp"]) / num}s')
def ablation_r():
print('Studying the effect of radius limit')
graph, depots, cities, distance = multiagent_instance_on_manhattan(20, 5, 100)
costs, times = [], []
for r in range(5, 16, 2):
costs.append([])
times.append([])
for i in tqdm(range(100)):
model = MultiAgentFlyingSidekickTSP(graph, depots[i], cities[i], distance, 3, limit=r/10, theta=(0.5, 0.5))
start = time.time()
_, cost = model.solve()
times[-1].append(time.time() - start)
costs[-1].append(cost)
np.save("r-time.npy", np.array(times))
np.save("r-cost.npy", np.array(costs))
def ablation_speed():
print('Studying the effect of speed')
graph, depots, cities, distance = multiagent_instance_on_manhattan(20, 5, 100)
costs, times = [], []
for speed in [i / 30 for i in range(10, 120, 20)]:
costs.append([])
times.append(0)
for i in tqdm(range(100)):
model = MultiAgentFlyingSidekickTSP(graph, depots[i], cities[i], distance, 3, speed=speed)
start = time.time()
_, cost = model.solve()
times[-1] += time.time() - start
costs[-1].append(cost)
np.save("speed-time.npy", np.array(times))
np.save("speed-cost.npy", np.array(costs))
def ablation_k():
print('studying the effect of drone number')
for size in range(50, 160, 20):
graph, depots, cities, distance = multiagent_instance_on_manhattan(10, 5, size)
costs, times = [], []
for i in tqdm(range(100)):
model = MultiAgentFlyingSidekickTSP(graph, depots[i], cities[i], distance, 0)
cost = model.solve_multiple_drones()
costs.append(cost.copy())
print(f'size {size} gives {np.mean(costs, axis=0)}')
def scale_cities():
print('studying the scalability of fix depot case')
graph = manhattan()
distance = {'truck': dict(nx.all_pairs_dijkstra_path_length(graph, weight='weight')),
'drone': {i: {j: haversine(graph.nodes[i]['pos'], graph.nodes[j]['pos']) for j in graph.nodes}
for i in graph.nodes}}
costs, times = [], []
for num in range(120, 350, 40):
costs.append([])
times.append([])
for _ in tqdm(range(100)):
locations = np.random.choice(graph.nodes, num + 10, replace=False)
model = MultiAgentFlyingSidekickTSP(graph, locations[:10], locations[10:], distance, 3)
start = time.time()
_, cost = model.solve()
times[-1].append(time.time() - start)
costs[-1].append(cost)
np.save("city-time.npy", np.array(times))
np.save("city-cost.npy", np.array(costs))
def scale_rates():
print('studying the scalability of fix rates case')
graph = manhattan()
distance = {'truck': dict(nx.all_pairs_dijkstra_path_length(graph, weight='weight')),
'drone': {i: {j: haversine(graph.nodes[i]['pos'], graph.nodes[j]['pos']) for j in graph.nodes}
for i in graph.nodes}}
costs, times = [], []
for num in range(3, 21, 3):
costs.append([])
times.append([])
for _ in tqdm(range(100)):
locations = np.random.choice(graph.nodes, num * (1 + 20), replace=False)
model = MultiAgentFlyingSidekickTSP(graph, locations[:num], locations[num:], distance, 'b3', 3)
start = time.time()
_, cost = model.solve()
times[-1].append(time.time() - start)
costs[-1].append(cost)
np.save("rates-time.npy", np.array(times))
np.save("rates-cost.npy", np.array(costs))
def scale_depots():
print('studying the scalability of fix cities case')
graph = manhattan()
distance = {'truck': dict(nx.all_pairs_dijkstra_path_length(graph, weight='weight')),
'drone': {i: {j: haversine(graph.nodes[i]['pos'], graph.nodes[j]['pos']) for j in graph.nodes}
for i in graph.nodes}}
costs, times = [], []
for num in range(5, 16, 2):
costs.append([])
times.append([])
for _ in tqdm(range(100)):
locations = np.random.choice(graph.nodes, num + 150, replace=False)
model = MultiAgentFlyingSidekickTSP(graph, locations[:num], locations[num:], distance, 3)
start = time.time()
_, cost = model.solve()
times[-1].append(time.time() - start)
costs[-1].append(cost)
np.save("depots-time.npy", np.array(times))
np.save("depots-cost.npy", np.array(costs))
if __name__ == '__main__':
for size in [5, 10, 15]:
test_small_instance(100, size)
for size in [50, 100, 150]:
test_manhattan(100, size)
test_cambridge(100, size)
ablation_r()
ablation_speed()
ablation_k()
scale_cities()
scale_rates()
scale_depots()