Bio-inspired stochastic optimization leveraging emergent collective intelligence of artificial ant colonies to converge on near-optimal Hamiltonian cycles in polynomial-bounded runtime.
Author ยท Built by AG โ Chief AI Officer @ Google
- Problem Statement
- Theoretical Foundation
- Algorithm Architecture
- Mathematical Formulation
- Convergence Analysis
- Implementation Details
- Getting Started
- Configuration & Hyperparameters
- Results & Visualization
- Computational Complexity
- References
- Contributing
- License
The Travelling Salesman Problem (TSP) is one of the most intensively studied problems in computational optimization. Given a set of
| Aspect | Details |
|---|---|
| Complexity Class | NP-hard (decision version is NP-complete) |
| Search Space |
|
| Brute Force | Intractable for |
| Real-World Impact | Logistics, VLSI design, DNA sequencing, telescope scheduling |
For our 20-city instance, the search space contains
Ant Colony Optimization (ACO), first introduced by Marco Dorigo (1992), belongs to the family of swarm intelligence algorithms. It draws inspiration from the foraging behavior of real ant colonies, where individual agents with limited cognitive ability collectively solve complex optimization problems through stigmergy โ indirect communication via chemical pheromone trails deposited on the environment.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SWARM INTELLIGENCE TAXONOMY โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ Nature-Inspired Metaheuristics โ
โ โโโ Evolutionary Algorithms โ
โ โ โโโ Genetic Algorithm (GA) โ
โ โ โโโ Differential Evolution (DE) โ
โ โ โโโ Evolution Strategy (ES) โ
โ โโโ Swarm Intelligence โโโ THIS PROJECT โ
โ โ โโโ Ant Colony Optimization (ACO) โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โโโ Particle Swarm Optimization (PSO) โ
โ โ โโโ Bee Colony Optimization (BCO) โ
โ โโโ Physics-Based โ
โ โโโ Simulated Annealing (SA) โ
โ โโโ Gravitational Search (GSA) โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Biological System | Computational Model |
|---|---|
| Ant | Constructive solution agent |
| Pheromone trail | Learned desirability values on edges |
| Trail evaporation | Forgetting mechanism (exploration) |
| Shorter path โ faster trips โ more pheromone | Positive feedback loop for better solutions |
| Colony convergence | Algorithm convergence to near-optimal tour |
The algorithm operates through a construct-update loop across discrete generations:
โโโโโโโโโโโโโโโโโโโโโโโโ
โ INITIALIZATION โ
โ โข Distance Matrix โ
โ โข Pheromone Matrix โ
โ ฯ(i,j) = 0.1 โi,j โ
โโโโโโโโโโโโฌโโโโโโโโโโโโ
โ
โโโโโโโโโโโโผโโโโโโโโโโโโ
โโโโโโบโ GENERATION LOOP โ
โ โ g = 1, 2, ..., G โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโ
โ โ
โ โโโโโโโโโโโโผโโโโโโโโโโโโ
โ โ ANT CONSTRUCTION โ
โ โ For each ant k: โ
โ โ โข Start random city โ
โ โ โข Build full tour โ
โ โ via probabilistic โ
โ โ city selection โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโ
โ โ
โ โโโโโโโโโโโโผโโโโโโโโโโโโ
โ โ PHEROMONE UPDATE โ
โ โ โข Deposit: ฮฯ โ โ
โ โ 1/โ(tour_cost) โ
โ โ โข Evaporate: ฯ -=ฯ โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโ
โ โ
โ โโโโโโโโโโโโผโโโโโโโโโโโโ
โ โ BEST TOUR TRACKING โ
โ โ Update global best โ
โ โ if improved โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโ
โ โ
โ โโโโโโโโโผโโโโโโโโ
โโโโโโโโโโค g < G ? YES โ
โโโโโโโโโฌโโโโโโโโ
โ NO
โโโโโโโโโโโโผโโโโโโโโโโโโ
โ RETURN BEST TOUR โ
โโโโโโโโโโโโโโโโโโโโโโโโ
The pairwise Euclidean distance between cities
At each construction step, ant
Where:
-
$\tau_{ij}$ โ pheromone intensity on edge$(i,j)$ -
$\eta_{ij} = \frac{1}{d_{ij}}$ โ heuristic visibility (inverse distance) -
$\mathcal{N}_i^k$ โ feasible neighborhood (unvisited cities for ant$k$ )
Note: This implementation uses
$\alpha = 1, \beta = 1$ implicitly, weighting pheromone and heuristic information equally.
After each ant completes a tour, pheromone is deposited on all traversed edges:
Where the pheromone deposit is proportional to the inverse square root of tour cost:
This non-linear deposit function amplifies the reward signal for shorter tours more aggressively than the classical
After all ants complete their tours, global evaporation reduces all pheromone values:
Where
The ACO algorithm exhibits characteristic convergence behavior driven by positive feedback:
Cost โฒ
โ โฒ
โ โฒโฒ
โ โฒโฒ
โ โฒโฒโฒ
โ โฒโฒโฒโฒ
โ โฒโฒโฒโฒโฒโฒ
โ โฒโฒโฒโฒโฒโฒโฒโฒโฒ
โ โฒโฒโฒโฒโฒโฒโฒโฒโฒโฒโฒโฒโฒโฒ
โ โฒโฒโฒโฒโฒโฒโฒโฒโฒโฒโฒโฒโฒโฒโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโบ Generation
0 32 64 96 128 160
| Phase | Generations | Behavior |
|---|---|---|
| Exploration | 0 โ 30 | High entropy; diverse tours; rapid cost decrease |
| Exploitation | 30 โ 100 | Pheromone trails strengthen; convergence accelerates |
| Refinement | 100 โ 160 | Marginal improvements; near-optimal plateau reached |
solving-TSP-problem-with-ACO-Heuristic-Algorithm-in-python/
โ
โโโ ACO.py # Core algorithm: ACO solver + visualization
โโโ Cities List.txt # Input dataset: 20 cities with (index, x, y)
โโโ README.md # This document
โโโ CONTRIBUTING.md # Contribution guidelines
โโโ CITATION.cff # Citation metadata (CFF format)
โโโ requirements.txt # Python dependencies
โโโ LICENSE # MIT License
โโโ .gitignore # Git ignore rules
class Draw # Visualization engine โ renders city plots & route arrows
class ACO # Core solver โ pheromone management, tour construction, optimization loop| Method | Purpose | Complexity |
|---|---|---|
initialDistances() |
Build |
|
getDistance() |
Euclidean distance between two cities | |
getNextCity() |
Probabilistic city selection via roulette wheel | |
updatePheromone() |
Deposit pheromone on edges of completed tour | |
applyEvaporatio() |
Global pheromone evaporation with floor clamping | |
getRouteCost() |
Sum edge weights along a tour | |
solve() |
Main optimization loop across |
- Python 3.8+
matplotlibfor visualization
# Clone the repository
git clone https://github.com/Elktrn/solving-TSP-problem-with-ACO-Heuristic-Algorithm-in-python.git
cd solving-TSP-problem-with-ACO-Heuristic-Algorithm-in-python
# Install dependencies
pip install -r requirements.txt
# Run the solver
python ACO.pyEdit Cities List.txt with one city per line in the format:
<index> <x_coordinate> <y_coordinate>
Coordinates can be in any numeric range. The algorithm automatically computes the Euclidean distance matrix.
| Parameter | Default | Description | Tuning Guidance |
|---|---|---|---|
cityCount |
20 | Number of cities (auto-detected from file) | Scale antCount and generations accordingly |
antCount |
15 | Number of ants per generation | Typically |
evaporationRate |
0.0008 | Pheromone decay per generation ( |
Lower โ stronger memory; higher โ more exploration |
generations |
160 | Number of optimization iterations ( |
Increase for larger instances |
| Initial pheromone | 0.1 |
|
Should be > 0 to avoid cold-start issues |
| Pheromone floor | Minimum |
Keep small but non-zero |
evaporationRate (ฯ)
Low (0.0001) High (0.01)
โโโโโโโโโโโโ โโโโโโโโโโโโ
โ Strong โ โ Weak โ
โ memory โ โ memory โ
โ Risk: โ โ Risk: โ
โ prematureโ โ slow โ
โ converge โ โ converge โ
โโโโโโโโโโโโ โโโโโโโโโโโโ
โฒ โฒ
โ SWEET SPOT โ
โ ฯ โ 0.0008 โ
โ โโโโโโโโโโโโโโบ โ
โ Balance of โ
โ exploration & โ
โ exploitation โ
โโโโโโโโโโโโโโโโโโโโโโ
The algorithm produces real-time matplotlib visualizations at every
Baseline: unoptimized random permutation of cities
| Component | Time Complexity | Space Complexity |
|---|---|---|
| Distance matrix initialization | ||
| Single ant tour construction | ||
| Pheromone update (per ant) | โ | |
| Pheromone evaporation | โ | |
| Total per generation | ||
| Total algorithm |
Where:
For our configuration:
- Dorigo, M. (1992). Optimization, Learning and Natural Algorithms. PhD thesis, Politecnico di Milano.
- Dorigo, M., & Gambardella, L. M. (1997). Ant colony system: A cooperative learning approach to the traveling salesman problem. IEEE Transactions on Evolutionary Computation, 1(1), 53-66.
- Stรผtzle, T., & Hoos, H. H. (2000). MAXโMIN Ant System. Future Generation Computer Systems, 16(8), 889-914.
- Blum, C. (2005). Ant colony optimization: Introduction and recent trends. Physics of Life Reviews, 2(4), 353-373.
- Garey, M. R., & Johnson, D. S. (1979). Computers and Intractability: A Guide to the Theory of NP-Completeness. W.H. Freeman.
Contributions are welcome. Please read CONTRIBUTING.md for guidelines on how to submit improvements.
This project is licensed under the MIT License โ see the LICENSE file for details.
Built with curiosity and purpose.
Exploring the intersection of biological intelligence and computational optimization.




