No project description provided
Project description
ACO-TSP
Ant Colony Optimization for the Traveling Salesman Problem (TSP) implemented in Python.
This library provides a clean and configurable implementation of the Ant Colony Optimization (ACO) algorithm to solve the TSP.
It includes robust error handling, parameter customization, convergence tracking, and easy integration into Python projects.
Usage
Basic Example
from aco_tsp import ACO_TSP
# Define your distance matrix (5x5 example)
distance_matrix = [
[0, 10, 12, 11, 14],
[10, 0, 13, 15, 8],
[12, 13, 0, 9, 14],
[11, 15, 9, 0, 16],
[14, 8, 14, 16, 0]
]
# Initialize and run ACO
aco = ACO_TSP(
distance_matrix,
labels=['A', 'B', 'C', 'D', 'E'],
num_ants=15,
num_iterations=100,
beta=3
)
path, length = aco.solve()
print(f"Optimal Path: {' → '.join(path)}")
print(f"Total Distance: {length:.2f}")
Advanced Usage
# Get full solution details
solution = aco.get_solution()
print(f"Best tour indices: {solution['tour']}")
print(f"Convergence history: {solution['convergence']}")
# Access algorithm parameters
print(f"Parameters used: {solution['parameters']}")
# Visualize pheromone matrix
import matplotlib.pyplot as plt
plt.imshow(solution['pheromones'], cmap='hot', interpolation='nearest')
plt.title("Pheromone Matrix")
plt.colorbar()
plt.show()
Parameters
All parameters are configurable when initializing the ACO_TSP class:
| Parameter | Type | Default | Description |
|---|---|---|---|
distance_matrix |
2D list | Required | Distance matrix between nodes (square matrix) |
labels |
list | None | Node labels (default: numerical indices) |
num_ants |
int | 10 | Number of ants in the colony (>0) |
evap_rate |
float | 0.5 | Pheromone evaporation rate (0-1) |
Q |
float | 100 | Pheromone deposit constant (>0) |
alpha |
float | 1 | Pheromone influence exponent (≥0) |
beta |
float | 2 | Heuristic (visibility) influence exponent (≥0) |
num_iterations |
int | 50 | Number of optimization iterations (>0) |
start_node |
int | 0 | Starting node index (0-based) |
Error Handling
The library provides comprehensive error checking with meaningful messages:
try:
aco = ACO_TSP(
[[0, 10], [10, 0]], # Invalid matrix size
labels=['A', 'B', 'C'],
evap_rate=1.5,
num_ants=0
)
except ValueError as e:
print(f"Configuration error: {e}")
Common error scenarios:
- Non-square distance matrix
- Label size mismatch
- Parameter out of valid range
- Invalid start node index
- Calling
get_solution()beforesolve()
Theory Overview
Ant Colony Optimization mimics how real ants find shortest paths:
- Pheromone Initialization
Equal pheromone on all paths. - Solution Construction
Ants probabilistically choose paths based on:- Pheromone intensity (
α) - Heuristic visibility (
1 / distance,β)
- Pheromone intensity (
- Pheromone Update
- Evaporation: All paths lose pheromone.
- Deposition: Ants reinforce paths used in their solution.
- Convergence
Over iterations, better paths accumulate more pheromone.
Output
After running solve(), you can get:
- Best path (labels & indices)
- Total path length
- Convergence history
- Final pheromone matrix
- Parameters used
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aco_tsp-0.1.tar.gz.
File metadata
- Download URL: aco_tsp-0.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d70dcd06624bddd328d25aec9f4e2e9a32eda41b7804f062879a15f8e3d5dd74
|
|
| MD5 |
7796f3c37f9c08c51d1cc0217d8c4752
|
|
| BLAKE2b-256 |
4339dd916af3b408bca84ab6d0e11ee1bf3d5c0d24d621b467c40e8582a5deff
|
File details
Details for the file aco_tsp-0.1-py3-none-any.whl.
File metadata
- Download URL: aco_tsp-0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d4df22322d385200ab84a24e6837080b49bc01930f3fd3fc9d4d3e2a14bd43e
|
|
| MD5 |
e07f53a827db2b5d7fa8d9acb382eb11
|
|
| BLAKE2b-256 |
82a3119a1c43f2eedd74226206bad8aff4884a738ef7234e9d50d1b04d2db49b
|