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
"; } 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 '
Simulation of Multi-Level ANN in a Routing Algorithm
'; echo '
'; 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

    "; /* simulate packets going to a destination on a network, where the destination is a 1 */ echo "
  1.         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             
        
  2. "; $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 "
  3. Asking Router F to learn to send packet on outbound port E for both cases above"; echo "
            A, B, C, D, E
            0, 0, 0, 0, 1
                        ^
                  Use outbound port C for destnation D and C
        
  4. "; /* router->train( desired outbout port for simulated packets ) */ $router->train(array(0,0,0,0,1)); echo "

Router F Said Training is now complete

"; echo "
    Sending test packet to Router F with a destination of D"; 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 "
"; ?>