Values.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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;
  48. /**
  49. * @package ANN
  50. * @access public
  51. * @since 2.0.6
  52. */
  53. class Values extends Filesystem implements InterfaceLoadable
  54. {
  55. /**#@+
  56. * @ignore
  57. */
  58. /**
  59. * @var array
  60. */
  61. protected $arrInputs = array();
  62. /**
  63. * @var array
  64. */
  65. protected $arrOutputs = array();
  66. /**
  67. * @var boolean
  68. */
  69. protected $boolLastActionInput = FALSE;
  70. /**
  71. * @var boolean
  72. */
  73. protected $boolTrain = FALSE;
  74. /**
  75. * @var integer
  76. */
  77. protected $intCountInputs = null;
  78. /**
  79. * @var integer
  80. */
  81. protected $intCountOutputs = null;
  82. /**#@-*/
  83. /**
  84. * Input values
  85. *
  86. * List all input values comma separated
  87. *
  88. * <code>
  89. * $objValues = new \ANN\Values;
  90. *
  91. * $objValues->train()
  92. * ->input(0.12, 0.11, 0.15)
  93. * ->output(0.56);
  94. * </code>
  95. *
  96. * <code>
  97. * $objValues = new \ANN\Values;
  98. *
  99. * $objValues->input(0.12, 0.11, 0.15)
  100. * ->input(0.13, 0.12, 0.16)
  101. * ->input(0.14, 0.13, 0.17);
  102. * </code>
  103. *
  104. * @return Values
  105. * @uses Exception::__construct()
  106. * @throws Exception
  107. */
  108. public function input()
  109. {
  110. if($this->boolTrain && $this->boolLastActionInput)
  111. throw new Exception('After calling input() method output() should be called');
  112. $arrParameters = func_get_args();
  113. $arrInputParameters = array();
  114. foreach($arrParameters as $mixedParameter)
  115. if(is_array($mixedParameter))
  116. {
  117. $arrInputParameters = array_merge($arrInputParameters, $mixedParameter);
  118. }
  119. elseif(is_numeric($mixedParameter))
  120. {
  121. $arrInputParameters[] = $mixedParameter;
  122. }
  123. $intCountParameters = func_num_args();
  124. foreach($arrInputParameters as $floatParameter)
  125. if(!is_float($floatParameter) && !is_integer($floatParameter))
  126. throw new Exception('Each parameter should be float');
  127. if($this->intCountInputs === null)
  128. $this->intCountInputs = $intCountParameters;
  129. if($this->intCountInputs != $intCountParameters)
  130. throw new Exception('There should be '. $this->intCountInputs .' parameter values for input()');
  131. $this->arrInputs[] = $arrInputParameters;
  132. $this->boolLastActionInput = TRUE;
  133. return $this;
  134. }
  135. /**
  136. * Output values
  137. *
  138. * List all output values comma separated. Before you can call this method you
  139. * have to call input(). After calling output() you cannot call the same method
  140. * again. You have to call input() again first.
  141. *
  142. * <code>
  143. * $objValues = new \ANN\Values;
  144. *
  145. * $objValues->train()
  146. * ->input(0.12, 0.11, 0.15)
  147. * ->output(0.56);
  148. * </code>
  149. *
  150. * @return Values
  151. * @uses Exception::__construct()
  152. * @throws Exception
  153. */
  154. public function output()
  155. {
  156. if(!$this->boolLastActionInput)
  157. throw new Exception('After calling output() method input() should be called');
  158. if(!$this->boolTrain)
  159. throw new Exception('Calling output() is just allowed for training. Call train() if values for training.');
  160. $arrParameters = func_get_args();
  161. // If Classification is used
  162. if(isset($arrParameters[0]) && is_array($arrParameters[0]))
  163. $arrParameters = $arrParameters[0];
  164. $intCountParameters = func_num_args();
  165. foreach($arrParameters as $floatParameter)
  166. if(!is_float($floatParameter) && !is_integer($floatParameter))
  167. throw new Exception('Each parameter should be float');
  168. if($this->intCountOutputs === null)
  169. $this->intCountOutputs = $intCountParameters;
  170. if($this->intCountOutputs != $intCountParameters)
  171. throw new Exception('There should be '. $this->intCountOutputs .' parameter values for output()');
  172. $this->arrOutputs[] = $arrParameters;
  173. $this->boolLastActionInput = FALSE;
  174. return $this;
  175. }
  176. /**
  177. * @return Values
  178. */
  179. public function train()
  180. {
  181. $this->boolTrain = TRUE;
  182. return $this;
  183. }
  184. /**
  185. * Get internal saved input array
  186. *
  187. * Actually there is no reason to call this method in your application. This
  188. * method is used by \ANN\Network only.
  189. *
  190. * @return array
  191. */
  192. public function getInputsArray()
  193. {
  194. return $this->arrInputs;
  195. }
  196. /**
  197. * Get internal saved output array
  198. *
  199. * Actually there is no reason to call this method in your application. This
  200. * method is used by Network only.
  201. *
  202. * @return array
  203. */
  204. public function getOutputsArray()
  205. {
  206. return $this->arrOutputs;
  207. }
  208. /**
  209. * Unserializing \ANN\Values
  210. *
  211. * After calling unserialize the train mode is set to false. Therefore it is
  212. * possible to use a saved object of \ANN\Values to use inputs not for training
  213. * purposes.
  214. *
  215. * You would not use unserialize in your application but you can call loadFromFile()
  216. * to load the saved object to your application.
  217. */
  218. public function __wakeup()
  219. {
  220. $this->boolTrain = FALSE;
  221. }
  222. /**
  223. * Reset saved input and output values
  224. *
  225. * All internal saved input and output values will be deleted after calling reset().
  226. * If train() was called before, train state does not change by calling reset().
  227. *
  228. * <code>
  229. * $objValues = new \ANN\Values;
  230. *
  231. * $objValues->train()
  232. * ->input(0.12, 0.11, 0.15)
  233. * ->output(0.56)
  234. * ->reset()
  235. * ->input(0.12, 0.11, 0.15)
  236. * ->output(0.56);
  237. * </code>
  238. *
  239. * @return Values
  240. */
  241. public function reset()
  242. {
  243. $this->arrInputs = array();
  244. $this->arrOutputs = array();
  245. return $this;
  246. }
  247. }