Skip to content

Ant colony optimization on a road network

Ants that find the shortest path between two points of a Delaunay road network.

  • Class: AntColonyOptimizationSystem
  • Import: from skcomplex.systems.biology.collective_behaviour import AntColonyOptimizationSystem
  • Field: biology/collective_behaviour
  • Topics: ant colony optimization, stigmergy, shortest path, agents
  • References:
    • Dorigo 1992
    • Argun et al. 2021

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.biology.collective_behaviour import AntColonyOptimizationSystem

trajectory = AntColonyOptimizationSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
n_points 40
ants 20
steps 80
alpha 0.8
beta 1.0
rho 0.5
Q 1.0
cut None
seed 0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time discrete
Space none
Laws update
Randomness stochastic
Domains boolean, real
Structure variable membership

Class

The source of the class shows how it builds its components from its model parameters.

class AntColonyOptimizationSystem(BaseSystem):
    """Ants that find the shortest path between two points of a Delaunay road network.

    The ants search round after round.

    Every round the ants walk from the point nearest one corner of the unit square towards the point nearest the
    opposite corner, choosing segments by their pheromone and length, and mark the paths they find with pheromone in
    inverse proportion to their length, while all pheromone evaporates. Short paths gather pheromone fastest, so the
    colony converges on a short path, usually the shortest, as in exercise 15.7 of Argun et al. (2021). Cutting the best
    path at the round `cut` makes the colony explore again and settle on a new route, exercise 15.12. The system has the
    pheromone `roads.tau` and the open segments `roads.passable` of the agent group `roads`, and the shortest path of
    every round `search.shortest`.

    Args:
        n_points:
            The number of points of the network.

        ants:
            The number of ants of a round.

        steps:
            The largest number of steps of an ant in a round.

        alpha:
            The exponent of the pheromone.

        beta:
            The exponent of the weight `1 / d` of a segment.

        rho:
            The evaporation rate.

        Q:
            The pheromone an ant lays along a path.

        cut:
            The round at which the best path is cut, or `None` for none.

        seed:
            The seed of the points.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> from skcomplex.systems.biology.collective_behaviour import AntColonyOptimizationSystem
        >>> AntColonyOptimizationSystem(10).prepare().system_.interactions[0].ants
        20
    """

    field = 'biology/collective_behaviour'
    topics = ('ant colony optimization', 'stigmergy', 'shortest path', 'agents')
    references = ('Dorigo 1992', 'Argun et al. 2021')
    until = 100

    def __init__(
        self,
        n_points: int = 40,
        *,
        ants: int = 20,
        steps: int = 80,
        alpha: float = 0.8,
        beta: float = 1.0,
        rho: float = 0.5,
        Q: float = 1.0,
        cut: int | None = None,
        seed: int | None = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n_points = n_points
        self.ants = ants
        self.steps = steps
        self.alpha = alpha
        self.beta = beta
        self.rho = rho
        self.Q = Q
        self.cut = cut
        self.seed = seed

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of ants that find the shortest path on a Delaunay road network.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        points = np.random.default_rng(self.seed).random((self.n_points, 2))
        triangles = Delaunay(points).simplices
        pairs = {
            (int(min(a, b)), int(max(a, b))) for triangle in triangles for a, b in itertools.combinations(triangle, 2)
        }
        edges = sorted(pairs)
        lengths = [float(np.linalg.norm(points[a] - points[b])) for a, b in edges]
        start = int(np.argmin(np.linalg.norm(points, axis=1)))
        target = int(np.argmin(np.linalg.norm(points - 1.0, axis=1)))
        roads = TrailSegmentElement('roads', size=len(edges))
        search = ColonySearchInteraction(
            'search',
            segments='roads',
            edges=tuple(edges),
            lengths=tuple(lengths),
            start=start,
            target=target,
            ants=self.ants,
            steps=self.steps,
            alpha=self.alpha,
            beta=self.beta,
            rho=self.rho,
            Q=self.Q,
            cut=self.cut,
        )
        self.system_ = System(self.label, elements=[roads], interactions=[search])

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

  • TrailSegmentElement, from Element

Interactions:

  • ColonySearchInteraction, from Interaction
class TrailSegmentElement(Element):
    """Segment of a road network between two points.

    The segment holds the pheromone ants left on it and whether it is open.

    Args:
        label:
            The label of the segment or of the group of segments.

        tau:
            The pheromone level.

        passable:
            1 while the segment can be walked and 0 once it is cut.

        size:
            The number of members, or `None` for one.

    Examples:
        >>> from skcomplex.systems.biology.collective_behaviour import TrailSegmentElement
        >>> TrailSegmentElement('segment').tau
        1.0
    """

    def __init__(
        self,
        label: str,
        *,
        tau: Annotated[float | Array, RealSet.closed_open(0, oo)] = 1.0,
        passable: Annotated[float | Array, FiniteEnumeratedSet([0, 1])] = 1.0,
        size: int | None = None,
    ) -> None:
        super().__init__(label, size=size)
        self.tau = tau
        self.passable = passable

class ColonySearchInteraction(Interaction):
    """Rounds of ants that walk a road network by pheromone and length.

    The ants mark the paths they find, and the pheromone fades.

    In every round, following Argun et al. (2021, section 15.2), `ants` ants start at the vertex `start` and take up to
    `steps` steps. At vertex `i` an ant moves to the neighbour `j` with the probability `tau_ij^alpha w_ij^beta / sum_k
    tau_ik^alpha w_ik^beta`, with the weight `w_ij = 1 / d_ij` of the length `d_ij`, Eq. (15.2), and stops at the vertex
    `target`. The path of every ant that arrives is cleared of its loops, and every segment of it receives the pheromone
    `Q / L` of its length `L`, Eq. (15.3), after the pheromone of every segment has evaporated by the factor `1 - rho`,
    Eq. (15.4). At the round `cut`, two segments near the middle of the shortest path found so far are cut, as long as
    the network stays connected, and the colony must heal the disruption. The interaction reports the length of the
    shortest path of every round as `shortest`.

    Args:
        label:
            The label of the interaction.

        segments:
            The path of the group of segments.

        edges:
            The two ends of every segment, in the order of the group.

        lengths:
            The length of every segment.

        start:
            The vertex the ants start from.

        target:
            The vertex the ants look for.

        ants:
            The number of ants of a round.

        steps:
            The largest number of steps of an ant in a round.

        alpha:
            The exponent of the pheromone.

        beta:
            The exponent of the weight.

        rho:
            The evaporation rate.

        Q:
            The pheromone an ant lays along a path.

        cut:
            The round at which the shortest path found is cut, or `None` for none.

    Examples:
        >>> from skcomplex.systems.biology.collective_behaviour import ColonySearchInteraction
        >>> ColonySearchInteraction('search', segments='roads', edges=((0, 1),), lengths=(1.0,), target=1).ants
        20
    """

    draws = True

    law_classes = (UpdateLaw,)

    reports: ClassVar[tuple[str, ...]] = ('shortest',)

    def __init__(
        self,
        label: str,
        *,
        segments: Role,
        edges: Sequence[tuple[int, int]] = (),
        lengths: Sequence[float] = (),
        start: int = 0,
        target: int = 1,
        ants: int = 20,
        steps: int = 80,
        alpha: float = 0.8,
        beta: float = 1.0,
        rho: float = 0.5,
        Q: float = 1.0,
        cut: int | None = None,
    ) -> None:
        super().__init__(label)
        self.segments = segments
        self.edges = edges
        self.lengths = lengths
        self.start = start
        self.target = target
        self.ants = ants
        self.steps = steps
        self.alpha = alpha
        self.beta = beta
        self.rho = rho
        self.Q = Q
        self.cut = cut

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the rule of a round.

        Args:
            context:
                The namespace of the symbols the laws can use.
        """
        super()._prepare(context)
        self.laws_: list[BaseLaw] = [UpdateLaw(context, self.search)]

    def search(self, system: SystemView) -> None:
        """Run one round of ants and update the pheromone.

        At the round `cut`, the round also cuts the best path.

        Args:
            system:
                The system.
        """
        segments = cast(list[TrailSegmentView], system.get_agents(self.segments))
        tau = np.array([item.tau for item in segments])
        is_open = np.array([item.passable for item in segments]) > 0
        best = system.storage.get('best')
        if (
            self.cut is not None
            and system.time >= self.cut
            and isinstance(best, list)
            and not system.storage.get('cut')
        ):
            is_open = self._disrupt(is_open, best)
            tau[~is_open] = 0.0
            system.storage['cut'] = True
        paths = [self._walk(tau, is_open, system.rng) for _ in range(self.ants)]
        found = [path for path in paths if path]
        lengths = np.asarray(self.lengths, dtype=float)
        deposit = np.zeros_like(tau)
        shortest = np.inf
        for path in found:
            total = float(lengths[path].sum())
            deposit[path] += self.Q / total
            if total < shortest:
                shortest = total
                system.storage['best'] = path
        tau = ((1 - self.rho) * tau + deposit) * is_open
        for item, level, state in zip(segments, tau, is_open, strict=True):
            item.tau = float(level)
            item.passable = float(state)
        system.report('shortest', shortest if np.isfinite(shortest) else float('nan'))

    def _walk(self, tau: Array, is_open: npt.NDArray[np.bool_], rng: np.random.Generator) -> list[int]:
        """Return the segments of the loop-free path of one ant to the target.

        The path has no segment if the ant does not arrive.
        """
        neighbours = self._get_neighbours()
        lengths = np.asarray(self.lengths, dtype=float)
        vertex, visited = self.start, [self.start]
        for _ in range(self.steps):
            options = [(other, edge) for other, edge in neighbours[vertex] if is_open[edge]]
            if not options:
                return []
            edges = np.array([edge for _, edge in options])
            scores = tau[edges] ** self.alpha * (1 / lengths[edges]) ** self.beta + 1e-12
            vertex = options[int(rng.choice(len(options), p=scores / scores.sum()))][0]
            if vertex in visited:
                visited = visited[: visited.index(vertex) + 1]
            else:
                visited.append(vertex)
            if vertex == self.target:
                index = self._get_index()
                return [index[frozenset(pair)] for pair in itertools.pairwise(visited)]
        return []

    def _disrupt(self, is_open: npt.NDArray[np.bool_], best: list[int]) -> npt.NDArray[np.bool_]:
        """Return the open segments after cutting two segments of the best path.

        The two segments lie in the middle of the best path, and the cut keeps the network connected.
        """
        cut = is_open.copy()
        middle = len(best) // 2
        removed = 0
        for edge in best[middle:] + best[:middle]:
            if removed == CUTS:
                break
            trial = cut.copy()
            trial[edge] = False
            graph = nx.Graph([pair for pair, keep in zip(self.edges, trial, strict=True) if keep])
            if (
                graph.has_node(self.start)
                and graph.has_node(self.target)
                and nx.has_path(graph, self.start, self.target)
            ):
                cut = trial
                removed += 1
        return cut

    def _get_neighbours(self) -> dict[int, list[tuple[int, int]]]:
        """Return the neighbours of every vertex with the index of the joining segment."""
        neighbours: dict[int, list[tuple[int, int]]] = {}
        for index, (first, second) in enumerate(self.edges):
            neighbours.setdefault(first, []).append((second, index))
            neighbours.setdefault(second, []).append((first, index))
        return neighbours

    def _get_index(self) -> dict[frozenset[int], int]:
        """Return the index of the segment between every pair of vertices."""
        return {frozenset(pair): index for index, pair in enumerate(self.edges)}

Equations

The equations are those that symbolic returns for the system with its default parameters.