PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/opencog/opencog/learning/moses/moses/ann_scoring.h

https://bitbucket.org/KohrAh/jni-pln
C Header | 326 lines | 213 code | 64 blank | 49 comment | 12 complexity | 0ea9eb070210d6d3e34d8cd86ecdac18 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-3.0
  1. /*
  2. * opencog/learning/moses/moses/ann_scoring.h
  3. *
  4. * Copyright (C) 2002-2008 Novamente LLC
  5. * All Rights Reserved
  6. *
  7. * Written by Joel Lehman
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License v3 as
  11. * published by the Free Software Foundation and including the exceptions
  12. * at http://opencog.org/wiki/Licenses
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program; if not, write to:
  21. * Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. #ifndef _ANN_SCORING_H
  25. #define _ANN_SCORING_H
  26. #include <opencog/util/numeric.h>
  27. #include <opencog/util/mt19937ar.h>
  28. #include <opencog/comboreduct/combo/vertex.h>
  29. #include <opencog/comboreduct/combo/simple_nn.h>
  30. #include <opencog/comboreduct/reduct/ann_rules.h>
  31. #include "scoring.h"
  32. #include "scoring_functions.h"
  33. #include "pole_balancing.h"
  34. using namespace opencog;
  35. using namespace combo;
  36. using namespace std;
  37. using namespace moses;
  38. #define MIN_FITNESS -1.0e10
  39. struct AnnPole2NVFitnessFunction : public unary_function<combo_tree, double> {
  40. result_type operator()(argument_type tr) const {
  41. bool velocity = false;
  42. if (tr.empty())
  43. return MIN_FITNESS;
  44. tree_transform tt;
  45. ann nn = tt.decodify_tree(tr);
  46. CartPole the_cart(true,velocity);
  47. the_cart.nmarkov_long=false;
  48. the_cart.generalization_test=false;
  49. double fitness = -100000.0+the_cart.evalNet(&nn);
  50. return fitness;
  51. }
  52. };
  53. struct AnnPole2FitnessFunction : public unary_function<combo_tree, double> {
  54. result_type operator()(argument_type tr) const {
  55. bool velocity = true;
  56. if (tr.empty())
  57. return MIN_FITNESS;
  58. tree_transform tt;
  59. ann nn = tt.decodify_tree(tr);
  60. CartPole the_cart(true,velocity);
  61. the_cart.nmarkov_long=false;
  62. the_cart.generalization_test=false;
  63. double fitness = -100000+the_cart.evalNet(&nn);
  64. return fitness;
  65. }
  66. };
  67. struct AnnPoleFitnessFunction : public unary_function<combo_tree, double> {
  68. result_type operator()(argument_type tr) const {
  69. if (tr.empty())
  70. return MIN_FITNESS;
  71. tree_transform tt;
  72. ann nn = tt.decodify_tree(tr);
  73. return go_cart(&nn,100000);
  74. }
  75. // cart_and_pole() was take directly from the pole simulator written
  76. // by Richard Sutton and Charles Anderson.
  77. int go_cart(ann *net,int max_steps) const
  78. {
  79. float x, /* cart position, meters */
  80. x_dot, /* cart velocity */
  81. theta, /* pole angle, radians */
  82. theta_dot; /* pole angular velocity */
  83. int steps=0,y;
  84. int random_start=1;
  85. double in[5]; //Input loading array
  86. double out1;
  87. double out2;
  88. // double one_degree= 0.0174532; /* 2pi/360 */
  89. // double six_degrees=0.1047192;
  90. double twelve_degrees=0.2094384;
  91. // double thirty_six_degrees= 0.628329;
  92. // double fifty_degrees=0.87266;
  93. if (random_start) {
  94. /*set up random start state*/
  95. x = (lrand48()%4800)/1000.0 - 2.4;
  96. x_dot = (lrand48()%2000)/1000.0 - 1;
  97. theta = (lrand48()%400)/1000.0 - .2;
  98. theta_dot = (lrand48()%3000)/1000.0 - 1.5;
  99. }
  100. else
  101. x = x_dot = theta = theta_dot = 0.0;
  102. /*--- Iterate through the action-learn loop. ---*/
  103. while (steps++ < max_steps) {
  104. /*-- setup the input layer based on the four iputs --*/
  105. //setup_input(net,x,x_dot,theta,theta_dot);
  106. in[0]=1.0; //Bias
  107. in[1]=(x + 2.4) / 4.8;;
  108. in[2]=(x_dot + .75) / 1.5;
  109. in[3]=(theta + twelve_degrees) / .41;
  110. in[4]=(theta_dot + 1.0) / 2.0;
  111. net->load_inputs(in);
  112. int depth = net->feedforward_depth();
  113. dorepeat(depth)
  114. net->propagate();
  115. /*-- decide which way to push via which output unit is greater --*/
  116. out1=net->outputs[0]->activation;
  117. out2=net->outputs[1]->activation;
  118. if (out1 > out2)
  119. y = 0;
  120. else
  121. y = 1;
  122. /*--- Apply action to the simulated cart-pole ---*/
  123. cart_pole(y, &x, &x_dot, &theta, &theta_dot);
  124. /*--- Check for failure. If so, return steps ---*/
  125. if (x < -2.4 || x > 2.4 || theta < -twelve_degrees ||
  126. theta > twelve_degrees)
  127. return steps;
  128. }
  129. return steps;
  130. }
  131. // cart_and_pole() was take directly from the pole simulator written
  132. // by Richard Sutton and Charles Anderson.
  133. // This simulator uses normalized, continous inputs instead of
  134. // discretizing the input space.
  135. /*----------------------------------------------------------------------
  136. cart_pole: Takes an action (0 or 1) and the current values of the
  137. four state variables and updates their values by estimating the state
  138. TAU seconds later.
  139. ----------------------------------------------------------------------*/
  140. void cart_pole(int action, float *x,float *x_dot, float *theta, float *theta_dot) const {
  141. float xacc,thetaacc,force,costheta,sintheta,temp;
  142. const float GRAVITY=9.8;
  143. const float MASSCART=1.0;
  144. const float MASSPOLE=0.1;
  145. const float TOTAL_MASS=(MASSPOLE + MASSCART);
  146. const float LENGTH=0.5; /* actually half the pole's length */
  147. const float POLEMASS_LENGTH=(MASSPOLE * LENGTH);
  148. const float FORCE_MAG=10.0;
  149. const float TAU=0.02; /* seconds between state updates */
  150. const float FOURTHIRDS=1.3333333333333;
  151. force = (action>0)? FORCE_MAG : -FORCE_MAG;
  152. costheta = cos(*theta);
  153. sintheta = sin(*theta);
  154. temp = (force + POLEMASS_LENGTH * *theta_dot * *theta_dot * sintheta)
  155. / TOTAL_MASS;
  156. thetaacc = (GRAVITY * sintheta - costheta* temp)
  157. / (LENGTH * (FOURTHIRDS - MASSPOLE * costheta * costheta
  158. / TOTAL_MASS));
  159. xacc = temp - POLEMASS_LENGTH * thetaacc* costheta / TOTAL_MASS;
  160. /*** Update the four state variables, using Euler's method. ***/
  161. *x += TAU * *x_dot;
  162. *x_dot += TAU * xacc;
  163. *theta += TAU * *theta_dot;
  164. *theta_dot += TAU * thetaacc;
  165. }
  166. };
  167. struct AnnFitnessFunction : public unary_function<combo_tree, double> {
  168. typedef combo_tree::iterator pre_it;
  169. typedef combo_tree::sibling_iterator sib_it;
  170. result_type operator()(argument_type tr) const {
  171. if (tr.empty())
  172. return MIN_FITNESS;
  173. tree_transform tt;
  174. // binary xor_problem. The third input is always 1.0, this is
  175. // "to potentially supply a constant 'bias' to influence the
  176. // behavior of other neurons" (Joel Lehman)
  177. double inputs[4][3] = { {0.0, 0.0, 1.0},
  178. {0.0, 1.0, 1.0},
  179. {1.0, 0.0, 1.0},
  180. {1.0, 1.0, 1.0}};
  181. double outputs[4] = {0.0, 1.0, 1.0, 0.0};
  182. ann nn = tt.decodify_tree(tr);
  183. int depth = nn.feedforward_depth();
  184. double error = 0.0;
  185. for (int pattern = 0;pattern < 4;++pattern) {
  186. nn.load_inputs(inputs[pattern]);
  187. dorepeat(depth)
  188. nn.propagate();
  189. double diff = outputs[pattern] - nn.outputs[0]->activation;
  190. error += diff * diff;
  191. }
  192. return -error;
  193. }
  194. };
  195. namespace opencog { namespace moses {
  196. struct ann_pole2nv_score : public unary_function<combo_tree, double> {
  197. ann_pole2nv_score() { }
  198. double operator()(const combo_tree& tr) const {
  199. return p2ff(tr);
  200. }
  201. AnnPole2NVFitnessFunction p2ff;
  202. };
  203. struct ann_pole2nv_bscore : public unary_function<combo_tree, behavioral_score> {
  204. ann_pole2nv_bscore( ) { }
  205. behavioral_score operator()(const combo_tree& tr) const {
  206. behavioral_score bs(2);
  207. bs[0] = moses::ann_pole2nv_score()(tr);
  208. bs[1] = tr.size();
  209. return bs;
  210. }
  211. };
  212. struct ann_pole2_score : public unary_function<combo_tree, double> {
  213. ann_pole2_score() { }
  214. double operator()(const combo_tree& tr) const {
  215. return p2ff(tr);
  216. }
  217. AnnPole2FitnessFunction p2ff;
  218. };
  219. struct ann_pole2_bscore : public unary_function<combo_tree, behavioral_score> {
  220. ann_pole2_bscore( ) { }
  221. behavioral_score operator()(const combo_tree& tr) const {
  222. behavioral_score bs(2);
  223. bs[0] = moses::ann_pole2_score()(tr);
  224. bs[1] = tr.size();
  225. return bs;
  226. }
  227. };
  228. struct ann_pole_score : public unary_function<combo_tree, double> {
  229. ann_pole_score() { }
  230. double operator()(const combo_tree& tr) const {
  231. return pff(tr);
  232. }
  233. AnnPoleFitnessFunction pff;
  234. };
  235. struct ann_pole_bscore : public unary_function<combo_tree, behavioral_score> {
  236. ann_pole_bscore( ) { }
  237. behavioral_score operator()(const combo_tree& tr) const {
  238. behavioral_score bs(2);
  239. bs[0] = moses::ann_pole_score()(tr);
  240. bs[1] = tr.size();
  241. return bs;
  242. }
  243. };
  244. struct ann_score : public unary_function<combo_tree, double> {
  245. ann_score() { }
  246. double operator()(const combo_tree& tr) const {
  247. return aff(tr);
  248. }
  249. AnnFitnessFunction aff;
  250. };
  251. struct ann_bscore : public unary_function<combo_tree, behavioral_score> {
  252. ann_bscore( ) { }
  253. behavioral_score operator()(const combo_tree& tr) const {
  254. behavioral_score bs(2);
  255. bs[0] = -moses::ann_score()(tr);
  256. bs[1] = tr.size();
  257. return bs;
  258. }
  259. };
  260. } // ~namespace moses
  261. } // ~namespace opencog
  262. #endif