forked from madak1hallgato/TSP_ppke-msc-scipython-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_console.py
41 lines (34 loc) · 1.58 KB
/
app_console.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
import random
from map import MapFileManager
from traveling_salesman_problem import TSP
with MapFileManager(file_path="cities.json") as map_file_manager:
map_file_manager.create_map_from_file()
complete_map = map_file_manager.get_map()
city_num = random.randint(2, 8)
complete_map.activate_n_random_city(city_num=city_num)
complete_map.activate_city("New York")
tsp_solver = TSP(complete_map=complete_map, start_city_name="New York")
result_nn = tsp_solver.nearest_neighbor()
solution_nearest_neighbor = result_nn["path"]
time_nearest_neighbor = result_nn["time"]
if solution_nearest_neighbor:
print("Nearest Neighbor solution:")
print(' -> '.join(city.name for city in solution_nearest_neighbor))
print(f"Time: {time_nearest_neighbor:.2f} seconds")
else: print("No NN solution found.")
result_bf = tsp_solver.brute_force()
solution_brute_force = result_bf["path"]
time_brute_force = result_bf["time"]
if solution_brute_force:
print("Brute Force solution:")
print(' -> '.join(city.name for city in solution_brute_force))
print(f"Time: {time_brute_force:.2f} seconds")
else: print("No BF solution found.")
result_ga = tsp_solver.genetic_algorithm(population_size=150, generations=150, mutation_rate=0.01)
solution_genetic_algorithm = result_ga["path"]
time_genetic_algorithm = result_ga["time"]
if solution_genetic_algorithm:
print("Genetic Algorithm solution:")
print(' -> '.join(city.name for city in solution_genetic_algorithm))
print(f"Time: {time_genetic_algorithm:.2f} seconds")
else: print("No GA solution found.")