-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainNGSuite.cpp
321 lines (277 loc) · 10.1 KB
/
mainNGSuite.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
/*
* This file is part of NeuralGas.
*
* NeuralGas is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* NeuralGas 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with NeuralGas. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <iostream>
#include "NeuralGasSuite.h"
#include "GrowingNeuralGas/Testing/ErrorTesting.h"
#include "GrowingNeuralGas/MergeGrowingNeuralGas/MGNGAlgorithm.h"
#include "GrowingNeuralGas/MergeGrowingNeuralGas/CDNAlgorithm.h"
#include "GrowingNeuralGas/GNGAlgorithm.h"
#include "GrowingNeuralGas/ErrorBasedGNGAlgorithm/EBGNGAlgorithm.h"
#include "GrowingNeuralGas/LifelongRobustGNGAlgorithm/LLRGNGAlgorithm.h"
#include "DataGenerator/MackeyGlass.h"
#include "DataGenerator/NoisyAutomata.h"
#include <math.h>
using namespace std;
using namespace neuralgas;
double func(const unsigned int& time)
{return 0.5 ;}
double constalpha(const unsigned int& time)
{return 0.5;}
double constbeta(const unsigned int& time)
{return 0.75;}
double constgamma(const unsigned int& time)
{return 88;}
double constepsilonw(const unsigned int& time)
{return 0.05;}
double constepsilonn(const unsigned int& time)
{return 0.0006;}
double constdelta(const unsigned int& time)
{return 0.5;}
double consteta(const unsigned int& time)
{return 0.99995;}
double consttheta(const unsigned int& time)
{return 100;}
double constlambda(const unsigned int& time)
{return 600 ;}
double funcalpha(const unsigned int& time)
{
double random_alpha = double(rand()) / double(50000);
return random_alpha;
}
double funcbeta(const unsigned int& time)
{
return 0.75 ;
}
double funcgamma(const unsigned int& time)
{return 1+sqrt(time);}
double funclambda(const unsigned int& time)
{return 3 ;}
double functheta(const unsigned int& time)
{
return 100;
}
enum _algorithm { _gng, _ebgng, _mgng, _cdn, _llbgng };
enum _dataset {_mg, _na };
void display_error_msg (char *argv[])
{
cerr << "Usage: " << argv[0] << " gng/ebgng/mgng/cdn/llrgng mg/na size" << endl;
exit(1);
}
int main(int argc, char *argv[])
{
if (argc < 4)
display_error_msg (argv);
unsigned int dataset = _na;
if (string(argv[2]) == "mg")
dataset = _mg;
else if (string(argv[2]) == "na")
dataset = _na;
else
display_error_msg (argv);
GNGModul<double, int> *gng = NULL;
unsigned int algorithm = _gng;
//MGNGAlgorithm<double,int>* mgng = new MGNGAlgorithm<double,int>(1);
//GNGAlgorithm<double,int>* gng = new GNGAlgorithm<double,int>(1);
//EBGNGAlgorithm<double,int>* ebgng = new EBGNGAlgorithm<double,int>(2);
//CDNAlgorithm<double,int>* cdn = new CDNAlgorithm<double,int>(1);
if (string(argv[1]) == "gng")
{
gng = new GNGAlgorithm<double,int>(dataset + 1);
algorithm = _gng;
}
else if (string(argv[1]) == "ebgng")
{
gng = new EBGNGAlgorithm<double,int>(dataset + 1);
algorithm = _ebgng;
}
else if (string(argv[1]) == "mgng")
{
gng = new MGNGAlgorithm<double,int>(dataset + 1);
algorithm = _mgng;
}
else if (string(argv[1]) == "cdn")
{
gng = new CDNAlgorithm<double,int>(dataset + 1);
algorithm = _cdn;
}
else if (string(argv[1]) == "llrgng")
{
const unsigned int error_window_size = 81;
gng = new LLRGNGAlgorithm<double,int>(dataset + 1, error_window_size);
algorithm = _llbgng;
}
else
display_error_msg (argv);
int sizeofdata=atoi (argv[3]);
NeuralGasSuite<double,int> ng;
DataGenerator<double> *dG;
if (dataset == _mg)
{
dG = new MackeyGlass;
static_cast<MackeyGlass*>(dG)->setPastTimeSteps(17);
static_cast<MackeyGlass*>(dG)->setBoundary(0.4);
static_cast<MackeyGlass*>(dG)->setPower(10);
}
else if (dataset == _na)
{
dG = new NoisyAutomata;
double sigma, transprob;
cout << "Sigma: ";
cin >> sigma;
cout << "Transition prob.: ";
cin >> transprob;
static_cast<NoisyAutomata*>(dG)->setSigma (sigma);
static_cast<NoisyAutomata*>(dG)->setTransProb (transprob);
}
ng.setDataGenerator(dG);
//ng.setDataGenerator(na);
dG->generate(sizeofdata);
//na->generate(sizeofdata);
//vector<Vector<double>*>* data = na->getData();
vector<Vector<double>*>* data = dG->getData();
for (unsigned int i=0; i < data->size(); i++)
{
std::cout <<data->operator[](i)->operator[](0);
if (dataset == _na)
std::cout << " "<< data->operator[](i)->operator[](1);
std::cout << std::endl;
}
ng.add(gng);
ng.setData();
ng.setRefVectors(2);
for(int i=0; i < NUM_PARAM; i++)
{
gng->setFuncArray(func,i);
//ebgng->setFuncArray(func,i);
//cdn->setFuncArray(func,i);
}
// mgng->setFuncArray(constalpha,0);
// mgng->setFuncArray(constbeta,1);
// mgng->setFuncArray(constgamma,2);
// mgng->setFuncArray(constdelta,3);
// mgng->setFuncArray(constepsilonw,4);
// mgng->setFuncArray(constepsilonn,5);
// mgng->setFuncArray(consttheta,6);
// mgng->setFuncArray(consteta,7);
// mgng->setFuncArray(constlambda,8);
if (algorithm == _mgng)
{
gng->setFuncArray(constalpha,0);
gng->setFuncArray(constbeta,1);
gng->setFuncArray(constgamma,2);
gng->setFuncArray(constdelta,3);
gng->setFuncArray(constepsilonw,4);
gng->setFuncArray(constepsilonn,5);
gng->setFuncArray(consttheta,6);
gng->setFuncArray(consteta,7);
gng->setFuncArray(constlambda,8);
}
else if (algorithm == _cdn)
{
// cdn->setFuncArray(constalpha,0);
// cdn->setFuncArray(constbeta,1);
// cdn->setFuncArray(constgamma,2);
// cdn->setFuncArray(constdelta,3);
// cdn->setFuncArray(constepsilonw,4);
// cdn->setFuncArray(constepsilonn,5);
// cdn->setFuncArray(consttheta,6);
// cdn->setFuncArray(consteta,7);
// cdn->setFuncArray(constlambda,8);
gng->setFuncArray(constalpha,0);
gng->setFuncArray(constbeta,1);
gng->setFuncArray(constgamma,2);
gng->setFuncArray(constdelta,3);
gng->setFuncArray(constepsilonw,4);
gng->setFuncArray(constepsilonn,5);
gng->setFuncArray(consttheta,6);
gng->setFuncArray(consteta,7);
gng->setFuncArray(constlambda,8);
}
else if (algorithm == _gng)
{
gng->setFuncArray(constgamma,3);
gng->setFuncArray(functheta,7);
gng->setFuncArray(funclambda,6);
//gng->setStoppingCriterion (global_error);
gng->setStoppingCriterion (epochs);
gng->setMaxEpochs (100);
gng->setSamplingMode(randomly);
//(static_cast<GNGAlgorithm<double,int>*>(gng))->setMinGlobalError (0.01);
}
else if (algorithm == _ebgng)
{
// ebgng->setFuncArray(constgamma,3);
// ebgng->setFuncArray(functheta,7);
// ebgng->setFuncArray(funclambda,8);
static_cast<EBGNGAlgorithm<double,int>*>(gng)->setErrorThreshold(0.03);
gng->setFuncArray(constgamma,3);
gng->setFuncArray(functheta,7);
gng->setFuncArray(funclambda,8);
gng->setSamplingMode(randomly);
// gng->setStoppingCriterion (epochs);
gng->setStoppingCriterion (stability);
// gng->setMaxEpochs (1000000000);
}
else if (algorithm == _llbgng)
{
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setTimeWindows (50, 30, sizeofdata);
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setLearningRates (0.1, 0.001);
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setInsertionRate (sizeofdata);
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setAdaptationThreshold (0.0);
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setMaximalEdgeAge (50);
// static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setDataAccuracy (0.001);
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setDataAccuracy (0.0001);
if (dataset == _na)
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setModelEfficiencyConst (1.0);
else if (dataset == _mg)
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setModelEfficiencyConst (1.3);
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setMaxEpochsErrorReduction (3);
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setMaxEpochsMDLReduction (400);
static_cast<LLRGNGAlgorithm<double,int>*>(gng)->setSamplingMode (randomly);
//gng->setStoppingCriterion (epochs);
//gng->setMaxEpochs (100);
//(static_cast<LLBGNGAlgorithm<double,int>*>(gng))->setMaxNodes(5);
gng->setStoppingCriterion (stability);
}
//(static_cast< CDNAlgorithm<double,int>* > (ng[1]))->setEnergy(0.1);
//na->save("data.txt");
dG->save("data.dat");
ng.run();
std::vector<double> errors;
for (int i=0; i < ng.size(); i++)
{
std::cout << "Algorithm " << i << std::endl;
//errors = ng.getErrors(i,500);
bool random_data = false;
errors = ng.getErrors(i,sizeofdata,random_data);
double total_error=0.0;
ng[i]->showGraph();
ng[i]->save ("nodes.dat");
for(unsigned int j=0; j < errors.size(); j++)
{
// std::cout << errors [j] << " ";
total_error+=errors[j];
}
std::cout << "Avg error: "<< total_error / sizeofdata <<std::endl;
std::cout << std::endl;
}
std::cout << std::endl;
dG->reset ();
delete dG;
delete gng;
return EXIT_SUCCESS;
}