-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
381 lines (345 loc) · 12.5 KB
/
main.cpp
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <QCoreApplication>
# include <get_options.h>
# include <nncneuralprogram.h>
# include <population.h>
# include <tolmin.h>
# include <converter.h>
# include <odeneuralprogram.h>
# include <pdeneuralprogram.h>
# include <sodeneuralprogram.h>
# include <kdvneuralprogram.h>
# include <QFile>
# include <QTextStream>
# include <QIODevice>
NeuralProgram *program=NULL;
Population *pop=NULL;
typedef vector<int>Genome;
int getDimension(QString filename)
{
int d=0;
QFile fp(filename);
if(!fp.open(QIODevice::ReadOnly|QIODevice::Text)) return 0;
QTextStream st(&fp);
st>>d;
fp.close();
return d;
}
void run()
{
int d=0;
if(kind=="neural")
{
d=getDimension(train_file);
program=new NNCNeuralProgram (d,train_file,test_file);
}
else
if(kind=="ode")
{
d=1;
program=new OdeNeuralProgram(train_file);
}
else
if(kind=="pde")
{
d=2;
program=new PdeNeuralProgram(train_file);
}
else
if(kind=="kdv")
{
d=2;
program=new KdvNeuralProgram(train_file);
}
pop=new Population (genome_count,genome_length ,program);
pop->setSelectionRate(selection_rate);
pop->setMutationRate(mutation_rate);
const int max_generations=generations;
vector<int> genome;
genome.resize(genome_length);
string str;
double f;
double old_test_error=0.0;
Data bestWeights;
double bestError=1e+100;
for(int i=1;i<=max_generations;i++)
{
pop->nextGeneration();
f=pop->getBestFitness();
genome=pop->getBestGenome();
str=program->printProgram(genome);
program->fitness(genome);
if(fabs(f)<bestError)
{
program->neuralparser->getWeights(bestWeights);
bestError=fabs(f);
old_test_error=program->getTestError();
}
printf("%d\t%lf\t%s\n",i,f,str.c_str());
//printf("BEST[%d]=%20.10lf Solution: y(x)=%s\n",i,f,str.c_str());
//LOCALSEARCH
if(i%localSearchGenerations==0)
{
int imax=localSearchChromosomes;
int iflag=0;
for(int k=0;k<imax;k++)
{
vector<int> trial_genome;
trial_genome.resize(genome_length);
int trial_pos;
Data x;
again:
if(iflag==0)
trial_pos=rand() % genome_count;//(rand()%2==1)?0:(rand() % genome_count);//genome_count-1;
else
trial_pos=rand() % genome_count;
iflag=1;
pop->getGenome(trial_pos,trial_genome);
program->fitness(trial_genome);
program->neuralparser->getWeights(x);
double value=0;
MinInfo Info1;
Info1.iters=2001;
Info1.problem=program;
double old_f=1e+100;
int tries=0;
do
{
value=program->getTrainError();
if(value>=1e+8) {iflag=1;goto again;}
value=tolmin(x,Info1);
if(value>=1e+8) {iflag=1;goto again;}
program->neuralparser->getWeights(x);
if(fabs(old_f-value)<1e-5) break;
old_f=value;
fflush(stdout);
tries++;
if(tries>=20) break;
break;
}while(1);
program->neuralparser->getWeights(x);
value=program->getTrainError();
if((std::isnan(value) || std::isinf(value)))
{
if(!k) continue;
iflag=1;
goto again;
}
Converter con(x,x.size()/(d+2),d);
con.convert(trial_genome);
for(int i=0;i<trial_genome.size();i++)
{
//if(abs(trial_genome[i])>255) trial_genome[i]=0;
}
double trial_fitness=-value;
pop->setGenome(trial_pos,trial_genome,trial_fitness);
if(fabs(value)<=bestError)
{
bestWeights=x;
bestError=fabs(value);
old_test_error=program->getTestError();
pop->setBest(trial_genome,trial_fitness);
f=trial_fitness;
}
if(value<f)
{
bestWeights=x;
bestError=fabs(value);
old_test_error=program->getTestError();
pop->setBest(trial_genome,trial_fitness);
f=trial_fitness;
}
}
pop->select();
}
if(fabs(bestError)<1e-6) break;
}
program->neuralparser->setWeights(bestWeights);
old_test_error=program->getTestError();
str=program->printProgram(genome);
printf("TRAIN ERROR =%.10lf\n",bestError);
printf("TEST ERROR =%.10lf\n",old_test_error);
if(kind=="neural")
{
NNCNeuralProgram *p=(NNCNeuralProgram*)program;
double class_test=p->getClassTestError(genome);
printf("CLASS ERROR=%.2lf%%\n",class_test);
}
printf("SOLUTION: y(x)=%s\n",str.c_str());
if(output_file!="") program->printOutput(output_file);
delete pop;
delete program;
}
void runSode()
{
SodeNeuralProgram p(train_file);
genome_length = genome_length * p.getNode();
Population pop(genome_count,genome_length ,&p);
pop.setSelectionRate(selection_rate);
pop.setMutationRate(mutation_rate);
const int max_generations=generations;
vector<int> genome;
genome.resize(genome_length);
string str;
double f;
double old_test_error=0.0;
vector<Data> bestWeights;
bestWeights.resize(p.getNode());
double bestError=1e+100;
for(int i=1;i<=max_generations;i++)
{
pop.nextGeneration();
genome=pop.getBestGenome();
p.fitness(genome);
str=p.printProgram(genome);
f=p.getTrainError();
if(fabs(f)<bestError)
{
for(int j=0;j<p.getNode();j++)
p.nparser[j]->getWeights(bestWeights[j]);
bestError=fabs(f);
old_test_error=p.getTestError();
}
printf("BEST[%d]=%20.10lf\n",i,f);
//LOCALSEARCH
vector<double> fvalue;
fvalue.resize(p.getNode());
if(i%localSearchGenerations==0)
{
for(int k=0;k<localSearchChromosomes;k++)
{
vector<int> trial_genome;
trial_genome.resize(genome_length);
int trial_pos;
vector<Data> x;
x.resize(p.getNode());
if(i>localSearchChromosomes && k==0)
{
trial_pos=0;
for(int j=0;j<p.getNode();j++)
{
p.nparser[j]->setWeights(bestWeights[j]);
x[j].resize(bestWeights[j].size());
x[j]=bestWeights[j];
}
}
else
{
trial_pos=rand() % genome_count;
pop.getGenome(trial_pos,trial_genome);
p.fitness(trial_genome);
for(int j=0;j<p.getNode();j++)
{
p.nparser[j]->getWeights(x[j]);
}
}
MinInfo Info1;
Info1.problem=&p;
Info1.iters=2001;
double old_value=p.getTrainError();
double value;
int ik=0;
do{
for(int j=0;j<p.getNode();j++)
{
p.neuralparser=p.nparser[j];
p.currentparser=j;
double old_f=p.getTrainError();
int tries=0;
do{
p.neuralparser->getWeights(x[j]);
value=tolmin(x[j],Info1);
fvalue[j]=-value;
if(fabs(value-old_f)<1e-5) break;
old_f=value;
fflush(stdout);
tries++;
if(tries>=20) break;
}while(1);
}
double new_value=p.getTrainError();
if(fabs(new_value-old_value)<1e-5) break;
ik++;
if(ik>=1) break;
}while(1);
//END LOCAL
for(int j=0;j<p.getNode();j++) p.nparser[j]->getWeights(x[j]);
value=p.getTrainError();
if((std::isnan(value) || std::isinf(value)))
{
continue;
}
trial_genome.resize(0);
vector<Genome> subgenome;
subgenome.resize(p.getNode());
int max_length=0;
for(int j=0;j<p.getNode();j++)
{
int d=1;
Converter con(x[j],x[j].size()/(d+2),d);
subgenome[j].resize(genome_length/p.getNode());
con.convert(subgenome[j]);
if(subgenome[j].size()>max_length) max_length=subgenome[j].size();
}
for(int j=0;j<p.getNode();j++)
{
int s=subgenome[j].size();
if(s<max_length)
{
subgenome[j].resize(max_length);
for(int k=s;k<max_length;k++) subgenome[j][k]=0;
}
for(int k=0;k<subgenome[j].size();k++) trial_genome.push_back(subgenome[j][k]);
}
for(int i=0;i<trial_genome.size();i++)
{
if(abs(trial_genome[i])>255) trial_genome[i]=0;
}
double trial_fitness=-value;
pop.setGenome(trial_pos,trial_genome,trial_fitness,p.getNode());
if(fabs(value)<=bestError)
{
for(int j=0;j<p.getNode();j++)
{
bestWeights[j]=x[j];
p.nparser[j]->setWeights(x[j]);
}
bestError=fabs(value);
old_test_error=p.getTestError();
pop.setGenome(0,trial_genome,trial_fitness,p.getNode());
f=trial_fitness;
}
if(value<f)
{
for(int j=0;j<p.getNode();j++)
{
bestWeights[j]=x[j];
p.nparser[j]->setWeights(x[j]);
}
bestError=fabs(value);
old_test_error=p.getTestError();
pop.setGenome(0,trial_genome,trial_fitness,p.getNode());
f=trial_fitness;
}
}
pop.select();
}
if(fabs(bestError)<1e-6) break;
}
for(int j=0;j<p.getNode();j++)
p.nparser[j]->setWeights(bestWeights[j]);
old_test_error=p.getTestError();
printf("TRAIN ERROR =%.10lf\n",bestError);
printf("TEST ERROR =%.10lf\n",old_test_error);
if(output_file!="") p.printOutput(output_file);
}
int main(int argc, char *argv[])
{
parse_cmd_line(argc,argv);
if(argc==1) print_usage();
srand(random_seed);
if(kind=="sode")
runSode();
else
run();
return 0;
}