-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpdeprogram.cc
More file actions
298 lines (267 loc) · 8.14 KB
/
Copy pathgpdeprogram.cc
File metadata and controls
298 lines (267 loc) · 8.14 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
# include <gpdeprogram.h>
# include <math.h>
# include <isinf.h>
typedef double(*DOUBLE_FUNCTION)();
typedef int(*INTEGER_FUNCTION)();
# define REDO_MAX 2
# define GINF -1e+8
# define GLAMBDA 100.0
GPdeProgram::GPdeProgram(double X0,double X1,double Y0,double Y1,int n,int b)
{
/* The first constructor of the class.
* It sets the value for the boundaries, the
* training points and the boundary points. The
* other parameters must set with other methods.
* */
x0 = X0;
x1 = X1;
y0 = Y0;
y1 = Y1;
npoints = n;
if(npoints<0) npoints=25;
bpoints = b;
if(bpoints<0) bpoints=50;
f0=NULL;
f1=NULL;
g0=NULL;
g1=NULL;
ff0=NULL;
ff1=NULL;
fg0=NULL;
fg1=NULL;
pde=NULL;
fpde=NULL;
ptr=NULL;
}
GPdeProgram::GPdeProgram(QString filename)
{
/* The second constructor. It tries to load the
* parameters from the dll filename. If the dll
* can not be opened, the it uses default values
* for the parameters.
* */
ptr=new QLibrary(filename);
if(ptr==NULL)
{
x0 = 0.0;
x1 = 1.0;
y0 = 0.0;
y1 = 1.0;
npoints = 25;
bpoints = 50;
f0=NULL;
f1=NULL;
g0=NULL;
g1=NULL;
ff0=NULL;
ff1=NULL;
fg0=NULL;
fg1=NULL;
pde=NULL;
fpde=NULL;
ptr=NULL;
}
else
{
INTEGER_FUNCTION NPOINTS, BPOINTS;
DOUBLE_FUNCTION X0, X1, Y0, Y1;
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=25; else npoints=NPOINTS();
BPOINTS=(INTEGER_FUNCTION)ptr->resolve("getbpoints");
if(BPOINTS==NULL)
BPOINTS=(INTEGER_FUNCTION)ptr->resolve("_getbpoints");
if(BPOINTS==NULL)
BPOINTS=(INTEGER_FUNCTION)ptr->resolve("getbpoints_");
if(BPOINTS==NULL)
BPOINTS=(INTEGER_FUNCTION)ptr->resolve("_getbpoints_");
if(BPOINTS==NULL) bpoints=50; else bpoints=BPOINTS();
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();
ff0=NULL;
ff1=NULL;
fg0=NULL;
fg1=NULL;
fpde=NULL;
Y0=(DOUBLE_FUNCTION)ptr->resolve("gety0");
if(Y0==NULL) Y0=(DOUBLE_FUNCTION)ptr->resolve("_gety0");
if(Y0==NULL) Y0=(DOUBLE_FUNCTION)ptr->resolve("gety0_");
if(Y0==NULL) Y0=(DOUBLE_FUNCTION)ptr->resolve("_gety0_");
if(Y0==NULL) y0=0.0; else y0=Y0();
Y1=(DOUBLE_FUNCTION)ptr->resolve("gety1");
if(Y1==NULL) Y1=(DOUBLE_FUNCTION)ptr->resolve("_gety1");
if(Y1==NULL) Y1=(DOUBLE_FUNCTION)ptr->resolve("gety1_");
if(Y1==NULL) Y1=(DOUBLE_FUNCTION)ptr->resolve("_gety1_");
if(Y1==NULL) y1=0.0; else y1=Y1();
f0=(GBOUNDF)ptr->resolve("f0");
if(f0==NULL) ff0=(GFBOUNDF)ptr->resolve("_f0");
if(f0==NULL) ff0=(GFBOUNDF)ptr->resolve("f0_");
if(ff0==NULL) ff0=(GFBOUNDF)ptr->resolve("_f0_");
f1=(GBOUNDF)ptr->resolve("f1");
if(f1==NULL) f1=(GBOUNDF)ptr->resolve("_f1");
if(f1==NULL) ff1=(GFBOUNDF)ptr->resolve("f1_");
if(ff1==NULL) ff1=(GFBOUNDF)ptr->resolve("_f1_");
g0=(GBOUNDF)ptr->resolve("g0");
if(g0==NULL) g0=(GBOUNDF)ptr->resolve("_g0");
if(g0==NULL) fg0=(GFBOUNDF)ptr->resolve("g0_");
if(fg0==NULL) fg0=(GFBOUNDF)ptr->resolve("_g0_");
g1=(GBOUNDF)ptr->resolve("g1");
if(g1==NULL) g1=(GBOUNDF)ptr->resolve("_g1");
if(g1==NULL) fg1=(GFBOUNDF)ptr->resolve("g1_");
if(fg1==NULL) fg1=(GFBOUNDF)ptr->resolve("_g1_");
pde=(GPDE)ptr->resolve("pde");
if(pde==NULL) pde=(GPDE)ptr->resolve("_pde");
if(pde==NULL) fpde=(GFPDE)ptr->resolve("pde_");
if(fpde==NULL) fpde=(GFPDE)ptr->resolve("_pde_");
}
}
double GPdeProgram::getX0() const
{
/* Return the left boundary at xx'.
* */
return x0;
}
double GPdeProgram::getX1() const
{
/* Return the right boundary at xx'.
* */
return x1;
}
double GPdeProgram::getY0() const
{
/* Return the left boundary at yy'.
* */
return y0;
}
double GPdeProgram::getY1() const
{
/* Return the right boundary at yy'.
* */
return y1;
}
double GPdeProgram::getNpoints() const
{
/* Return the amount of training points.
* */
return npoints;
}
double GPdeProgram::getBoundaryPoints() const
{
/* Return the amount of boundary points.
* */
return bpoints;
}
void GPdeProgram::setBoundaries(GBOUNDF F0,GBOUNDF F1,GBOUNDF G0,GBOUNDF G1)
{
/* Set the boundary conditions.
* */
if(f0==NULL)
{
f0=F0;
f1=F1;
g0=G0;
g1=G1;
}
}
void GPdeProgram::setPde(GPDE p)
{
/* Set the pde to be solved.
* */
if(pde==NULL) pde=p;
}
double GPdeProgram::fitness(vector<int> &genome)
{
/* Estimate the fitness for the integer variable genome.
* */
if(pde==NULL && fpde==NULL) return GINF;
int redo=0;
string str=printProgram(genome,redo);
if(redo>=REDO_MAX) return GINF;
double value=0.0;
double x[2];
double X,y,Y;
int code=parser.Parse(str,"x,y");
if(code!=-1) return GINF;
double penalty1=0.0,penalty2=0.0,penalty3=0.0,penalty4=0.0;
double val1,val2,val3,val4;
double stepx=(x1-x0)/bpoints;
double stepy=(y1-y0)/bpoints;
/* Calculation of the penalties.
* */
for(X=x0;X<=x1;X+=stepx)
{
x[0]=X;
x[1]=y0;
if(fg0==NULL) val1=g0(X); else val1=fg0(&X);
penalty3+=pow(parser.Eval(x)-val1,2.0);
if(parser.EvalError()) return GINF;
x[1]=y1;
if(fg1==NULL) val2=g1(X); else val2=fg1(&X);
penalty4+=pow(parser.Eval(x)-val2,2.0);
if(parser.EvalError()) return GINF;
}
for(y=y0;y<=y1;y+=stepy)
{
x[0]=x0;
x[1]=y;
if(ff0==NULL) val3=f0(y); else val3=ff0(&y);
penalty1+=pow(parser.Eval(x)-val3,2.0);
if(parser.EvalError()) return GINF;
x[0]=x1;
if(ff1==NULL) val4=f1(y); else val4=ff1(&y);
penalty2+=pow(parser.Eval(x)-val4,2.0);
if(parser.EvalError()) return GINF;
}
if(std::isnan(penalty1) || std::isinf(penalty1)) return GINF;
if(std::isnan(penalty2) || std::isinf(penalty2)) return GINF;
if(std::isnan(penalty3) || std::isinf(penalty3)) return GINF;
if(std::isnan(penalty4) || std::isinf(penalty4)) return GINF;
/* Calculation of the fitness.
* */
stepx=(x1-x0)/(int)(sqrt(1.0*npoints));
stepy=(y1-y0)/(int)(sqrt(1.0*npoints));
for(Y=y0;Y<y1+0.01;Y+=stepy)
{
for(X=x0;X<x1+0.01;X+=stepx)
{
x[0]=X;
x[1]=Y;
double valx2=parser.EvalDeriv2(x,0);
if(parser.EvalError()) return GINF;
double valy2=parser.EvalDeriv2(x,1);
if(parser.EvalError()) return GINF;
double valx1=parser.EvalDeriv(x,0);
double valy1=parser.EvalDeriv(x,1);
double val=parser.Eval(x);
if(fpde==NULL)
value = value +pow(pde(X,Y,val,valx1,valy1,valx2,valy2),2.0);
else
value = value +pow(fpde(&X,&Y,&val,&valx1,&valy1,&valx2,&valy2),2.0);
if(std::isnan(value) || std::isinf(value)) return GINF;
}
}
double p=penalty1+penalty2+penalty3+penalty4;
value+=p;///4.0;
if(std::isnan(value) || std::isinf(value)) return GINF;
return -value;
}
GPdeProgram::~GPdeProgram()
{
/* The destructor of the class. It closes the dll pointed
* by ptr, if it has opened.
* */
if(ptr!=NULL) delete ptr;
}