-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsp_solver.py
More file actions
278 lines (220 loc) · 9.33 KB
/
tsp_solver.py
File metadata and controls
278 lines (220 loc) · 9.33 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
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
"""
TSP Solver implementing brute force and nearest neighbor approximation algorithms.
Includes timing and comparison utilities for analyzing computational limits.
"""
import json
import time
import itertools
import sys
from pathlib import Path
# ============================================================================
# BRUTE FORCE SOLUTION
# ============================================================================
def tsp_brute_force(distances, timeout=60):
"""
Find optimal TSP route by checking all possible permutations.
Args:
distances: 2D list where distances[i][j] is distance from location i to j
timeout: Maximum seconds to run before stopping (default 60)
Returns:
Tuple of (best_route, best_distance) or (None, None) if timeout
"""
n = len(distances)
locations = list(range(n))
# Start at location 0 (dispatch center)
start = 0
other_locations = [i for i in locations if i != start]
best_distance = float('inf')
best_route = None
start_time = time.time()
routes_checked = 0
# Check all permutations of locations (excluding start)
for perm in itertools.permutations(other_locations):
# Check if we've exceeded timeout
if time.time() - start_time > timeout:
print(f" TIMEOUT after checking {routes_checked:,} routes")
return None, None
# Build full route: start -> permutation -> back to start
route = [start] + list(perm) + [start]
# Calculate total distance for this route
distance = 0
for i in range(len(route) - 1):
distance += distances[route[i]][route[i + 1]]
# Update best if this is better
if distance < best_distance:
best_distance = distance
best_route = route[:-1] # Remove duplicate start at end
routes_checked += 1
return best_route, best_distance
# ============================================================================
# NEAREST NEIGHBOR APPROXIMATION
# ============================================================================
def tsp_nearest_neighbor(distances, start=0):
"""
Find approximate TSP route using nearest neighbor heuristic.
Always travels to the nearest unvisited location.
Args:
distances: 2D list where distances[i][j] is distance from location i to j
start: Starting location index (default 0 for dispatch center)
Returns:
Tuple of (route, total_distance)
"""
n = len(distances)
# Track which locations we've visited
unvisited = set(range(n))
current = start
route = [current]
unvisited.remove(current)
total_distance = 0
# Visit each location
while unvisited:
# Find nearest unvisited location
nearest = min(unvisited, key=lambda x: distances[current][x])
# Travel to nearest location
total_distance += distances[current][nearest]
current = nearest
route.append(current)
unvisited.remove(current)
# Return to start
total_distance += distances[current][start]
return route, total_distance
# ============================================================================
# TESTING AND TIMING UTILITIES
# ============================================================================
def load_dataset(size):
"""Load emergency site dataset of given size."""
filename = f'data/sites_{size}.json'
if not Path(filename).exists():
print(f"Error: {filename} not found. Run location_generator.py first.")
sys.exit(1)
with open(filename, 'r') as f:
return json.load(f)
def test_small_cases():
"""Test both algorithms on small datasets with known correct results."""
print("\n" + "="*70)
print("TESTING: Small Cases (Verifying Correctness)")
print("="*70)
test_sizes = [5, 8, 10]
for size in test_sizes:
print(f"\nTesting {size} locations...")
data = load_dataset(size)
distances = data['distances']
# Test brute force
route_bf, dist_bf = tsp_brute_force(distances)
print(f" Brute Force: Distance = {dist_bf:.2f}")
# Test nearest neighbor
route_nn, dist_nn = tsp_nearest_neighbor(distances)
print(f" Nearest Neighbor: Distance = {dist_nn:.2f}")
print(f" Approximation Quality: {(dist_nn/dist_bf)*100:.1f}% of optimal")
def time_brute_force():
"""Time brute force algorithm on increasing dataset sizes."""
print("\n" + "="*70)
print("TIMING: Brute Force Algorithm")
print("="*70)
print("\nWARNING: Larger sizes may take several minutes or timeout at 60 seconds")
sizes = [5, 8, 10, 12, 15]
print(f"\n{'Size':<6} {'Routes':<15} {'Time (s)':<12} {'Distance':<12} {'Status'}")
print("-" * 70)
for size in sizes:
data = load_dataset(size)
distances = data['distances']
# Calculate number of routes to check
import math
num_routes = math.factorial(size - 1) # (n-1)! for fixed start
# Time the algorithm
start_time = time.time()
route, distance = tsp_brute_force(distances, timeout=60)
elapsed = time.time() - start_time
if route is None:
print(f"{size:<6} {num_routes:<15,} {elapsed:<12.3f} {'N/A':<12} TIMEOUT")
else:
print(f"{size:<6} {num_routes:<15,} {elapsed:<12.3f} {distance:<12.2f} Complete")
def time_approximation():
"""Time nearest neighbor approximation on increasing dataset sizes."""
print("\n" + "="*70)
print("TIMING: Nearest Neighbor Approximation")
print("="*70)
sizes = [5, 8, 10, 12, 15, 20]
print(f"\n{'Size':<6} {'Time (s)':<12} {'Distance':<12}")
print("-" * 40)
for size in sizes:
data = load_dataset(size)
distances = data['distances']
# Time the algorithm
start_time = time.time()
route, distance = tsp_nearest_neighbor(distances)
elapsed = time.time() - start_time
print(f"{size:<6} {elapsed:<12.6f} {distance:<12.2f}")
def compare_all_approaches():
"""Compare brute force and approximation side-by-side."""
print("\n" + "="*70)
print("COMPARISON: Brute Force vs Nearest Neighbor")
print("="*70)
print("\nThis compares both algorithms on datasets where brute force completes.")
sizes = [5, 8, 10]
print(f"\n{'Size':<6} {'Optimal':<12} {'Approx':<12} {'% of Optimal':<14} {'BF Time (s)':<14} {'NN Time (s)':<12}")
print("-" * 90)
for size in sizes:
data = load_dataset(size)
distances = data['distances']
# Brute force
start_time = time.time()
route_bf, dist_bf = tsp_brute_force(distances, timeout=60)
time_bf = time.time() - start_time
# Nearest neighbor
start_time = time.time()
route_nn, dist_nn = tsp_nearest_neighbor(distances)
time_nn = time.time() - start_time
if route_bf is None:
print(f"{size:<6} {'TIMEOUT':<12} {dist_nn:<12.2f} {'N/A':<14} {'TIMEOUT':<14} {time_nn:<12.6f}")
else:
quality = (dist_nn / dist_bf) * 100
print(f"{size:<6} {dist_bf:<12.2f} {dist_nn:<12.2f} {quality:<14.1f} {time_bf:<14.3f} {time_nn:<12.6f}")
print("\n" + "="*70)
print("Note: Approximation runs in polynomial time, handling larger sizes easily.")
print("Brute force becomes impractical beyond 12-15 locations.")
print("="*70)
# ============================================================================
# MAIN INTERFACE
# ============================================================================
def print_usage():
"""Print usage instructions."""
print("\nUsage: python tsp_solver.py [option]")
print("\nOptions:")
print(" --test-small Test correctness on small datasets")
print(" --time-brute-force Time brute force on increasing sizes")
print(" --time-approximation Time approximation on increasing sizes")
print(" --compare-all Compare both algorithms side-by-side")
print(" --help Show this help message")
print("\nFor the assignment, run these commands in order:")
print(" 1. python tsp_solver.py --test-small")
print(" 2. python tsp_solver.py --time-brute-force")
print(" 3. python tsp_solver.py --time-approximation")
print(" 4. python tsp_solver.py --compare-all")
def main():
"""Main entry point for running timing experiments."""
if len(sys.argv) < 2:
print_usage()
return
option = sys.argv[1]
# Check if data files exist
if not Path('data/sites_5.json').exists():
print("\nError: Data files not found!")
print("Please run: python location_generator.py")
print("This will create the required test datasets.")
sys.exit(1)
if option == '--test-small':
test_small_cases()
elif option == '--time-brute-force':
time_brute_force()
elif option == '--time-approximation':
time_approximation()
elif option == '--compare-all':
compare_all_approaches()
elif option == '--help':
print_usage()
else:
print(f"\nError: Unknown option '{option}'")
print_usage()
if __name__ == '__main__':
main()