This project provides interactive visualizations for two common heuristics for the Traveling Salesman Problem (TSP):
- Nearest Neighbor Heuristic
- Insertion Heuristic
The visualizations are built with Pygame and use real-world city data from Argentina.
- Interactive City Selection: Choose your starting city or initial set of cities.
- Step-by-Step Visualization: Watch the algorithms progress one step at a time.
- Detailed Information: See candidate cities, distances, and insertion costs.
- Real Distance Calculation: Uses the Haversine formula for accurate geographic distances.
- Complete Tour Visualization: Shows the final tour connecting all cities.
Make sure you have Python and pip installed. Then, install the required dependencies:
pip install -r requirements.txt
You can run either of the two heuristic visualizations:
To run the Nearest Neighbor Heuristic:
python heuristic.py
To run the Insertion Heuristic:
python heuristic2.py
- City Selection Phase:
- LEFT/RIGHT Arrow Keys: Navigate through the list of cities.
- SPACE: Start the algorithm with the selected city.
- Algorithm Phase:
- SPACE: Execute the next step of the algorithm.
- R: Run the algorithm automatically until completion.
- ESC: Reset and return to city selection.
- Initial Tour Selection Phase:
- LEFT/RIGHT Arrow Keys: Navigate through the list of cities.
- SPACE: Select a city (you will select 3 to form the initial tour).
- Algorithm Phase:
- SPACE: Execute the next step (find best insertion and perform it).
- R: Run the algorithm automatically until completion.
- ESC: Reset and return to city selection.
This is a greedy algorithm that works as follows:
- Start at a selected city.
- From the current city, find the nearest unvisited city.
- Move to that city and mark it as visited.
- Repeat until all cities are visited.
- Return to the starting city to complete the tour.
This heuristic builds the tour incrementally:
- Start with a small tour of 3 cities.
- Select a city not yet in the tour.
- Find the edge in the current tour where inserting the new city causes the minimum increase in total tour length.
- Insert the city into that position.
- Repeat until all cities are included in the tour.
The program reads city data from cities.csv
which contains Argentine cities with their coordinates:
name
: City namelat
: Latitudelong
: Longitude
Heuristics provide approximate solutions to the TSP. They are fast but do not guarantee finding the absolute best (optimal) tour. The quality of the solution can depend on the starting city or the initial tour.
- Blue Circles: Unvisited cities
- Yellow Circle: Currently selected starting city (during selection)
- Red Circle: Current city being processed
- Green Circles: Already visited cities
- Orange Circles: Candidate cities (possible next destinations)
- Red Lines: Current path taken
- Orange Lines: Lines to candidate cities
- Candidates Table: Right-side table showing all unvisited cities sorted by distance
- Yellow highlight: Next city to be selected (nearest neighbor)
- Distance shown in kilometers using Haversine formula
- Run the program
- Use arrow keys to select "Buenos Aires" as starting city
- Press SPACE to begin
- Press SPACE repeatedly to see each step, or press R for automatic execution
- Observe how the algorithm chooses the nearest city at each step
- See the final tour and total distance
- Press ESC to try a different starting city