1

My goal is to get the shortest path and its length between two nodes in a graph using Ant Colony Algorithm.

Let say, we have a graph like this:

example graph

$graph = array(
    array(0,5,7,3,0),
    array(5,0,4,0,0),
    array(7,4,0,0,5),
    array(3,0,0,0,4),
    array(0,0,5,4,0),
);

And I want to find the shortest path from node 0 to 5

$start = 0;
$finish = 5;

The goal output:

$shortest = [0, 4, 5]; // shortest path
$length = 7; // the path length

What I have done:

I created Ant Class, but I can't finish it.

<?php
// file: Ant.php
namespace Algoritma;

/**********************
 | @AUTHOR: Ardianta Pargo <ardianta_pargo@yahoo.co.id>
 | @DATE: 22 Juli 2017
*/

class Ant {

    // the graph
    private $graf = array(array());

    // probability between cities/nodes
    private $probabilitas = array(array());

    // visited nodes
    private $visitedNode = array();

    // start node and finish node
    private $start;
    private $finish;

    // current position of ant
    private $currentPosition;

    // tour length of ant
    private $tourLength;

    public function __construct($start, $finish, $graf){
        $this->graf = $graf;
        $this->start = $start;
        $this->finish = $finish;
        $this->currentPosition = $start;
    }

    public function depositFeromon(){
        //...
    }

    public function move(){
        //...
        echo "Ant is moving...<br>";

        // @TODO calculate probabality to city
        // @TODO take random city by probability
        // @TODO update current position
        // @TODO call deposit pheromone
    }

    // Setter dan getter
    public function setStart($start){
        $this->start = $start;
    }

    public function getStart(){
        return $this->start;
    }

    public function setFinish($finish){
        $this->finish = $finish;
    }

    public function getFinish(){
        return $this->finish;
    }

    public function setCurrentPosition($position){
        $this->currentPosition = $position;
    }

    public function getCurrentPosition(){
        return $this->currentPosition;
    }

}

I am also create another file to test it class:

<?php

include("Ant.php");
use Algoritma\Ant;

// the graph in matrix
$graf = array(
    array(0,5,7,3,0),
    array(5,0,4,0,0),
    array(7,4,0,0,5),
    array(3,0,0,0,4),
    array(0,0,5,4,0),
);

// initial pheromone
$t0 = 0.01;

// pheromone array
$feromon = array(array());

// cities/nodes count
$n = count($graf);

// where to start and finish
$start = 0; // A
$finish = 5; // E

// tetapan siklus semut
$Q = 1;

// tetapan pengendali intensitas jejak semut
$alpha = 1.00;

// tetapan pengendali visibilitas
$beta = 1.00;

// tetapan penguapan jejak semut
$rho = 0.50;

// visibility
$visibilitas = array(array());

// ants count
$m = 4;

// jumlah siklus maksimal
$NCmax = 2;
$NC = 0;

// ------------------- INITIALIZE -----------------------------


for($i = 0; $i < $n; $i++){
    for($j = 0; $j < $n; $j++){

        // initialize the pheromone
        $feromon[$i][$j] = $t0;

        if($graf[$i][$j] == 0){
            $visibilitas[$i][$j] = 0;
        } else {
            // initialize the visibility
            $visibilitas[$i][$j] = 1 / $graf[$i][$j];
        }

    }
}

do {
    // create ant object 
    $semut[] = new Ant($start, $finish, $graf);
} while ( count($semut) < $m);


// ------------------- ALGORITHM PROCESS -----------------------------



while($NC < $NCmax){

    // each ant now start moving...
    foreach($semut as $ant){
        $ant->move();
    }

    $NC++;
}

var_dump($visibilitas);

I don't know what to do next.

Many examples on the internet using TSP.

Dian
  • 470
  • 1
  • 6
  • 20
  • 1. You need to save the pheromone concentration tau for each edge of the graph. 2. Initialize the concentrations with a big value and let them evaporate or they will all follow the first ant. 3. You don't need beta if you don't have any a priori knowledge (attractiveness) eta 4. Choose evaporation factor rho < 1, e.g. 0.99. 5. The quality of the solution is the path length, so you could deposit const/length pheromones on each edge in the solution. – maraca Aug 22 '17 at 20:41
  • Looks like your adjacency matrix is intended for 5 nodes, so you probably want to find the path between node 0 and 4, or 1 and 5, right? – localheinz Aug 22 '17 at 22:38
  • @localheinz yes, you are right. Actually I want to implement this algorithm on 39 nodes. I have done using Floyd Warshall [demo here](http://ruwi.herokuapp.com). – Dian Aug 23 '17 at 00:52

0 Answers0