Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
77368c8
Moved tuning scripts to folder
seofernando25 Oct 5, 2025
7d56bc7
Update constants.py
seofernando25 Oct 5, 2025
5453f61
Moved tuning scripts to folder
seofernando25 Oct 5, 2025
f00865f
Add figure generation scripts
seofernando25 Oct 5, 2025
8116d00
Simplify GeneticAlgorithmSolver step
seofernando25 Oct 5, 2025
fe57f63
Remove random seed usage and improve logging messages
seofernando25 Oct 5, 2025
2a65675
Remove unused imports from main.py
seofernando25 Oct 5, 2025
523ad71
Remove random seed usage and adjust algorithm parameters
seofernando25 Oct 5, 2025
aef117e
Add tqdm dependency
seofernando25 Oct 5, 2025
e7f9f7e
delete todo
seofernando25 Oct 5, 2025
26bb67d
Rename main by tsp_solver
seofernando25 Oct 5, 2025
32e32c8
update readme
seofernando25 Oct 5, 2025
d1c2997
Update GeneticAlgorithmSolver
seofernando25 Oct 5, 2025
4e595b9
Update GeneticAlgorithmSolver
seofernando25 Oct 5, 2025
c1666ac
Adjust figure scripts
seofernando25 Oct 5, 2025
58b8542
Refactor figure scripts
seofernando25 Oct 5, 2025
8f7821f
Update constants and tuning configurations; enhance figure scripts wi…
seofernando25 Oct 6, 2025
2296815
Add random baseline figure generation and update box plot labels
seofernando25 Oct 6, 2025
9ef1af1
Add statistics to box plot
seofernando25 Oct 6, 2025
850a625
Clean up unused imports
seofernando25 Oct 6, 2025
b6784d4
Adjust figure size
seofernando25 Oct 6, 2025
d941b66
Update analysis notebook.
seofernando25 Oct 6, 2025
925616d
update readme and notebook
seofernando25 Oct 6, 2025
e9d5905
rename notebook
seofernando25 Oct 6, 2025
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
7 changes: 2 additions & 5 deletions main.py → tsp solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

# Utilities
from pathlib import Path
import time
import matplotlib.pyplot as plt
import random
from constants import *
from util import *
from constants import N_RUNS, MAX_SECONDS, MAX_ITERATIONS
from util import find_optimal_tour, setup_algorithm, run_single_trial_by_timing, run_single_iteration_trial
import sys
import logging
from multiprocessing import Pool, cpu_count
Expand Down
88 changes: 55 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The program takes a .tsp file as input and produces:

For example, for a solution visiting cities 5, 4, 1, 3, 2 with distance 8934.12:
```
> python main.py aaa.tsp
> python tsp_solver.py aaa.tsp
8934.12
> cat solution.csv
5
Expand All @@ -32,16 +32,9 @@ For example, for a solution visiting cities 5, 4, 1, 3, 2 with distance 8934.12:

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.

## Dataset Setup

This project includes a dataset setup script that downloads and filters TSP instances from TSPLIB95. The script automatically:

1. Downloads the complete TSPLIB95 dataset
2. Filters for TSP instances with `TYPE: TSP` and `EDGE_WEIGHT_TYPE: EUC_2D`
3. Extracts corresponding optimal tour files (`.opt.tour`) when available
4. Saves all files to the `dataset/` directory
This project includes a dataset setup script that downloads and filters TSP instances from TSPLIB95.

To set up the dataset:
```bash
Expand Down Expand Up @@ -74,53 +67,82 @@ The `uv run` commands will automatically handle the virtual environment for you.
git clone <repository-url>
cd <repository-name>
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
source .venv/bin/activate
pip install -e .
```

2. **Run commands**:
```bash
python setup_dataset.py
python main.py dataset/<filename>.tsp
python tsp_solver.py dataset/<filename>.tsp
```

## Usage

To run the solver:

```
uv run python main.py <path-to-input.tsp>
uv run python tsp_solver.py <problem.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.

## Project Structure
## Generating Figures

The project is organized into several key packages and modules:
To generate performance figures for the TSP algorithms, you can run individual scripts from the `figure_scripts/` directory.

### Core Modules
- `main.py`: Main solver script and entry point.
- `setup_dataset.py`: Dataset setup script for downloading and filtering TSP instances from TSPLIB95.
```bash
uv run python -m figure_scripts.box_plot_figures
uv run python -m figure_scripts.relative_work_figures
uv run python -m figure_scripts.relative_work_nn_figures
uv run python -m figure_scripts.time_budget_figures
uv run python -m figure_scripts.time_budget_nn_figures
```

To generate all figures at once:

```bash
uv run python generate_figures.py
```

### Package Organization
Ensure the dataset is set up (run `uv run python setup_dataset.py` if not already done).

#### `tsp/` - TSP Core Package
- `model.py`: Core data structures (`City`, `TSPInstance`) and distance calculations.
- `io.py`: TSPLIB file parsing utilities for reading `.tsp` files.
## Hyperparam Tuning

#### `algorithm/` - Algorithm Implementations
- `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.
The `tuning/` directory contains scripts for hyperparameter tuning of GA and SA. These tune parameters over a fixed time budget on the lin105.tsp instance.

### Data and Analysis
- `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.
```bash
uv run python -m tuning.ga_tuning
uv run python -m tuning.sa_tuning
```

The console output should include output of the best params.

### Configuration
- `pyproject.toml`: Project configuration and dependencies.
- `uv.lock`: Locked dependencies for reproducibility.
- `README.md`: This file.
## TSP Analysis Notebook

You can compile the notebook to PDF by running:

```
uv run jupyter nbconvert --to pdf tsp_analysis.ipynb
```

## Project Structure

The project is organized into directories for core functionality, algorithms, data handling, and analysis:

### Core Modules
- Entry point and utilities: `main.py`, `setup_dataset.py`, `generate_figures.py`, `constants.py`, `util.py`.

### Packages
- `tsp/`: Core TSP model and I/O.
- `algorithm/`: Heuristic algorithm implementations (e.g., genetic, simulated annealing, nearest neighbor and random solver).
- `figure_scripts/`: Scripts for generating performance visualizations (e.g., box plots, time budgets, relative work comparisons).
- `tuning/`: Hyperparameter tuning scripts.
- `tests/`: Tests.

### Data and Outputs
- `dataset/`: TSP instances and optimal tours.
- `figures/`: Generated plots.
- `solution.csv`: Solver output file.
9 changes: 0 additions & 9 deletions TODO.md

This file was deleted.

Loading