This repository contains the implementation of a Travelling Salesman Problem (TSP) solver for the CSI 5186 – AI-enabled Software Verification and Testing course, Assignment 1, Autumn 2025. Report
The project is developed by Fernando Nogueira and Kelvin Mock.
The aim is to implement a solver for the Travelling Salesman Problem (TSP) using a selection of metaheuristic search algorithms discussed in the lectures or an algorithm researched independently.
The solver targets TSP instances from TSPLIB, specifically symmetric TSP (TYPE: TSP) with Euclidean distances in 2D (EDGE WEIGHT TYPE: EUC 2D).
Problem instances can be found at http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/.
The dataset structure is explained at http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/tsp95.pdf.
The program takes a .tsp file as input and produces:
- A single output file named
solution.csvcontaining a single column of city (node) indices in the order of the solution. - The total distance travelled printed to standard output.
For example, for a solution visiting cities 5, 4, 1, 3, 2 with distance 8934.12:
> python main.py aaa.tsp
8934.12
> cat solution.csv
5
4
1
3
2
Optionally, you could select an algorithm in the third command-line argument. Here are the allowed argument names:
- SimulatedAnnealing_random
- SimulatedAnnealing_NearestNeighbor
- GeneticAlgorithm_NearestNeighbor
- Baseline_Random
By default, Simulated Annealing with Nearest Neighbor search is chosen given its consistent best results.
The submission includes the implementation files and a detailed report (as .pdf) describing the solution, approach, and optimizations implemented.
Note: The submission must be self-contained, with no dependencies on external files. Solvers should work out of the box, with reasonable documentation.
This project includes a dataset setup script that downloads and filters TSP instances from TSPLIB95. The script automatically:
- Downloads the complete TSPLIB95 dataset
- Filters for TSP instances with
TYPE: TSPandEDGE_WEIGHT_TYPE: EUC_2D - Extracts corresponding optimal tour files (
.opt.tour) when available - Saves all files to the
dataset/directory
To set up the dataset:
uv run python setup_dataset.pyNote: Not every TSP instance has a corresponding optimal tour file available in the source dataset. This is normal and expected - the script will only extract tour files that correspond to valid TSP instances.
This project requires Python 3.12 or later.
-
Install uv: Follow the installation instructions at https://docs.astral.sh/uv/getting-started/installation/
-
Clone and Set Up:
git clone <repository-url> cd <repository-name> uv sync
The uv run commands will automatically handle the virtual environment for you.
-
Clone and Set Up:
git clone <repository-url> cd <repository-name> python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate pip install -e .
-
Run commands:
python setup_dataset.py python main.py dataset/<filename>.tsp
To run the solver:
uv run python main.py <path-to-input.tsp>
This will output the total distance to stdout and generate solution.csv in the current directory.
For development or testing, use uv run python to execute scripts in the project environment.
The project is organized into several key packages and modules:
main.py: Main solver script and entry point.setup_dataset.py: Dataset setup script for downloading and filtering TSP instances from TSPLIB95.
model.py: Core data structures (City,TSPInstance) and distance calculations.io.py: TSPLIB file parsing utilities for reading.tspfiles.
base.py: Protocol definitions and base classes for iterative TSP solvers.nearest_neighbor.py: Nearest neighbor constructive algorithm implementation.random_solver.py: Random permutation solver for baseline comparison.
dataset/: Directory containing TSP instances and optimal tour files (created by setup script).bench_results/: Directory for storing benchmark results.tsp_analysis.ipynb: Jupyter notebook for algorithm analysis and visualization.
pyproject.toml: Project configuration and dependencies.uv.lock: Locked dependencies for reproducibility.README.md: This file.