ControllerPrintNetwork.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Artificial Neural Network - Version 2.2
  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.2 by Thomas Wien
  44. * @copyright Copyright (c) 2007-2012 by Thomas Wien
  45. * @package ANN
  46. */
  47. namespace ANN\Controller;
  48. /**
  49. * @package ANN
  50. * @access private
  51. */
  52. class ControllerPrintNetwork extends Controller
  53. {
  54. /**
  55. * @var \ANN\Network
  56. */
  57. protected $objNetwork;
  58. /**
  59. * @var \ANN\Views\View
  60. */
  61. protected $objViewLayer;
  62. /**
  63. * @var \ANN\Views\View
  64. */
  65. protected $objViewNeuron;
  66. /**
  67. * @param \ANN\Network $objNetwork
  68. * @uses parent::__construct()
  69. */
  70. public function __construct(\ANN\Network $objNetwork)
  71. {
  72. $this->objNetwork = $objNetwork;
  73. parent::__construct();
  74. }
  75. /**
  76. * \ANN\Views\View::__construct()
  77. * parent::Header()
  78. */
  79. protected function Header()
  80. {
  81. parent::Header();
  82. $strFilenameNetwork = $this->strDirectoryTemplates . DIRECTORY_SEPARATOR .'tpl.network.html';
  83. $strFilenameLayer = $this->strDirectoryTemplates . DIRECTORY_SEPARATOR .'tpl.layer.html';
  84. $strFilenameNeuron = $this->strDirectoryTemplates . DIRECTORY_SEPARATOR .'tpl.neuron.html';
  85. if(!is_file($strFilenameNetwork))
  86. throw new \ANN\Exception('File '. $strFilenameNetwork .' does not exist');
  87. if(!is_file($strFilenameLayer))
  88. throw new \ANN\Exception('File '. $strFilenameLayer .' does not exist');
  89. if(!is_file($strFilenameNeuron))
  90. throw new \ANN\Exception('File '. $strFilenameNeuron .' does not exist');
  91. if(!is_readable($strFilenameNetwork))
  92. throw new \ANN\Exception('File '. $strFilenameNetwork .' is not readable');
  93. if(!is_readable($strFilenameLayer))
  94. throw new \ANN\Exception('File '. $strFilenameLayer .' is not readable');
  95. if(!is_readable($strFilenameNeuron))
  96. throw new \ANN\Exception('File '. $strFilenameNeuron .' is not readable');
  97. $this->objViewContent = new \ANN\Views\View($strFilenameNetwork);
  98. $this->objViewLayer = new \ANN\Views\View($strFilenameLayer);
  99. $this->objViewNeuron = new \ANN\Views\View($strFilenameNeuron);
  100. }
  101. /**
  102. * @uses getNeurons()
  103. * @uses \ANN\Network::getNetworkInfo()
  104. * @uses \ANN\Views\View::setArray()
  105. * @uses \ANN\Views\View::setVar()
  106. */
  107. protected function Content()
  108. {
  109. $arrNetworkInfo = $this->objNetwork->getNetworkInfo();
  110. $this->objViewContent->setArray($arrNetworkInfo);
  111. $this->objViewContent->setVar('neurons', $this->getNeurons());
  112. $intMemoryPeak = (int)memory_get_peak_usage(TRUE) / 1024;
  113. $this->objViewContent->setVar('memory_peak', $intMemoryPeak);
  114. }
  115. /**
  116. * @return string
  117. * @uses \ANN\Network::getNetworkInfo()
  118. * @uses \ANN\Layer::getDelta()
  119. * @uses \ANN\Layer::getNeurons()
  120. * @uses \ANN\Views\View::getView()
  121. * @uses \ANN\Views\View::resetView()
  122. * @uses \ANN\Views\View::setIf()
  123. * @uses \ANN\Views\View::setVar()
  124. */
  125. protected function getNeurons()
  126. {
  127. $strReturn = '';
  128. $arrNetworkInfo = $this->objNetwork->getNetworkInfo();
  129. $intCountInputs = $arrNetworkInfo['network']['intCountInputs'];
  130. $strNeuron = '<h1>Inputs</h1>';
  131. $strNeuron .= $intCountInputs;
  132. $this->objViewNeuron->setVar('neuron', $strNeuron);
  133. $strReturn .= $this->objViewNeuron->getView();
  134. $this->objViewNeuron->resetView();
  135. $arrHiddenLayers = $arrNetworkInfo['network']['arrHiddenLayers'];
  136. foreach($arrHiddenLayers as $intIndex => $objLayer)
  137. {
  138. $arrNeurons = $objLayer->getNeurons();
  139. $this->objViewNeuron->setIf('newline', TRUE);
  140. foreach($arrNeurons as $objNeuron)
  141. {
  142. $strNeuron = '<h1>Neuron</h1>';
  143. $strNeuron .= $objNeuron->getDelta();
  144. $this->objViewNeuron->setVar('neuron', $strNeuron);
  145. $strReturn .= $this->objViewNeuron->getView();
  146. $strReturn .= "\n";
  147. $this->objViewNeuron->resetView();
  148. }
  149. }
  150. /* @var $objOutputLayer \ANN\Layer */
  151. $objOutputLayer = $arrNetworkInfo['network']['objOutputLayer'];
  152. $arrOutputNeurons = $objOutputLayer->getNeurons();
  153. $this->objViewNeuron->setIf('newline', TRUE);
  154. foreach($arrOutputNeurons as $objNeuron)
  155. {
  156. $strNeuron = '<h1>Output</h1>';
  157. $strNeuron .= $objNeuron->getDelta();
  158. $this->objViewNeuron->setVar('neuron', $strNeuron);
  159. $strReturn .= $this->objViewNeuron->getView();
  160. $strReturn .= "\n";
  161. $this->objViewNeuron->resetView();
  162. }
  163. return $strReturn;
  164. }
  165. }