Download the PHP package mgrechanik/ant-colony-optimization without Composer
On this page you can find all versions of the php package mgrechanik/ant-colony-optimization. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mgrechanik/ant-colony-optimization
More information about mgrechanik/ant-colony-optimization
Files in mgrechanik/ant-colony-optimization
Package ant-colony-optimization
Short Description The implementation of the Ant colony optimization algorithm
License BSD-3-Clause
Informations about the package ant-colony-optimization
Ant colony optimization
Русская версия
Table of contents
- Introdution
- Demo
- Installing
- How to use
- Settings
- Performance
- TSPLIB95
- Terminology
Introdution
The Ant colony optimization is a probabilistic technique for solving computational problems which can be reduced to finding good paths through graphs (from Wikipedia).
The task we are solving could be either "Shortest path problem", or Constrained Shortest Path First, etc.
The two first tasks are solved within this library.
There are a lot of strategies and variations of Classic ACO algorithm.
This library out of the box implements Classic ACO and ACO with elitist ants.
The library could be easily extended so you can implement your ACO variations and to solve the tasks you need.
The initial data about the graph comes either from adjacency matrix or from a list of nodes (cities, vertices, etc) with their X and Y coordinates.
The work of library had been tested with performance and efficiency.
Amount of ants, all coefficients and parameters could be changed to your need.
Now we have a web app which you can install and run locally to practice with this algorithm. Look for it's video demo for details.
Demo
Solving the travelling salesman problem with the ant colony optimization algorithm:
Another example:
Installing
Installing through composer::
The preferred way to install this library is through composer.
Either run
or add
to the require section of your composer.json
.
How to use
Basic API
1) Creating a Manager with the dependencies we need
- By default Finder will be Classic one, and the Task will be the Travelling salesman problem
2) Loading data from an adjacency matrix
- - from which number start naming aliases of nodes
3) Loading data from an array of cities
- This array of cities will be transformed to adjacency matrix. Distances will be calculated according to the strategy we set to a Manager.
- If city has a property it will become it's name alias
4) Changing of the adjacency matrix
- For example we could make some path impassable -
5) Run the computational process
- for small graphs we could reduce amount of iterations.
- It will return the distance we found or when the search gave no result
6) Getting the path we found
- The path we found who consists of node's numbers.
All nodes are internally named as numbers from 0 to N-1, where N is node's amount.
7) Getting aliased path we found
- The path we found which consists of node's name aliases, if we set them.
Examples
Solving "Travelling salesman problem" with Classic ACO
We will get:
Solving "Shortest path problem" with Classic ACO
We will get:
Loading data as an array of cities
Loading data as an adjacency matrix
Using the Elitist Finder
Have a look at the history of our work - best solutions we had been finding
Loading a list of cities from an image file
With the use of this library we can load a list of cities from the image. And the result of the search could be displayed on the image too. It will look like images on Demo.
Read docs of that library for more information how to prepare images but briefly it is this: On white canvas draw points of 10 px diameter (they are vertices of the graph) and use this image with the code below
Settings
Finder settings
The base object we tune is the Finder.
Lets get it:
Settings available:
-
Set the distance value which makes the path between two nodes impassable
-
Set amount of ants
-
Set amount of ants in percents relatively to amount of nodes. Default behavior (=40%)
-
Set the coefficients for formulas
-
Set the strategy to do the mathematical work
-
Set the task we are solving. Say TSP, SPP or other.
-
Set an amount of elitist ants (when we use Elitist Finder)
- Set an amount of elitist ants in percents relatively to amount of regular ants. Default behavior (=50%) (when we use Elitist Finder)
Performance
First of all turn off XDebug or it's analogies, since they could significantly affect the time the algorithm works
This ACO algorithm finds good paths on a graph. And sometimes even best paths.
Lets take, for example, the task from TSPLIB95 library, which has 52 nodes.
Solving this task with the next code:
We will see:
This code, working on an office computer, found the best path ever known for less than 2 seconds.
We used here Elitist ACO algorithm since in practice it gives better results than Classic one.
An Algorithm is probabilistic, ants travel differently each new search. A lot depends upon amount of nodes, amount of ants, all coefficients and parameters used with formulas.
TSPLIB95
The TSPLIB95 library ships with a lot of - initial data and solutions - best results ever found for these tasks (distances).
The library is valuable that with it's data we could test the efficiency of our algorithms, coefficients and parameters.
The library consists of a lot of different initial data formats. Out of the box we support two of them.
Loading data as a set of X and Y coordinates of cities. Distance is euclidean.
Example of the file with this format - berlin52.tsp .
Loading the list of nodes (cities) and transfer it to Manager:
Loading data as an adjacency matrix
Example of the file with this format - bays29.tsp .
Loading the adjacency matrix and transfer it to Manager:
Terminology
- Ant colony optimization algorithm
- Nodes or vertices or cities. Ants travel between them.
- Adjacency Matrix sets the distances between graph nodes. It is a basic structure our algorithm starts work with.
When graph is loaded like with their coordinates, this information will be converted to Adjacency Matrix