Carina Samson
Algorithm
- Read the input file from command line terminal
- Store the x,y coordinates from the input file to cities
- Call solve_tsp() to get a random start city.
- Iterate until the best_tour with the shortest distance is found
- Execute the Greedy Algorithm using the method solve_greedy()
- Calculate the Euclidean distances between cities
- Set current_city equal to start_city
- Create a set called unvisited_cities to track all the unvisited cities, starting with start city
- Remove the first city from unvisited_cities, indicating that we've visited it
- Add current_city to the tour
- Iterate until there are no more unvisited cities
- Find the nearest city with the minimum distance from the current city and store it as next_city
- Add next_city to the tour
- Set current_city = next_city to iterate
- Return the resulting tour
- Execute the 3-Opt algorithm using the method solve_3opt() to improve tour
- Refer to the following showing the different 3-Opt cases (a, b and c refer to city1, city2 and city3 in my code): 3-Opt Diagram
- Iterate for all possible combinations of three edges in the given tour
- Call swap_3opt() for all combination of the three segments
- Create a subset of tour with segments 1-2, 3-4 and 5-6
- Calculate the distances for the subtours
- Swap the segments by comparing the distances of the subtours
- Reverse the segments for cases 4-7 only
- Case 1-3 will be completed by the 2-opt swap function
- Execute the 2-Opt algorithm using the method solve_2opt() to further improve tour
- Set best_tour as our previously created tour and best_distance as the computed total distance of our tour
- Find a segment that crosses over itself
- Reorder the segment so that it does not cross
- Calculate the new distance of the tour after the swap
- Repeat until no improvements can be made
- If new tour distance after swap is less than the distance of the given tour then return new tour
- Set best_tour as our previously created tour and best_distance as the computed total distance of our tour
- Return the newly optimized tour
- Print the shortest tour and its distance
- Save the shortest tour as an ordered list of the index numbers of cities to the corresponding output file
Variables
input_file
- name of the input filecities
- tuple consisting of x, y coordinate pointstour
- used to keep track of the TSP tourshortest_tour
- the resulting shortest TSP tourshortest_distance
- the shortest_distance of the TSP tour
Methods
-
solve_tsp_tour()
- Domain Parameter(s): cities
- Range: shortest_tour, shortest_distance
-
solve_greedy()
- Domain Parameter(s): start_city, cities
- Range: tour
-
solve_3opt()
- Domain Parameter(s): tour, cities
- Range: best_tour
-
swap_3opt()
- Domain Parameter(s): tour, cities, city1, city2, city3
- Range: new_tour
-
solve_2opt()
- Domain Parameter(s): tour, cities
- Range: best_tour
-
swap_2opt()
- Domain Parameter(s): tour, city1, city2
- Range: new_tour
-
distance()
- Domain Parameter(s): city1, city2
- Range: euclidean_distance
-
compute_total()
- Domain Parameter(s): tour, cities
- Range: total_distance