-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
85 lines (66 loc) · 1.87 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
#include "Dataset.hpp"
#include "NeuralNetwork.hpp"
#include "Solution.hpp"
#include "crossing.hpp"
#include "utils.hpp"
#include <cassert>
#include <iostream>
#include <random>
Solution best;
void killHandler(int signum)
{
cout << "received signal: " << signum << endl;
cout << "best mean error: " << best.err << endl;
cout << endl;
for (auto d : best.sol) {
cout << d << " ";
}
cout << endl;
exit(signum);
}
int main(int argc, char** argv)
{
signal(SIGINT, killHandler);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string datasetPath = "./dataset.txt";
if (argc > 1) {
datasetPath = argv[1];
}
Dataset set(datasetPath.c_str());
vector<unsigned int> layout({ 2, 8, 4, 3 });
NeuralNetwork ann(layout);
const unsigned int populationSize = 5;
const unsigned int maxIter = 100000;
//initialize population
vector<Solution> population;
population.reserve(populationSize);
for (unsigned int i = 0; i < populationSize; ++i) {
population.push_back(Solution(ann.size));
population.back().err = ann.calcError(set, population.back().sol);
population.back().fit = 1 / population.back().err;
}
//mutate operator
Mutate mutate(0.1, 2, 0.95, 0.02);
for (unsigned int currentIter = 0; currentIter < maxIter; ++currentIter) {
scaleFitness(population, best);
vector<Solution> nextGen;
nextGen.reserve(populationSize);
cout << "iter: " << currentIter << ", best MSE : " << best.err << endl;
nextGen.push_back(best);
while (nextGen.size() != populationSize) {
auto mom = select(population);
auto dad = select(population);
//TODO
auto child = mom * dad;
mutate(child.sol);
child.err = ann.calcError(set, child.sol);
child.fit = 1 / child.err;
nextGen.push_back(child);
}
population = nextGen;
}
//log best to console on kill
killHandler(0);
return 0;
}