NeuralNetwork
  • Class
  • Tree

Classes

  • NetworkGraph
  • NeuralNetwork
  1 <?php
  2 /**
  3  * <b>Network Graph</b>
  4  *
  5  * For updates and changes visit the project page at http://ann.thwien.de/
  6  *
  7  *
  8  *
  9  * <b>LICENCE</b>
 10  *
 11  * The BSD 2-Clause License
 12  *
 13  * http://opensource.org/licenses/bsd-license.php
 14  *
 15  * Copyright (c) 2007 - 2012, Thomas Wien
 16  * All rights reserved.
 17  *
 18  * Redistribution and use in source and binary forms, with or without
 19  * modification, are permitted provided that the following conditions
 20  * are met:
 21  *
 22  * 1. Redistributions of source code must retain the above copyright
 23  * notice, this list of conditions and the following disclaimer.
 24  *
 25  * 2. Redistributions in binary form must reproduce the above copyright
 26  * notice, this list of conditions and the following disclaimer in the
 27  * documentation and/or other materials provided with the distribution.
 28  *
 29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 32  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 33  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 35  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 36  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 37  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 39  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 40  * POSSIBILITY OF SUCH DAMAGE.
 41  *
 42  * @author Thomas Wien <info_at_thwien_dot_de>
 43  * @version ANN Version 2.3 by Thomas Wien
 44  * @copyright Copyright (c) 2007-2012 by Thomas Wien
 45  */
 46 class NetworkGraph
 47 {
 48     /**#@+
 49      * @ignore
 50      */
 51 
 52     /**
 53      * @var integer
 54      */
 55     protected $intNumberInputs;
 56 
 57     /**
 58      * @var integer
 59      */
 60     protected $intNumberHiddenLayers;
 61 
 62     /**
 63      * @var integer
 64      */
 65     protected $intNumberNeuronsOfHiddenLayer;
 66 
 67     /**
 68      * @var integer
 69      */
 70     protected $intNumberOfOutputs;
 71 
 72     /**
 73      * @var resource
 74      */
 75     protected $handleImage;
 76 
 77     /**
 78      * @var resource
 79      */
 80     protected $handleColorNeuronInput;
 81 
 82     /**
 83      * @var resource
 84      */
 85     protected $handleColorNeuronHidden;
 86 
 87     /**
 88      * @var resource
 89      */
 90     protected $handleColorNeuronOutput;
 91 
 92     /**
 93      * @var resource
 94      */
 95     protected $handleColorNeuronBorder;
 96 
 97     /**
 98      * @var resource
 99      */
100     protected $handleColorBackground;
101 
102     /**
103      * @var resource
104      */
105     protected $handleColorConnection;
106 
107     /**
108      * @var integer
109      */
110     protected $intMaxNeuronsPerLayer;
111 
112     /**
113      * @var integer
114      */
115     protected $intLayerDistance = 250;
116 
117     /**
118      * @var integer
119      */
120     protected $intNeuronDistance = 50;
121     
122     /**#@-*/
123     
124     /**
125      * @param Network $objNetwork
126      * @uses createImage()
127      * @uses drawNetwork()
128      * @uses Network::getNumberHiddenLayers()
129      * @uses Network::getNumberInputs()
130      * @uses Network::getNumberHiddens()
131      * @uses Network::getNumberOutputs()
132      */
133     public function __construct($intNumberInputs, $intNumberHiddenLayers, $intNumberNeuronsOfHiddenLayer, $intNumberOfOutputs)
134     {
135       $this->intNumberInputs = $intNumberInputs;
136     
137       $this->intNumberHiddenLayers = $intNumberHiddenLayers;
138     
139       $this->intNumberNeuronsOfHiddenLayer = $intNumberNeuronsOfHiddenLayer;
140     
141       $this->intNumberOfOutputs = $intNumberOfOutputs;
142     
143       $this->intMaxNeuronsPerLayer = $intNumberInputs;
144     
145       $this->createImage();
146     
147       $this->drawNetwork();
148     }
149     
150     /**
151      * @uses drawConnections()
152      * @uses drawHiddenNeurons()
153      * @uses drawInputNeurons()
154      * @uses drawOutputNeurons()
155      */
156     protected function drawNetwork()
157     {
158       $this->drawConnections();
159     
160       $this->drawInputNeurons();
161     
162       $this->drawHiddenNeurons();
163     
164       $this->drawOutputNeurons();
165     }
166     
167     /**
168      * @uses drawConnectionsHiddenOutput()
169      * @uses drawConnectionsHiddens()
170      * @uses drawConnectionsInputHidden()
171      */
172     protected function drawConnections()
173     {
174       $this->drawConnectionsInputHidden();
175     
176       $this->drawConnectionsHiddens();
177     
178       $this->drawConnectionsHiddenOutput();
179     }
180     
181     /**
182      * @uses calculateYPosStart()
183      */
184     protected function drawConnectionsInputHidden()
185     {
186       $intYPosHiddenStart = $this->calculateYPosStart($this->intNumberNeuronsOfHiddenLayer);
187     
188       $intYPosInputStart = $this->calculateYPosStart($this->intNumberInputs);
189     
190       for($intIndexInput = 0; $intIndexInput < $this->intNumberInputs; $intIndexInput++)
191         for($intIndexHidden = 0; $intIndexHidden < $this->intNumberNeuronsOfHiddenLayer; $intIndexHidden++)
192         {
193           $intXPosInput = 100;
194           
195           $intYPosInput = $intYPosInputStart + $this->intNeuronDistance * $intIndexInput;
196     
197           $intXPosHidden = 100 + $this->intLayerDistance;
198           
199           $intYPosHidden = $intYPosHiddenStart + $this->intNeuronDistance * $intIndexHidden;
200     
201           imageline($this->handleImage, $intXPosInput, $intYPosInput, $intXPosHidden, $intYPosHidden, $this->handleColorConnection);
202         }
203     }
204     
205     /**
206      * @uses calculateYPosStart()
207      */
208     protected function drawConnectionsHiddenOutput()
209     {
210       for($intIndexLayer = 0; $intIndexLayer < $this->intNumberHiddenLayers; $intIndexLayer++)
211         $intXPosHidden = 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer;
212     
213       $intYPosHiddenStart = $this->calculateYPosStart($this->intNumberNeuronsOfHiddenLayer);
214     
215       $intYPosOutputStart = $this->calculateYPosStart($this->intNumberOfOutputs);
216     
217       for($intIndexOutput = 0; $intIndexOutput < $this->intNumberOfOutputs; $intIndexOutput++)
218         for($intIndexHidden = 0; $intIndexHidden < $this->intNumberNeuronsOfHiddenLayer; $intIndexHidden++)
219         {
220           $intXPosHidden = $intXPosHidden;
221     
222           $intYPosHidden = $intYPosHiddenStart + $this->intNeuronDistance * $intIndexHidden;
223     
224           $intXPosOutput = $intXPosHidden + $this->intLayerDistance;
225     
226           $intYPosOutput = $intYPosOutputStart + $this->intNeuronDistance * $intIndexOutput;
227     
228           imageline($this->handleImage, $intXPosHidden, $intYPosHidden, $intXPosOutput, $intYPosOutput, $this->handleColorConnection);
229       }
230     }
231     
232     /**
233      * @uses calculateYPosStart()
234      */
235     protected function drawConnectionsHiddens()
236     {
237       if($this->intNumberHiddenLayers <= 1)
238         return;
239     
240       $intYPosHiddenStart = $this->calculateYPosStart($this->intNumberNeuronsOfHiddenLayer);
241     
242       for($intIndexLayer = 1; $intIndexLayer < $this->intNumberHiddenLayers; $intIndexLayer++)
243         for($intIndexHidden1 = 0; $intIndexHidden1 < $this->intNumberNeuronsOfHiddenLayer; $intIndexHidden1++)
244           for($intIndexHidden2 = 0; $intIndexHidden2 < $this->intNumberNeuronsOfHiddenLayer; $intIndexHidden2++)
245           {
246             $intXPosHidden1 = 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer - $this->intLayerDistance;
247     
248             $intYPosHidden1 = $intYPosHiddenStart + $this->intNeuronDistance * $intIndexHidden1;
249     
250             $intXPosHidden2 = 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer;
251     
252             $intYPosHidden2 = $intYPosHiddenStart + $this->intNeuronDistance * $intIndexHidden2;
253     
254             imageline($this->handleImage, $intXPosHidden1, $intYPosHidden1, $intXPosHidden2, $intYPosHidden2, $this->handleColorConnection);
255           }
256     }
257     
258     /**
259      * @uses calculateYPosStart()
260      */
261     protected function drawInputNeurons()
262     {
263       $intYPosInputStart = $this->calculateYPosStart($this->intNumberInputs);
264     
265       for($intIndex = 0; $intIndex < $this->intNumberInputs; $intIndex++)
266       {
267         imagefilledellipse($this->handleImage, 100, $intYPosInputStart + $this->intNeuronDistance * $intIndex, 30, 30, $this->handleColorNeuronInput);
268     
269         imageellipse($this->handleImage, 100, $intYPosInputStart + $this->intNeuronDistance * $intIndex, 30, 30, $this->handleColorNeuronBorder);
270       }
271     }
272     
273     /**
274      * @uses calculateYPosStart()
275      */
276     protected function drawHiddenNeurons()
277     {
278       $intYPosHiddenStart = $this->calculateYPosStart($this->intNumberNeuronsOfHiddenLayer);
279     
280       for($intIndexLayer = 0; $intIndexLayer < $this->intNumberHiddenLayers; $intIndexLayer++)
281         for($intIndexNeuron = 0; $intIndexNeuron < $this->intNumberNeuronsOfHiddenLayer; $intIndexNeuron++)
282         {
283           imagefilledellipse($this->handleImage, 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer, $intYPosHiddenStart + $this->intNeuronDistance * $intIndexNeuron, 30, 30, $this->handleColorNeuronHidden);
284     
285           imageellipse($this->handleImage, 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer, $intYPosHiddenStart + $this->intNeuronDistance * $intIndexNeuron, 30, 30, $this->handleColorNeuronBorder);
286         }
287     }
288     
289     /**
290      * @uses calculateYPosStart()
291      */
292     protected function drawOutputNeurons()
293     {
294       for($intIndexLayer = 0; $intIndexLayer < $this->intNumberHiddenLayers; $intIndexLayer++)
295         $xpos = 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer;
296     
297       $yposStart = $this->calculateYPosStart($this->intNumberOfOutputs);
298     
299       for($intIndexNeuron = 0; $intIndexNeuron < $this->intNumberOfOutputs; $intIndexNeuron++)
300       {
301         imagefilledellipse($this->handleImage, $xpos + $this->intLayerDistance, $yposStart + $this->intNeuronDistance * $intIndexNeuron, 30, 30, $this->handleColorNeuronOutput);
302     
303         imageellipse($this->handleImage, $xpos + $this->intLayerDistance, $yposStart + $this->intNeuronDistance * $intIndexNeuron, 30, 30, $this->handleColorNeuronBorder);
304       }
305     }
306     
307     /**
308      * @uses calculateImageHeight()
309      * @uses calculateImageWidth()
310      * @uses setBackground()
311      * @uses setColors()
312      */
313     protected function createImage()
314     {
315       $this->handleImage = imagecreatetruecolor($this->calculateImageWidth(), $this->calculateImageHeight());
316     
317       $this->setColors();
318     
319       $this->setBackground();
320     }
321     
322     protected function setColors()
323     {
324       $this->handleColorBackground = imagecolorallocate($this->handleImage, 200, 200, 200);
325     
326       $this->handleColorNeuronInput = imagecolorallocate($this->handleImage, 0, 255, 0);
327     
328       $this->handleColorNeuronHidden = imagecolorallocate($this->handleImage, 255, 0, 0);
329     
330       $this->handleColorNeuronOutput = imagecolorallocate($this->handleImage, 0, 0, 255);
331     
332       $this->handleColorConnection = imagecolorallocate($this->handleImage, 155, 255, 155);
333     
334       $this->handleColorNeuronBorder = imagecolorallocate($this->handleImage, 0, 0, 0);
335     }
336     
337     protected function setBackground()
338     {
339       imagefill($this->handleImage, 0, 0, $this->handleColorBackground);
340     }
341     
342     /**
343      * Returns PNG image
344      *
345      * @return binary Image
346      */
347     public function getImage()
348     {
349       ob_start();
350     
351       imagepng($this->handleImage);
352     
353       $binReturn = ob_get_contents();
354     
355       ob_end_clean();
356     
357       return $binReturn;
358     }
359     
360     /**
361      * Print PNG image
362      *
363      * @uses getImage()
364      */
365     public function printImage()
366     {
367       header('Content-type: image/png');
368     
369       print $this->getImage();
370     }
371     
372     /**
373      * @param integer $intNumberNeurons
374      * @return integer
375      */
376     protected function calculateYPosStart($intNumberNeurons)
377     {
378       $v1 = $this->intMaxNeuronsPerLayer * $this->intNeuronDistance / 2;
379     
380       $v2 = $intNumberNeurons * $this->intNeuronDistance / 2;
381     
382       return $v1 - $v2 + $this->intNeuronDistance;
383     }
384     
385     /**
386      * @return integer Pixel
387      */
388     protected function calculateImageHeight()
389     {
390       return (int)($this->intMaxNeuronsPerLayer * $this->intNeuronDistance + $this->intNeuronDistance);
391     }
392     
393     /**
394      * @return integer Pixel
395      */
396     protected function calculateImageWidth()
397     {
398       return (int)(($this->intNumberHiddenLayers + 2) * $this->intLayerDistance);
399     }
400     
401     /**
402      * Saves PNG image
403      *
404      * @param string $strFilename
405      * @uses getImage()
406      */
407     public function saveToFile($strFilename)
408     {
409       file_put_contents($strFilename, $this->getImage());
410     }
411 }
412 
NeuralNetwork API documentation generated by ApiGen