Skip to content

Ant System (AS)

The first ant colony algorithm, for discrete problems such as tours. It is mainly a baseline: ACS and MMAS usually do better.

AntSystem

AntSystem(*, beta=5.0, rho=0.5, elitist=None, rank=None, deposit_scale=1.0, **kwargs)

Ant System (AS), with the elitist (EAS) and rank-based (AS_rank) variants.

Every ant deposits Q / f on its choices after evaporation. elitist=e adds e Q / f_best on the best so far solution (EAS), and rank=w lets only the w - 1 best ants of the colony deposit, weighted w - r by their rank r, plus w on the best so far (AS_rank). The start trail is m / f_nn, (e + m) / (rho f_nn) or w (w - 1) / (2 rho f_nn) (Box 3.1 of the book), where f_nn is the value of the greedy solution.

Parameters

  • beta: heuristic weight (default 5, the best value of the 1996 paper)
  • rho: evaporation rate (default 0.5)
  • elitist: None (default), an int e, or "n" (e = the number of choices per ant, the book)
  • rank: None (default) or an int w >= 2 (the book uses w = 6 with rho 0.1)
  • deposit_scale: Q (default 1, the book. The 1996 paper used 100 with a small constant tau0)

References: M. Dorigo, V. Maniezzo and A. Colorni, "Ant System: optimization by a colony of cooperating agents", IEEE Transactions on Systems, Man, and Cybernetics B 26(1), 1996, 29 to 41. M. Dorigo and T. Stützle, "Ant Colony Optimization", MIT Press, 2004, Chapter 3.

ypea.AS is the same class. elitist= gives Elitist AS and rank= gives rank-based AS. The settings shared by the three ant colony classes, and how each variable type is built, are on Ant colony optimization.

Example

The heuristic makes a large difference on tours: without it the ants learn from the pheromone alone.

import numpy as np
import ypea

problem = ypea.problems.random_tsp(50, seed=2)
distances = problem.objective.distances
n = len(distances)
heuristic = {"tour": 1 / (distances + np.eye(n))}  # the diagonal is never used

alg = ypea.AntSystem(heuristic=heuristic, max_nfe=50_000)
result = alg.solve(problem, seed=1)
print(result.summary())
AS on random_tsp50
  best objective value: 598.431
  feasible: True (violation 0)
  evaluations: 50000, iterations: 1000, time: 5.75 s, stopped by: max_nfe

Differs from Python: MATLAB draws its own cities and random numbers, so the tour length differs from Python's. The heuristic is a struct keyed by the variable name.

problem = ypea.problems.random_tsp(50, seed=2);
distances = problem.info.distances;
n = size(distances, 1);
heuristic = struct("tour", 1 ./ (distances + eye(n)));  % the diagonal is never used

alg = ypea.AntSystem(heuristic=heuristic, max_nfe=50000);
result = alg.solve(problem, seed=1);
disp(result.summary())
AS on random_tsp50
  best objective value: 559.549
  feasible: true (violation 0)
  evaluations: 50000, iterations: 1000, time: 1.9 s, stopped by: max_nfe