Sunday, March 24, 2024

Ant Colony Optimization in Motion | by Hennie de Tougher | Sep, 2023

Must read

A working ant. Picture created with Dall-E 2 by the writer.

Fixing optimization issues and enhancing outcomes with ACO in Python

Welcome again! In my earlier publish, I launched the basics of Ant Colony Optimization (ACO). On this installment, we’ll delve into implementing the ACO algorithm from scratch to sort out two distinct drawback varieties.

The issues we’ll be addressing are the Touring Salesman Drawback (TSP) and the Quadratic Task Drawback (QAP). Why these two? Properly, the TSP is a traditional problem, and ACO occurs to be an efficient algorithm for locating essentially the most cost-efficient path by way of a graph. Then again, the Quadratic Task Drawback represents a distinct class of issues associated to optimizing the association of things, and on this publish, I purpose to show that ACO is usually a precious software for fixing such assignment-related issues as effectively. This versatility makes the ACO algorithm relevant to a variety of issues. Lastly, I’ll share some ideas for attaining improved options extra quickly.

TSP is easy to explain however can pose a big problem to find an answer. Right here’s the fundamental definition: you’re tasked with discovering the shortest route that visits all nodes in a graph. This drawback falls into the class of NP-hard issues, which means that in the event you try and discover all doable routes, it may take an impractical period of time to search out the answer. As a substitute, a more practical strategy is to hunt a high-quality answer inside an affordable timeframe, and that’s exactly what we’ll accomplish utilizing ACO.

Drawback Definition

With the next code, we are able to create a TSP occasion with a given variety of nodes:

import itertools
import math
import random
from typing import Tuple

import networkx as nx
import networkx.algorithms.shortest_paths.dense as nxalg

class TSP:
Creates a TSP drawback with a sure variety of nodes
def __init__(self, nodes: int = 30, dimensions: Tuple[int, int] = (1000, 1000), seed: int = 5):
if seed:

graph = nx.Graph()
nodes_dict = dict()

for i in vary(nodes):
nodes_dict[i] =…

Supply hyperlink

More articles


Please enter your comment!
Please enter your name here

Latest article