-
Notifications
You must be signed in to change notification settings - Fork 20
/
greedy-random-tsp.py
executable file
·121 lines (90 loc) · 3.16 KB
/
greedy-random-tsp.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
#!/usr/bin/env python
"""
Traveling salesman solution with random start and greedy path selection
You can select how many iterations to run by doing the following:
python tsp_greedy_random_start.py 20 #runs 20 times
"""
import sys
from random import choice
import numpy as np
from routes import values
dt = np.dtype([("city_start", "S10"), ("city_end", "S10"), ("distance", int)])
data_set = np.array(values, dtype=dt)
def all_cities():
"""Finds unique cities
array([["A", "A"],
["A", "B"]])
"""
cities = {}
city_set = set(data_set["city_end"])
for city in city_set:
cities[city] = ""
return cities
def randomize_city_start(cities):
"""Returns a randomized city to start trip"""
return choice(cities)
def get_shortest_route(routes):
"""Sort the list by distance and return shortest distance route"""
route = sorted(routes, key=lambda dist: dist[2]).pop(0)
return route
def greedy_path():
"""Select the next path to travel based on the shortest, nearest path"""
itinerary = []
cities = all_cities()
starting_city = randomize_city_start(list(cities.keys()))
# print "starting_city: %s" % starting_city
cities_visited = {}
# we want to iterate through all cities once
count = 1
while True:
possible_routes = []
# print "starting city: %s" % starting_city
for path in data_set:
if starting_city in path["city_start"]:
# we can't go to cities we have already visited
if path["city_end"] in cities_visited:
continue
else:
# print "path: ", path
possible_routes.append(path)
if not possible_routes:
break
# append this to itinerary
route = get_shortest_route(possible_routes)
# print "Route(%s): %s " % (count, route)
count += 1
itinerary.append(route)
# add this city to the visited city list
cities_visited[route[0]] = count
# print "cities_visited: %s " % cities_visited
# reset the starting_city to the next city
starting_city = route[1]
# print "itinerary: %s" % itinerary
return itinerary
def get_total_distance(complete_itinerary):
distance = sum(z for x, y, z in complete_itinerary)
return distance
def lowest_simulation(num):
routes = {}
for _ in range(num):
itinerary = greedy_path()
distance = get_total_distance(itinerary)
routes[distance] = itinerary
shortest_distance = min(routes.keys())
route = routes[shortest_distance]
return shortest_distance, route
def main():
"""runs everything"""
if len(sys.argv) == 2:
iterations = int(sys.argv[1])
print("Running simulation %s times" % iterations)
distance, route = lowest_simulation(iterations)
print("Shortest Distance: %s" % distance)
print("Optimal Route: %s" % route)
else:
# print "All Routes: %s" % data_set
itinerary = greedy_path()
print("itinerary: %s" % itinerary)
print("Distance: %s" % get_total_distance(itinerary))
if __name__ == "__main__":
main()