-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
325 lines (291 loc) · 8.38 KB
/
Copy pathmain.cpp
File metadata and controls
325 lines (291 loc) · 8.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <QCoreApplication>
# include <METHODS/gradientdescent.h>
# include <METHODS/multistart.h>
# include <METHODS/genetic.h>
# include <METHODS/bfgs.h>
# include <METHODS/lbfgs.h>
# include <METHODS/differentialevolution.h>
# include <METHODS/ipso.h>
# include <METHODS/neuralminimizer.h>
# include <METHODS/parallelde.h>
# include <METHODS/parallelpso.h>
# include <METHODS/neldermead.h>
# include <METHODS/adam.h>
# include <METHODS/armadillo1.h>
# include <METHODS/simanmethod.h>
# include <METHODS/usermethod.h>
# include <METHODS/ofalgorithm.h>
# include <METHODS/gwooptimizer.h>
# include <OPTIMUS/statistics.h>
#ifdef OPTIMUS_ARMADILLO
# include <MLMODELS/functionalrbf.h>
#endif
# include <OPTIMUS/problemloader.h>
# include <OPTIMUS/methodloader.h>
# include <QVector>
# include <QDebug>
# include <QStringList>
void error(QString message);
//QVector<Optimizer*> method;
QStringList methodName;
QVector<QStringList> methodParams;
Problem *mainProblem = NULL;
QVector<Parameter> mainParams;
QJsonObject problemParams;
MethodLoader *methodLoader = NULL;
QString header="==========================================================================";
void printOption(QString fullName,
QString description,QString value)
{
QString s=QString("--%1 %2 %3 %4").arg(fullName,-27).
arg(description,-35).arg("Default value:",-15).arg(value,-10);
qDebug().noquote()<<s;
}
void makeMainParams()
{
mainParams<<Parameter("help","","Show help screen");
mainParams<<Parameter("opt_method","Genetic","Used Optimization method");
mainParams<<Parameter("opt_problem","test2n","Used Optimization problem");
mainParams<<Parameter("opt_seed","1","Random Seed");
mainParams<<Parameter("opt_iters","30","Number of iterations");
mainParams<<Parameter("opt_threads","1","Number of threads");
QStringList t;
t<<"no"<<"yes";
mainParams<<Parameter("opt_enabletesting",t[0],t,"Enable or disable testing. The default value is no.");
}
void loadMethods()
{
methodLoader = new MethodLoader();
methodName = methodLoader->getMethodList();
for(int i=0;i<methodName.size();i++)
methodParams<<methodLoader->getMethodParamsNames(methodName[i]);
}
void loadProblem()
{
QString problemName = "";
bool found = false;
for(int i=0;i<mainParams.size();i++)
{
if(mainParams[i].getName()=="opt_problem")
{
problemName = mainParams[i].getValue();
break;
}
}
mainProblem = ProblemLoader::getProblemFromName(problemName);
if(mainProblem == NULL)
error("Problem not found!");
mainProblem->init(problemParams);
}
void unloadMethods()
{
delete methodLoader;
}
void unloadProblem()
{
if(mainProblem!=NULL)
delete mainProblem;
}
void printHelp()
{
qDebug().noquote()<<"MAIN PARAMETERS";
qDebug()<<header;
for(int i=0;i<mainParams.size();i++)
{
Parameter pt = mainParams[i];
printOption(pt.getName(),pt.getHelp(),pt.getValue());
}
for(int i = 0;i<methodName.size();i++)
{
qDebug()<<"METHOD: "<<methodName[i];
qDebug()<<header;
for(int j=0;j<methodParams[i].size();j++)
{
Parameter pt =
methodLoader->getSelectedMethod(methodName[i])->getParam(methodParams[i][j]);
if(pt.getName().startsWith("opt_")) continue;
printOption(pt.getName(),pt.getHelp(),pt.getValue());
}
qDebug()<<header;
}
qApp->exit(0);
exit(0);
}
void checkParameter(
QString name,QString value,
QString fullName,
QString ¶meter)
{
if(name == fullName)
parameter = value;
}
void error(QString message)
{
printf("Fatal error %s \n",message.toStdString().c_str());
qApp->exit(0);
exit(0);
}
void parseCmdLine(QStringList args)
{
QString lastParam="";
for(int i=1;i<args.size();i++)
{
if(args[i]=="--help") printHelp();
QStringList sp=args[i].split("=");
QString name = sp[0];
sp[0]=sp[0].replace("--","");
QString value = sp[1];
if(value=="")
{
error(QString("Param %1 is empty.").arg(value));
}
bool found = false;
for(int j=0;j<mainParams.size();j++)
{
Parameter pt = mainParams[j];
if(pt.getName()==sp[0])
{
mainParams[j].setValue(sp[1]);
found = true;
break;
}
}
if(!found)
{
for(int j=0;j<methodName.size();j++)
{
for(int k=0;k<methodParams[j].size();k++)
{
Parameter pt =
methodLoader->getSelectedMethod(methodName[j])->getParam(methodParams[j][k]);
if(pt.getName()==sp[0])
{
methodLoader->getSelectedMethod(methodName[j])->setParam(sp[0],sp[1]);
found = true;
}
}
}
}
if(!found)
{
problemParams[sp[0]]=sp[1];
}
}
}
void runMethod()
{
QString mname = "";
int index =-1;
for(int i=0;i<mainParams.size();i++)
{
if(mainParams[i].getName()=="opt_method")
{
mname = mainParams[i].getValue();
for(int j=0;j<methodName.size();j++)
{
if(methodName[j]==mname)
{
index = j;
break;
}
}
break;
}
}
methodLoader->getSelectedMethod(methodName[index])->setProblem(mainProblem);
methodLoader->getSelectedMethod(methodName[index])->solve();
}
int getIters()
{
for(int i=0;i<mainParams.size();i++)
{
Parameter p = mainParams[i];
if(p.getName()=="opt_iters")
return p.getValue().toInt();
}
return 0;
}
void runAllTests(int iterations) {
int matchFound = 0;
int triesCount = 0;
QStringList methodList;
methodList<<"Multistart"<<"Armadillo"<<"Genetic"<<"iPso"<<"NelderMead"<<"DifferentialEvolution";
for (int i = 0; i < methodName.size()-1; i++) {
if(methodList.contains(methodName[i])==false) continue;
qDebug() << "Running method: " << methodName[i];
Statistics stat;
for (int t = 1; t <= iterations; t++) {
srand(t);
triesCount++;
loadProblem();
methodLoader->getSelectedMethod(methodName[i])->setProblem(mainProblem);
methodLoader->getSelectedMethod(methodName[i])->solve();
stat.addProblem(mainProblem);
Data xx = mainProblem->getBestx();
double yy = mainProblem->getBesty();
bool match_found = false;
if (mainProblem->hasOptimum()) {
match_found = fabs(mainProblem->getKnownOptimumValue() - yy) <= 1e-6;
}
if (match_found) {
matchFound++;
}
unloadProblem();
}
stat.printStatistics();
}
qDebug() << "Percentage of global minimum found: " << matchFound*100.0/triesCount<<"%";
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc,argv);
setlocale(LC_ALL,"C");
makeMainParams();
loadMethods();
parseCmdLine(app.arguments());
int times = getIters();
for(int i=0;i<mainParams.size();i++)
{
if(mainParams[i].getName()=="opt_enabletesting")
{
if(mainParams[i].getValue()=="yes")
{
runAllTests(times);
unloadMethods();
return 0;
}
}
}
unloadProblem();
Statistics stat;
Data xx;
double yy;
int match_found = 0;
bool hasKnownSolution = false;
for(int t=1;t<=times;t++)
{
srand(t);
loadProblem();
runMethod();
stat.addProblem(mainProblem);
/**
* TESTING
*/
xx= mainProblem->getBestx();
yy = mainProblem->getBesty();
if(mainProblem->hasOptimum())
{
hasKnownSolution =true;
match_found+=fabs(mainProblem->getKnownOptimumValue()-mainProblem->getBesty())<=1e-6;
}
unloadProblem();
}
stat.printStatistics();
if(hasKnownSolution)
{
printf("Percentage of known global minimum found: %.2lf%%\n",
match_found*100.0/times);
}
unloadMethods();
return 0;
}