Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/interfaces/tspalgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class TSPAlgorithm:
Opt2: str = "2-Opt"
Opt3: str = "3-Opt"
SimulatedAnnealing: str = "SimulatedAnnealing"
SimulatedAnnealingGenetic: str = "SimulatedAnnealingGenetic"

##
## End of class
Expand Down
44 changes: 44 additions & 0 deletions src/lib/tsp/tspalgorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from lib.utils.create_dist_matrix import create_dist_matrix
from lib.utils.calculate_tour_distance import calculate_tour_distance
from lib.utils.three_opt_swap import three_opt_swap
from lib.utils.genetic_algo import run_generations
import itertools, math, random

from lib.utils.duration import duration
Expand Down Expand Up @@ -69,6 +70,9 @@ def get_shortest_tour(graph: Graph, algorithm: str) -> Tour:

elif algorithm == TSPAlgorithm.SimulatedAnnealing:
return TSPAlgorithms.simulated_annealing(graph)

elif algorithm == TSPAlgorithm.SimulatedAnnealingGenetic:
return TSPAlgorithms.simulated_annealing_with_genetic(graph)

elif algorithm == TSPAlgorithm.GreedyHeuristic:
return TSPAlgorithms.greedy_heuristic(graph)
Expand Down Expand Up @@ -259,6 +263,8 @@ def simulated_annealing(graph: Graph, initial_tour: Tour = None) -> Tour:
best_distance = current_distance
temperature = 195
cooling_rate = 0.995
#temperature = 270
#cooling_rate = 0.99

##
## Our main loop
Expand Down Expand Up @@ -293,6 +299,44 @@ def simulated_annealing(graph: Graph, initial_tour: Tour = None) -> Tour:
## End of function
##

##
## Simulated annealing algorithm
##

@staticmethod
def simulated_annealing_with_genetic(graph: Graph, initial_tour: Tour = None) -> Tour:
"""Applies the Simulated Annealing algorithm with genetic mutation to find an optimal tour for the TSP.

Args:
graph (Graph): The graph representing all the cities and distances between them.
initial_tour (Tour): An optional initial tour from which to start the optimization.

Returns:
Tour: An optimized tour found using GeAn
"""

##
## Calculate the shortest tour with brute force
##
bruteForce = calculate_tour_distance(TSPAlgorithms.get_shortest_tour(graph, "BruteForce").nodes)
#bruteForce = 118282
#print("BRUTE: ", bruteForce)

##
## Initial setup
##
current_nodes = initial_tour.nodes if initial_tour is not None else graph.nodes
current_distance = calculate_tour_distance(current_nodes)
best_nodes = current_nodes[:]
best_distance = current_distance


if (len(best_nodes) > 2):
best_nodes, best_distance = run_generations(100, bruteForce, best_nodes, best_distance)

## Assuming Tour class takes a list of Node objects, distance, and algorithm name
return Tour(nodes=best_nodes, distance=best_distance, algorithm=TSPAlgorithm.SimulatedAnnealing)

@staticmethod
def greedy_heuristic(graph: Graph) -> Tour:
"""Greedy heuristic algorithm for the TSP
Expand Down
Loading