-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
237 lines (167 loc) · 6.17 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
#include <iostream>
#include <ctime>
#include "FA/swarm.h"
#include "GA/population.h"
using namespace std;
static double stepSize = 0.01;
static double baseAttraction = 0.5;
static double absorption = 1.0;
static unsigned int swarmSize = 50;
static unsigned int iterations = 700;
static unsigned int taskID = 0;
static vector<neuralFireflyStrategy::layer> solution;
static population* p;
static swarm* s;
enum tasksID
{
Exit, OriginalWork, GANNWessingerDefault, FANNWessingerDefault, GANNWessinger, FANNWessinger
};
// Update each time changes in enum are made
static unsigned int maxInput = 5;
int main()
{
exponentialDistribution distribution;
neuralFireflyStrategy::topology topology = {1, 4, 1};
neuralNet nn(&topology);
alternativeNeuralWessingersEvaluator evaluator(&nn);
unsigned int experimentsNum = 1;
/* Setup */
// Set random seed for proper functioning of randomizers.
std::random_device rd;
srand(rd());
clock_t start;
string PAUSE;
/* Setup finished */
// UI xD
cout << "Select task to perform: " << endl;
cout << "0) exit," << endl;
cout << "1) check results from original work," << endl;
cout << "2) train NN using EA and default settings to solve Wessinger's equation," << endl;
cout << "3) train NN using FA and default settings to solve Wessinger's equation," << endl;
cout << "4) train NN using EA to solve Wessinger's equation," << endl;
cout << "5) train NN using FA to solve Wessinger's equation." << endl;
cout << "> ";
cin >> taskID;
while(cin.fail() || taskID > maxInput)
{
cout << "Incorrect input. Please enter correct one." << endl;
cout << "> ";
cin >> taskID;
}
switch(taskID)
{
case OriginalWork:
// NEURAL NET TEST WITH FIXED PARAMETERS WITH EVALUATION
solution = {
{{-1.552, -0.4596, -1.9932, -1.1452, 0.0}},
{{0.2565, -1.8594}, {0.4425, -2.4258}, {0.0024, -1.0052}, {0.5258, -1.5866}}
};
nn.setWeightsFromNeuronsStructure(&solution);
nn.print();
cout << "Error: " << evaluator.evaluate(&solution) << endl << endl;
evaluator.printTestCases(&solution);
// NEURAL NET TEST WITH FIXED PARAMETERS WITH EVALUATION
break;
case GANNWessinger:
// GENERIC ALGORITHM WITH EVALUATION
// Set attributes
// TR TODO: Add validations
cout << "Enter population size:" << endl << ">";
cin >> swarmSize;
cout << "Enter iterations number:" << endl << ">";
cin >> iterations;
for(int ii = 0; ii < 31; ++ii) {
cout << "iteration " << ii << endl;
// Start timer
start = clock();
p = new population(swarmSize, iterations, &nn, &distribution);
p->findSolution();
cout << double(clock() - start) / CLOCKS_PER_SEC << endl;
nn.setWeightsFromNeuronsStructure((vector<neuralFireflyStrategy::layer> *) p->getResult());
//nn.print();
//evaluator.printTestCases(p->getResult());
// Get time
cout << "Elapsed time: " << double(clock() - start) / CLOCKS_PER_SEC << " s." << endl;
}
// GENETIC ALGORITHM WITH EVALUATION
break;
case FANNWessinger:
// FIREFLY ALGORITHM WITH EVALUATION
// TR TODO: Validators needed.
cout << "Enter population size:" << endl << ">";
cin >> swarmSize;
cout << "Enter iterations number:" << endl << ">";
cin >> iterations;
cout << "Enter step size:" << endl << ">";
cin >> stepSize;
cout << "Enter base attraction:" << endl << ">";
cin >> baseAttraction;
cout << "Enter absorption:" << endl << ">";
cin >> absorption;
for(int i = 0; i < 30; ++i) {
cout << "iteration " << i << endl;
// Start timer
start = clock();
s = new swarm(&stepSize, &baseAttraction, &absorption, swarmSize, iterations, &distribution, &nn);
s->findSolution();
cout << double(clock() - start) / CLOCKS_PER_SEC << endl;
nn.setWeightsFromNeuronsStructure((vector<neuralFireflyStrategy::layer> *) s->getResult());
//nn.print();
//evaluator.printTestCases(s->getResult());
// Get time
//cout << "Elapsed time: " << double(clock() - start) / CLOCKS_PER_SEC << " s." << endl;
}
// FIREFLY ALGORITHM WITH EVALUATION
break;
case FANNWessingerDefault:
// FIREFLY ALGORITHM WITH EVALUATION WITH DEFAULT SETTINGS
cout << "Using FF with default settings." << endl;
for(unsigned int i = 0; i < experimentsNum; ++i) {
cout << "Iteration " << i << endl;
swarmSize = 50;
iterations = 700;
baseAttraction = 0.5;
stepSize = 0.01;
absorption = 1.0;
// Start timer
start = clock();
s = new swarm(&stepSize, &baseAttraction, &absorption, swarmSize,
iterations, &distribution, &nn);
s->findSolution();
nn.setWeightsFromNeuronsStructure((vector<neuralFireflyStrategy::layer>*)s->getResult());
//nn.print();
evaluator.printTestCases(s->getResult());
// Get time
cout << "Elapsed time: " << double(clock() - start) / CLOCKS_PER_SEC << " s.\n\n";
}
break;
// FIREFLY ALGORITHM WITH EVALUATION WITH DEFAULT SETTINGS
case GANNWessingerDefault:
// GENETIC ALGORITHM WITH EVALUATION WITH DEFAULT SETTINGS
cout << "Using EA with default settings." << endl;
for(unsigned int i = 0; i < experimentsNum; ++i) {
cout << "Iteration " << i << endl;
// Set attributes
swarmSize = 50;
iterations = 7000;
// Start timer
start = clock();
p = new population(swarmSize, iterations, &nn, &distribution);
p->findSolution();
nn.setWeightsFromNeuronsStructure((vector<neuralFireflyStrategy::layer> *) p->getResult());
//nn.print();
evaluator.printTestCases(p->getResult());
// Get time
cout << "Elapsed time: " << double(clock() - start) / CLOCKS_PER_SEC << " s.\n\n";
}
// GENETIC ALGORITHM WITH EVALUATION WITH DEFAULT SETTINGS
break;
case Exit:
default:
return EXIT_SUCCESS;
}
// Pause before exit
cout << endl;
//system("pause");
return EXIT_SUCCESS;
}