##############################################################################
# Author: Daniel Boorn
# Licence: GNU
# Purpose:
#
# The purpose of this script is to use an ANN as a routing algorithm
#
# Methods:
# Neuron Class - class simulates a learning perceptron and is used
# by the Router Class to create a multi-level forward feed ANN.
#
# Router Class - simulates a router that uses a nueral network to determin
# what outbound port a packet takes to reach a destination
#
##############################################################################
#############################################################
# Neuron Class - learning perceptron
#############################################################
class Neuron {
var $filename;
var $inputs;
var $weights;
var $debug = true;
var $learnRate;
var $thold = .5;
function Neuron($size,$filename){
$this->inputs = array();
for($i=0;$i<$size;$i++) $this->inputs[] = 0;
$this->create_weight_matrix();
$this->filename = $filename;
}
function create_weight_matrix(){
$this->weights = array();
foreach($this->inputs as $item)
$this->weights[] = rand(-5,5);
//$this->print_weights();
}
function set_inputs($inputs){
$this->inputs = $inputs;
}
function analize(){
//find sum of the product of weights and inputs
for($i = 0; $i < count($this->inputs); $i++)
$sum += $this->inputs[$i] * $this->weights[$i];
//compare the sum with the thresh hold
return ($sum > $this->thold) ? 1 : 0;
}
function print_weights(){
print "Printing Weight Matrix
";
print_r($this->weights);
print "";
}
function train($lr, $output){
$count = 0;
$result = $this->analize();
while($result != $output){
if($result < $output){
for($i = 0; $i < count($this->inputs); $i++){
$this->weights[$i] = ($this->inputs[$i] == 1) ? $this->weights[$i]+$lr : $this->weights[$i];
}
}
else{
for($i = 0; $i < count($this->inputs); $i++){
$this->weights[$i] = ($this->inputs[$i] == 1) ? $this->weights[$i]-$lr : $this->weights[$i];
}
}
$count++;
$result = $this->analize();
}
//echo "Loops: $count| ';
echo 'Creating new Router F with the following outbound ports { A, B, C, D, E } '; /* Router( avaiable outbound ports ) */ $router = new Router( array("A","B","C","D","E"), "F" ); echo " Sending Router F training data for broadcasting
Router F Said Training is now complete"; echo "
A, B, C, D, E, F, G, H, I
0, 0, 0, 1, 0, 0, 0, 0, 0
^
Packet's Destination is D
";
/* router->get_first_port( packet going to destination on a network, where the destination is 1 */
if( $results = $router->get_ports(array(0,0,0,1,0,0,0,0,0)) ){
echo "Router F sent packet out on the follow port(s):";
foreach($results as $portID)
echo " $portID";
echo "";
}
echo "";
echo " |