-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgsodeprogram.cc
More file actions
311 lines (290 loc) · 7.38 KB
/
Copy pathgsodeprogram.cc
File metadata and controls
311 lines (290 loc) · 7.38 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# include <gsodeprogram.h>
# include <math.h>
# include <isinf.h>
# include <QDebug>
typedef double(*DOUBLE_FUNCTION)();
typedef int(*INTEGER_FUNCTION)();
# define REDO_MAX 4
# define GINF -1e+100
# define GLAMBDA 1000.0
GSodeProgram::GSodeProgram(double X0,double X1,int Node,int Npoints)
{
/* The first constructor of the class. It sets the value of
* x0, x1, node and npoints. The rest parameters bust be set
* by the user, with the help of other methods.
* */
x0 = X0;
x1 = X1;
node = Node;
if(node<0) node=1;
f0=new double[node];
npoints=Npoints;
if(npoints<0) npoints=10;
systemfun=NULL;
fsystemfun=NULL;
systemf0=NULL;
fsystemf0=NULL;
parser.resize(node);
for(int i=0;i<node;i++) parser[i]=new FunctionParser();
}
GSodeProgram::GSodeProgram(QString filename)
{
/* The second constructor of the class. It tries to open
* the dll filename. If the dll can not be opened, then
* the constructor sets default values for the parameters.
* If the opening of dll was sucessfull, it loads all
* the parameters of the SODE from the dll.
* */
int i;
ptr=new QLibrary(filename);
if(ptr==NULL)
{
x0=0.0;
x1=1.0;
npoints=10;
node=1;
f0=new double[node];
systemfun=NULL;
fsystemfun=NULL;
systemf0=NULL;
fsystemf0=NULL;
parser.resize(node);
for(int i=0;i<node;i++) parser[i]=new FunctionParser();
}
else
{
DOUBLE_FUNCTION X0, X1;
INTEGER_FUNCTION NODE, NPOINTS;
X0=(DOUBLE_FUNCTION)ptr->resolve("getx0");
if(X0==NULL) X0=(DOUBLE_FUNCTION)ptr->resolve("_getx0");
if(X0==NULL) X0=(DOUBLE_FUNCTION)ptr->resolve("getx0_");
if(X0==NULL) X0=(DOUBLE_FUNCTION)ptr->resolve("_getx0_");
if(X0==NULL) x0=0.0; else x0=X0();
X1=(DOUBLE_FUNCTION)ptr->resolve("getx1");
if(X1==NULL) X1=(DOUBLE_FUNCTION)ptr->resolve("_getx1");
if(X1==NULL) X1=(DOUBLE_FUNCTION)ptr->resolve("getx1_");
if(X1==NULL) X1=(DOUBLE_FUNCTION)ptr->resolve("_getx1_");
if(X1==NULL) x1=1.0; else x1=X1();
NODE=(INTEGER_FUNCTION)ptr->resolve("getnode");
if(NODE==NULL) NODE=(INTEGER_FUNCTION)ptr->resolve("_getnode");
if(NODE==NULL) NODE=(INTEGER_FUNCTION)ptr->resolve("getnode_");
if(NODE==NULL) NODE=(INTEGER_FUNCTION)ptr->resolve("_getnode_");
if(NODE==NULL) node=1;else node=NODE();
NPOINTS=(INTEGER_FUNCTION)ptr->resolve("getnpoints");
if(NPOINTS==NULL)
NPOINTS=(INTEGER_FUNCTION)ptr->resolve("_getnpoints");
if(NPOINTS==NULL)
NPOINTS=(INTEGER_FUNCTION)ptr->resolve("getnpoints_");
if(NPOINTS==NULL)
NPOINTS=(INTEGER_FUNCTION)ptr->resolve("_getnpoints_");
if(NPOINTS==NULL) npoints=10; else npoints=NPOINTS();
fsystemfun=NULL;
fsystemf0=NULL;
systemfun=(GSYSTEMFUN)ptr->resolve("systemfun");
if(systemfun==NULL)
systemfun=(GSYSTEMFUN)ptr->resolve("_systemfun");
if(systemfun==NULL)
fsystemfun=(GFSYSTEMFUN)ptr->resolve("systemfun_");
if(fsystemfun==NULL)
fsystemfun=(GFSYSTEMFUN)ptr->resolve("systemfun_");
setparam=(SETPARAM)ptr->resolve("setparam");
if(setparam==NULL)
setparam=(SETPARAM)ptr->resolve("_setparam");
if(setparam==NULL)
setparam=(SETPARAM)ptr->resolve("setparam_");
systemf0=(GSYSTEMF0)ptr->resolve("systemf0");
if(systemf0==NULL)
systemf0=(GSYSTEMF0)ptr->resolve("_systemf0");
if(systemf0==NULL)
{
fsystemf0=(GFSYSTEMF0)ptr->resolve("systemf0_");
if(fsystemf0==NULL)
fsystemf0=(GFSYSTEMF0)ptr->resolve("_systemf0_");
f0=new double[node];
fsystemf0(&node,f0);
}
else
{
f0=new double[node];
systemf0(node,f0);
}
parser.resize(node);
for(i=0;i<node;i++) parser[i]=new FunctionParser();
}
}
void GSodeProgram::setGsystemFun(GSYSTEMFUN f)
{
/* Set the systemfun to f, if systemfun is NULL.
* */
if(systemfun==NULL) systemfun=f;
}
void GSodeProgram::setGsystemF0(GSYSTEMF0 f)
{
/* Set the systemf0 to f, if systemf0 is NULL.
* */
if(systemf0==NULL)
{
systemf0=f;
systemf0(node,f0);
}
}
double GSodeProgram::getX0() const
{
/* Return the left boundary of the equations.
* */
return x0;
}
double GSodeProgram::getX1() const
{
/* Return the right boundary of the equations.
* */
return x1;
}
int GSodeProgram::getNode() const
{
/* Return the amount of ODE's in the system.
* */
return node;
}
int GSodeProgram::getNpoints() const
{
/* Return the amount of training points.
* */
return npoints;
}
int xpos(string s)
{
for(int i=0;i<s.size()-1;i++)
{
if(s[i]=='x' && s[i+1]!='p') return 1;
}
return 0;
}
double GSodeProgram::fitness(vector<int> &genome)
{
/* Evaluation of the fitness value associated with the
* integer array genome. Each genome codes node functions,
* in its elements.
* */
if(systemfun==NULL && fsystemfun==NULL) return GINF;
int i,j,code,redo;
vector<int> genome_part;
genome_part.resize(genome.size()/node);
string str;
double *y_array=new double[node];
double *yy_array=new double[node];
double x[1];
/* Parsing of the functions.
* */
double pt = 0.0;
for(i=0;i<node;i++)
{
redo=0;
for(j=0;j<genome.size()/node;j++)
genome_part[j]=genome[i*(genome.size()/node)+j];
str=printProgram(genome_part,redo);
if(redo>=REDO_MAX)
{
delete[] y_array;
delete[] yy_array;
return GINF;
}
if(xpos(str)==0)
{
pt+=10.0;
}
code=parser[i]->Parse(str,"x");
if(code!=-1)
{
delete[] y_array;
delete[] yy_array;
return GINF;
}
}
double value=0.0;
/* Fitness evaluation at the npoints points.
* */
double lastValue=1e+100;
int sameValue=0;
for(i=0;i<npoints;i++)
{
x[0]=x0+i*1.0*(x1-x0)/(npoints-1.0);
for(int j=0;j<node;j++)
{
yy_array[j]=parser[j]->EvalDeriv(x,0);
y_array[j]=parser[j]->lastEval();
if(parser[j]->EvalError())
{
delete[] y_array;
delete[] yy_array;
return GINF;
}
}
if(setparam!=NULL)
{
if(x[0]<20.0)
{
setparam("delay", QString::number(f0[0]));
setparam("delay2",QString::number(f0[1]));
setparam("delay3",QString::number(f0[2]));
}
else
{
//go back 20
double lookupx[1];
lookupx[0]=x[0]-20.0;
parser[1]->EvalDeriv(lookupx,0);
double delay = parser[1]->lastEval();
setparam("delay2",QString::number(delay));
parser[2]->EvalDeriv(lookupx,0);
delay = parser[2]->lastEval();
setparam("delay3",QString::number(delay));
parser[0]->EvalDeriv(lookupx,0);
delay=parser[0]->lastEval();
setparam("delay",QString::number(delay));
}
}
double pf;
if(fsystemfun==NULL)
pf=systemfun(node,x[0],y_array,yy_array);
else
pf=fsystemfun(&node,&x[0],y_array,yy_array);
if(i && fabs(pf-lastValue)<1e-5) sameValue++;
lastValue = pf;
value = value + pf;
if(std::isnan(value) || std::isinf(value))
{
delete[] y_array;
delete[] yy_array;
return GINF;
}
}
/* Penalty evaluation.
* */
double penalty=0.0;
for(j=0;j<node;j++)
{
x[0]=x0;
y_array[j]=parser[j]->Eval(x);
penalty+=GLAMBDA * pow(y_array[j]-f0[j],2.0);
if(parser[j]->EvalError())
{
delete[] y_array;
delete[] yy_array;
return GINF;
}
}
value = value +(penalty);
delete[] y_array;
delete[] yy_array;
return -value*(1.0+pt);//sameValue*1.0/npoints);
}
GSodeProgram::~GSodeProgram()
{
/* The destructor of the class. It deallocates the memory of the
* class.
* */
if(ptr!=NULL) delete ptr;
delete[] f0;
for(int i=0;i<node;i++) delete parser[i];
}