Ant Colony Optimization (ACO) applied to five problems — four discrete and one continuous — built on one shared engine.
Ant Colony Optimization is a metaheuristic based on the foraging behaviour of ants. Many agents ("ants") build solutions step by step. They leave a chemical trail (pheromone) on the choices they make. Over iterations the colony concentrates on the lower-cost regions of the search space. This repository solves four discrete problems and one continuous problem with the same core engine.
Each ant builds a solution one component at a time. At every step it picks the next component with a probability that mixes two signals:
-
Pheromone
$\tau$ — how good this choice was for past ants (learned). -
Heuristic
$\eta$ — a fixed problem hint, such as "closer cities are better".
After all ants finish an iteration, the pheromone is updated. Good (low-cost) solutions add pheromone; then every trail evaporates a little so old choices fade:
where
The continuous problem (sphere) uses ACO_R (Socha & Dorigo, 2008), which replaces the pheromone matrix with a ranked archive of solutions and samples new points from Gaussian kernels. See its README.
| Problem | Type | Instance | Pheromone structure | Heuristic |
Local search | Reference result |
|---|---|---|---|---|---|---|
| Traveling Salesman | permutation | 50 cities |
|
2-opt | tour length 588.97 | |
| Quadratic Assignment | permutation | 20 locations, 10 facilities |
|
— | cost 22 669 | |
| Binary Knapsack | binary 0/1 | 30 items |
|
value/weight | — | value 2941, feasible |
| Integer Knapsack | bounded integer | 30 items | per-item arrays | value/weight | — | value 19 240, feasible |
| Sphere (ACO_R) | continuous | 5 dimensions | Gaussian archive | — | — | cost |
All reference results above are produced by the default seed (--seed 1).
TSP — best tour![]() |
TSP — convergence![]() |
QAP — facility assignment![]() |
Sphere (ACO_R) — convergence![]() |
The sphere curve uses a logarithmic axis: ACO_R drives the cost down by hundreds of orders of magnitude, reaching the global optimum at the origin.
Requires Python 3.10 or newer.
git clone https://github.com/hajibabaie/ant_colony_optimization.git
cd ant_colony_optimization
pip install -e . # add ".[dev]" to also install pytest and ruffThe editable install puts the aco package on your path and registers the
command-line tools below, so everything runs from any directory.
Each problem has a console command and an equivalent module entry point:
| Problem | Console command | Module form |
|---|---|---|
| Traveling Salesman | aco-tsp |
python -m aco.problems.tsp |
| Quadratic Assignment | aco-qap |
python -m aco.problems.qap |
| Binary Knapsack | aco-binary-knapsack |
python -m aco.problems.binary_knapsack |
| Integer Knapsack | aco-integer-knapsack |
python -m aco.problems.integer_knapsack |
| Sphere (ACO_R) | aco-sphere |
python -m aco.problems.sphere |
Common options: --seed (reproducibility), --iterations, and --no-plot.
aco-tsp # solve the default 50-city instance, save figures
aco-tsp --seed 7 # a different run
aco-sphere --dim 10 # minimize the 10-dimensional sphereFigures are written to results/<problem>/.
aco/
├── core/ # the shared engine (no problem code here)
│ ├── base.py # DiscreteACO: the template-method run loop
│ ├── continuous.py # ContinuousACO: the ACO_R archive engine
│ ├── selection.py # roulette-wheel selection (shared)
│ └── plotting.py # convergence figure (shared)
└── problems/ # one package per problem
├── tsp/ qap/ binary_knapsack/ integer_knapsack/ sphere/
│ ├── instance.py # data model (load / random / save)
│ ├── cost.py # objective function
│ ├── solver.py # the five hooks that specialize the engine
│ └── cli.py # command-line entry point
tests/ # pytest suite
results/ # generated figures (committed for the README)
docs/references/ # the ACO_R paper
The four discrete problems share one run loop. Each problem only implements how to build a solution, score it, and deposit/evaporate pheromone — the iteration order, best-tracking, timing, and plotting live once in the base class.
- Template method for the discrete engine.
DiscreteACOfixes the loop and exposes four required hooks (construct_solution,evaluate,deposit,evaporate) plus two optional ones (local_search,plot_solution). Adding a new discrete problem means writing one small solver class, not a new loop. - ACO_R is a separate engine. The continuous algorithm has a different shape (a ranked archive and Gaussian sampling instead of a pheromone matrix), so it is its own class rather than a forced subclass with empty methods.
- Reproducible runs. Every engine takes a
numpy.random.Generator, so a seed reproduces a run exactly. This also makes the test suite deterministic. - Data is loaded once. Each problem reads its instance into a small dataclass
and the cost function is a pure function of
(solution, instance)— no file is read during the search.
The engines use the standard ACO symbols.
| Symbol | Name | Meaning |
|---|---|---|
alpha |
pheromone exponent (weight of learned trails) | |
beta |
heuristic exponent (weight of the fixed hint) | |
rho |
evaporation rate | |
q |
pheromone deposit constant |
Values used by each discrete problem:
| Problem | ants | iterations | ||||
|---|---|---|---|---|---|---|
| Traveling Salesman | 150 | 100 | 2.7 | 1.2 | 0.04 | 1 |
| Quadratic Assignment | 40 | 100 | 2.7 | 1.02 | 0.04 | 1 |
| Binary Knapsack | 200 | 100 | 2.0 | 1.5 | 0.04 | 1 |
| Integer Knapsack | 120 | 100 | 3.2 | 1.2 | 0.04 | 1 |
ACO_R (sphere) uses an archive of 20, 50 samples per iteration, intensification
- M. Dorigo, V. Maniezzo, A. Colorni (1996). Ant System: optimization by a colony of cooperating agents. IEEE Transactions on Systems, Man, and Cybernetics, Part B, 26(1), 29–41.
- M. Dorigo, T. Stützle (2004). Ant Colony Optimization. MIT Press.
- K. Socha, M. Dorigo (2008). Ant colony optimization for continuous domains. European Journal of Operational Research, 185(3), 1155–1173. (local copy)
Released under the MIT License.



