PHP code example of mgrechanik / ant-colony-optimization
1. Go to this page and download the library: Download mgrechanik/ant-colony-optimization library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mgrechanik / ant-colony-optimization example snippets
use mgrechanik\aco\Manager;
use mgrechanik\aco\SppTask;
$task = new SppTask(0, 3);
$manager = new Manager(task : $task);
$matrix = [
[ 0 , 8, 4, 100],
[ 8 , 0, 9, 5 ],
[ 4 , 9, 0, 8 ],
[100, 5, 8, 0 ]
];
$manager->setMatrix($matrix);
$finder = $manager->getFinder();
// increase amount of ants to 6
$finder->setM(6);
$distance = $manager->run(50);
var_dump('Distance=' . $distance);
var_dump($manager->getInnerPath())
Distance=12
Array
(
[0] => 0
[1] => 2
[2] => 3
)
// for comparison, the direct path [0, 3] is closed by big distance and distance of path [0, 1, 3] is 13
use mgrechanik\aco\Manager;
use mgrechanik\aco\City;
$cities = [new City(10,10), new City(50,50), new City(10,50), new City(60,10)];
$manager = new Manager();
$manager->setCities(...$cities);