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