Location: PHPKode > scripts > ANN Router > neuron.php
<?
#################################################################################################
# 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<br><pre>";
				print_r($this->weights);
				print "</pre>";
		}		
		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<br>";
			
				
		}
		function save(){
			$fp = fopen($this->filename,"w+");
			if( fwrite($fp,serialize($this->weights)) )
				return true;
			else
				return false;
		}
		function load(){
			if (file_exists($this->filename) ) {
				$f = fopen($this->filename, "r");
				$serialised = fread($f, filesize($this->filename) );
				fclose($f);
				
				if ($serialised < 0) {
					return null;
				}
				$this->weights = unserialize($serialised);
			}
			return null;
		}	
			
	}
	############################################################################
	# Router Class - simulates a router that uses a nueral network to determin
	#				 what outbound port a packet takes to reach a destination
	############################################################################
	class Router{
		var $id;
		var $ports;
		var $portOutputs;
		var $inputs;
		var $neuron;
		
		//Constructor
		function Router($ports, $id){
			$this->id = $id;
			$this->ports = $ports;
			$this->inputs = array();
			$this->neuron = array();
		}
		function set_inputs($inputs){ $this->inputs = $inputs; }
		
		function train($portOutputs){
			$this->portOutputs = $portOutputs;
			//for each port
			for($i = 0; $i < count($this->ports); $i++){
				//create new neuron with port id, and input size
				$this->neuron[$this->ports[$i]] = new Neuron(4,$this->id . $this->ports[$i].".dat");
				//for each input train
				for($k = 0; $k < count($this->inputs); $k++){
					$this->neuron[$this->ports[$i]]->set_inputs($this->inputs[$k]);
					$this->neuron[$this->ports[$i]]->train(.5,$this->portOutputs[$i]);
				}
				$this->neuron[$this->ports[$i]]->save();
			}
		}
		
		function get_ports($inputs){
			foreach($this->neuron as $key => $value){
				$value->set_inputs($inputs);
				$result = $value->analize();
				if($result == 1) //port found
					$ports[] = $key;
			}
			if(count($ports) > 0)
				return $ports;
			else
			//else no port found
			return false;
		}
	}
	#__________END_OF_ROUTER_CLASS__________________________________________________________________________________
		
	
	//echo use info
	echo '<div align=center><font size=5 color=blue>Simulation of Multi-Level ANN in a Routing Algorithm</font></div>';
	echo '<table cellpadding=5 cellspacing=0 border=1 bordercolor=black align=center><tr><td>';
	echo '<font color=orange size=4>Creating new Router F with the following outbound ports { A, B, C, D, E }</font><hr size=1 color=blue>';
	
	/* Router( avaiable outbound ports ) */
	$router = new Router( array("A","B","C","D","E"), "F" );
	
	echo "<h4>Sending Router F training data for broadcasting</h4><ol>";
	/* simulate packets going to a destination on a network, where the destination is a 1 */
	echo "<li><pre>
		A, B, C, D, E, F, G, H, I 
		0, 0, 0, 1, 0, 0, 0, 0, 0
		         ^
			 Simulation Packet's Destination is D

		A, B, C, D, E, F, G, H, I 
		0, 0, 1, 0, 0, 0, 0, 0, 0
		      ^   
			 Simulation Packet's Destination is C			 
	</pre></li>";
	$inputs = array(
				array(0,0,0,1,0,0,0,0,0),
				array(0,0,1,0,0,0,0,0,0)
			);
	$router->set_inputs($inputs);		
	
	echo "<li><i>Asking Router F to learn to send packet on outbound port E for both cases above</i>";
	echo "<pre>
		A, B, C, D, E
		0, 0, 0, 0, 1
		            ^
			  Use outbound port C for destnation D and C
	</pre></li>";
	/* router->train( desired outbout port for simulated packets ) */
	$router->train(array(0,0,0,0,1));
	
	echo "</ol><h4>Router F Said Training is now complete</h4>";
	
	echo "<ul><i>Sending test packet to Router F with a destination of D</i>";
	echo "<pre>
		A, B, C, D, E, F, G, H, I 
		0, 0, 0, 1, 0, 0, 0, 0, 0
		         ^
			Packet's Destination is D
		</pre>";
	/* 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 "<i>Router F sent packet out on the follow port(s):</i><b>";
		foreach($results as $portID)
			echo " $portID";
		echo "</b>";
	}
	
	echo "</ul>";
	echo "</td></tr></table>";
	
	
	
	

?>
	
Return current item: ANN Router