AntSys - General Purpose Ant Colony Optimization System
Project description
antsys
A general purpose ant colony optimization system.
Overview
The Ant Colony Optimization (ACO) is a technique, inspired by the foraging behavior of ants, to find good solutions for discrete optimization problems. Its central metaphor resides in the indirect communication mechanism through chemical signals (pheromones) used by many species of social ants in their search for food sources.
The same inspiration was build in the antsys package, wich takes advantage of python flexibility to be easily applied to different optimization problems.
Installation
Installation via pip
pip3 install antsys
Usage Example: Travelling Salesman Problem
The Travelling Salesman Problem (TSP) is the challenge of finding the shortest yet most efficient route for a person to take given a list of specific destinations. It is a well-known optimization problem and commonly solved by ACO algorithm.
- Import necessary packages and modules
from antsys import AntWorld
from antsys import AntSystem
import numpy as np
import random
- Generate a travelling salesman problem instance
# generate cities
print('cities:')
print('| id | x | y |')
cities = []
for city in range(10):
x = random.uniform(-100, 100)
y = random.uniform(-100, 100)
cities.append((city, x, y))
print('|%4i|%9.4f|%9.4f|' % cities[city])
- The function
salesman_rules
will append the euclidean distance between cities to the edges.
def salesman_rules(start, end):
return [((start[1]-end[1])**2+(start[2]-end[2])**2)**0.5]
- The function
salesman_cost
will be used to calculate the cost of any possible solution (path
).
def salesman_cost(path):
cost = 0
for edge in path:
cost+=edge.info
return cost
- The
salesman_heuristic
is a simple heuristic that will help the ants to make better choices. Edges with small distances have a slightly higher probability of selection.
def salesman_heuristic(path, candidate):
return candidate.info
- This function shows the details of a possible solution (
sys_resp
).
def print_solution(sys_resp):
print('total cost = %g' % sys_resp[0])
print('path:')
print('| id | x | y |--distance-->| id | x | y |')
for edge in sys_resp[2]:
print('|%4i|%9.4f|%9.4f|--%8.4f-->|%4i|%9.4f|%9.4f|' %
(edge.start[0], edge.start[1], edge.start[2], edge.info, edge.end[0],
edge.end[1], edge.end[2]))
- The world (
new_world
) is created from the nodes (cities
) as a complete graph. In this point,salesman_rules
,salesman_cost
andsalesman_heuristic
are defined as respectivelyr_func
,c_func
andh_func
. These functions are bound to the world and the first one has an important role in its structure.
new_world = AntWorld(cities, salesman_rules, salesman_cost, salesman_heuristic)
- Configure
ant_opt
as anAntSystem
.
ant_opt = AntSystem(world=new_world, n_ants=50)
- Execute the optimization loop.
ant_opt.optimize(50,20)
- Show details about the best solution found.
print_solution(ant_opt.g_best)
- Examples can be found here as jupyter notebooks.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file antsys-0.1.44.tar.gz
.
File metadata
- Download URL: antsys-0.1.44.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7efd52db916951abf32bcbf317fb387f55095b8a14e4fda1d7a7eee3bfe1561e |
|
MD5 | e5fa24e367b1f617c074280fd37885ad |
|
BLAKE2b-256 | 249b75a11b022fc9306690460468088d9a593418d2c657329449360d6fd87174 |
File details
Details for the file antsys-0.1.44-py3-none-any.whl
.
File metadata
- Download URL: antsys-0.1.44-py3-none-any.whl
- Upload date:
- Size: 20.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4954234d4412e7203655ca0cdeb7c54072bc1010e3058f80378d902de2f067f |
|
MD5 | dfc5712012eba1c05892a5228849148d |
|
BLAKE2b-256 | 585a02fd1acf8fedbec593bbe50f4613bdcc318fa53e7f269d486d9d5ecf365b |