-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.py
311 lines (250 loc) · 12.2 KB
/
Menu.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
import random
import package as pkg
import graph as gr
import matplotlib.pyplot as plt
def plot_route(package_stream, route):
"""
Plot the route on a map.
"""
# Create a dictionary mapping package IDs to coordinates
package_coordinates = {package.id: (package.coordinates_x, package.coordinates_y) for package in package_stream}
# Extract coordinates of packages in the route
route_coordinates = [package_coordinates[node] for node in route]
# Extract X and Y coordinates separately
x = [coord[0] for coord in route_coordinates]
y = [coord[1] for coord in route_coordinates]
# Plot the map
plt.figure(figsize=(8, 8))
for package_id, (x_coord, y_coord) in package_coordinates.items():
plt.plot(x_coord, y_coord, 'o', markersize=8, color='blue') # Plot packages
# Plot the route
for i in range(len(route) - 1):
start_package_coord = package_coordinates[route[i]]
end_package_coord = package_coordinates[route[i + 1]]
plt.plot([start_package_coord[0], end_package_coord[0]], [start_package_coord[1], end_package_coord[1]], color='red') # Plot route segment
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
plt.title('Route Visualization on Map')
plt.grid(True)
plt.show()
def main():
print()
print("Welcome to another day of work!, choose which algorithm you would like to use to get the best route to deliver the packages.")
print("Remember, you can choose to see the costs of all the routes to compare, and choose the best one.")
print("As you know from experience, the best algorithm is not always the same as they depend on the dropoff locations.")
print()
print()
print("How many packages do you have to deliver today?")
print()
nr_packages = int(input("Enter the number of packages:"))
print("How large is the map ? (Enter maximum x and y coordinates, x and y are the same)")
print()
max = int(input("Enter the maximum value for the coordinates:"))
print()
print()
stream = pkg.generate_package_stream(nr_packages, max)
graph = gr.generate_graph(stream)
initial_solution = list(graph.vertices.keys())
random.shuffle(initial_solution)
print("Initial Solution:", initial_solution)
stop = False
while (stop==False):
print("Please choose an algorithm:")
print("1. Greedy Search")
print("2. Hill Climbing")
print("3. Genetic Algorithm")
print("4. Tabu Search")
print("5. Simulated Annealing")
print("6. Compare all algorithms")
print("7. Exit")
print()
choice = input("Enter your choice: ")
if choice == '1':
greedy_result = gr.greedy_search(graph)
greedy_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph, greedy_result)
print("Greedy Result:", greedy_result)
print()
print()
print()
print("Greedy Result with Time and Distance:", greedy_result_with_timeDistance)
print()
print()
print()
print("Greedy Result Cost:", gr.evaluation_function(graph, greedy_result, greedy_result_with_timeDistance))
print()
print()
print()
elif choice == '2':
print()
itrs = int(input("For the hill climbing algorithm, how many iterations to look for a better solution do you want (if it reaches a local maxima it will stop): "))
print()
hill_climbing_result = gr.hill_Climbing(graph, initial_solution, itrs)
print()
print("Hill Climbing Result:", hill_climbing_result)
print()
hill_climbing_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph, hill_climbing_result)
print("Hill Climbing Result with Time and Distance:", hill_climbing_result_with_timeDistance)
print()
print()
print("Hill Climbing Result Cost:", gr.evaluation_function(graph, hill_climbing_result, hill_climbing_result_with_timeDistance))
print()
print()
elif choice == '3':
print()
print("For the genetic algorithm, how many generations do you want to run?")
print()
generations = int(input("Enter the number of generations:"))
print()
genetic_algorithm_result = gr.genetic_algorithm(graph, generations)
print("Genetic algorithm result:", genetic_algorithm_result)
print()
genetic_algorithm_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph, genetic_algorithm_result)
print("Genetic algorithm result with time and distance:", genetic_algorithm_result_with_timeDistance)
print()
print()
print("Genetic algorithm result cost:", gr.evaluation_function(graph, genetic_algorithm_result, genetic_algorithm_result_with_timeDistance))
print()
print()
elif choice == '4':
print()
print("For the tabu search algorithm, how many iterations do you want to run?")
print()
iterations = int(input("Enter the number of iterations: "))
print()
print()
print("What is the size of the tabu list?")
print()
tabu_list_size = int(input("Enter the size of the tabu list: "))
print()
tabu_search_result = gr.tabu_search(graph, initial_solution, tabu_list_size, iterations)
print()
print()
print("Tabu search result:", tabu_search_result)
tabu_search_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph, tabu_search_result)
print("Tabu search result with time and distance:", tabu_search_result_with_timeDistance)
print()
print()
print("Tabu Search Result Cost:", gr.evaluation_function(graph, tabu_search_result, tabu_search_result_with_timeDistance))
print()
print()
print()
elif choice == '5':
print()
print("For the simulated annealing algorithm, how many iterations do you want to run?")
print()
iterations = int(input("Enter the number of iterations: "))
print()
print("What is the initial temperature?")
print()
initial_temperature = float(input("Enter the initial temperature: "))
print()
print("What is the cooling rate?")
print()
cooling_rate = float(input("Enter the cooling rate: "))
print()
simmulated_annealing_result = gr.simulated_annealing(graph, initial_temperature, cooling_rate, iterations, initial_solution)
print("Simmulated Annealing Result:", simmulated_annealing_result)
print()
simmulated_annealing_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph,simmulated_annealing_result)
print()
print("Simmulated Annealing Result with Time and Distance:", simmulated_annealing_result_with_timeDistance)
print()
print()
print("Simmulated Annealing Result Cost:", gr.evaluation_function(graph, simmulated_annealing_result, simmulated_annealing_result_with_timeDistance))
print()
print()
print()
elif choice == '6':
greedy_result = gr.greedy_search(graph)
greedy_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph, initial_solution)
print()
itrs = int(input("For the comparisons, how many iterations to look for a better solution do you want?: "))
print()
hill_climbing_result = gr.hill_Climbing(graph, initial_solution, itrs)
hill_climbing_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph, hill_climbing_result)
genetic_algorithm_result = gr.genetic_algorithm(graph, itrs)
genetic_algorithm_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph, genetic_algorithm_result)
print()
print("What is the size of the tabu list?")
print()
tabu_list_size = int(input("Enter the size of the tabu list: "))
print()
tabu_search_result = gr.tabu_search(graph, initial_solution, tabu_list_size, itrs)
print()
print()
tabu_search_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph, tabu_search_result)
initial_temperature = float(input("Enter the initial temperature: "))
print()
print("What is the cooling rate?")
print()
cooling_rate = float(input("Enter the cooling rate: "))
print()
simmulated_annealing_result = gr.simulated_annealing(graph, initial_temperature, cooling_rate, itrs, initial_solution)
print("Simmulated Annealing Result:", simmulated_annealing_result)
print()
simmulated_annealing_result_with_timeDistance = gr.attach_current_distanceAndTime_traveled(graph,simmulated_annealing_result)
print("Greedy Result:", greedy_result)
print("Greedy Cost:", gr.evaluation_function(graph, greedy_result, greedy_result_with_timeDistance))
print()
print()
print()
print("Hill Climbing Result:", hill_climbing_result)
print("Hill Climbing Cost:", gr.evaluation_function(graph, hill_climbing_result, hill_climbing_result_with_timeDistance))
print()
print()
print()
print("Genetic algorithm result:", genetic_algorithm_result)
print("Genetic algorithm cost:", gr.evaluation_function(graph, genetic_algorithm_result, genetic_algorithm_result_with_timeDistance))
print()
print()
print()
print("Tabu search result:", tabu_search_result)
print("Tabu search cost:", gr.evaluation_function(graph, tabu_search_result, tabu_search_result_with_timeDistance))
print()
print()
print()
print("Simmulated Annealing Result:", simmulated_annealing_result)
print("Simmulated Annealing Cost:", gr.evaluation_function(graph, simmulated_annealing_result, simmulated_annealing_result_with_timeDistance))
print()
print()
print()
print("1. Greedy Search")
print("2. Hill Climbing")
print("3. Genetic Algorithm")
print("4. Tabu Search")
print("5. Simulated Annealing")
choice_to_show = input("So which route are you going to take? Choose a number between 1 and 5: ")
if choice_to_show == '1':
print("Greedy Result:", greedy_result)
plot_route(stream, greedy_result)
print()
continue
elif choice_to_show == '2':
print("Hill Climbing Result:", hill_climbing_result)
plot_route(stream, hill_climbing_result)
print()
continue
elif choice_to_show == '3':
print("Genetic algorithm result:", genetic_algorithm_result)
plot_route(stream, genetic_algorithm_result)
print()
continue
elif choice_to_show == '4':
print("Tabu search result:", tabu_search_result)
plot_route(stream, tabu_search_result)
print()
continue
elif choice_to_show == '5':
print("Simmulated Annealing Result:", simmulated_annealing_result)
plot_route(stream, simmulated_annealing_result)
print()
continue
elif choice == '7':
stop = True
print("Goodbye, have a good trip!")
return
else:
print("Invalid choice. Please choose a number between 1 and 7.")
print()
if __name__ == "__main__":
main()