-
Notifications
You must be signed in to change notification settings - Fork 1
/
LayeredNeural_Genetic_Floats.cpp
214 lines (189 loc) · 5.45 KB
/
LayeredNeural_Genetic_Floats.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
#include <iostream>
#include <vector>
#include <algorithm>
//#define NDEBUG
#include <cassert>
#include <random>
#include <ctime>
#define fori(x) for (int i = 0, fori_size = (x); i < fori_size; i++)
#define forj(x) for (int j = 0, forj_size = (x); j < forj_size; j++)
using namespace std;
inline double randDist(std::uniform_real_distribution<double> d){
static std::random_device rd;
static std::mt19937 gen(rd());
double p = d(gen);
//cout << "(("<< p <<"))";
return p;
}
inline double randStart(){
static std::uniform_real_distribution<double> d(-1.0, 1.0);
return randDist(d);
}
inline double randMut(double r = -1.0){
static double range = 1.0;
if (r > 0.0 && r <= 1.0)range = r;
static std::uniform_real_distribution<double> d(-range, range);
return randDist(d);
}
double squaredDistance(const vector<double>& a, const vector<double>& b){
double sum = 0;
fori(a.size())
sum += (a[i] - b[i]) * (a[i] - b[i]);
return sum;
}
vector<vector<double> > getTestdata(int tests, int testsize){
static std::uniform_real_distribution<double> d(0.0, 1.0);
vector<vector<double> > t;
vector<double> b;
t.reserve(tests);
b.reserve(testsize);
fori(tests){
forj(testsize) b.push_back(randDist(d));
t.push_back(b);
b.clear();
}
return t;
}
class Neuron{
public:
int size;
vector<double> weights;
Neuron(){}
public:
Neuron(int inputLayerSize){
size = inputLayerSize;
fori(size+1) weights.push_back(randStart());
}
double getVal(const vector<double>& prevLayer){
double value = 0;
//assert(prevLayer.size() == size && weights.size() - 1 == size);
fori(size) if (prevLayer[i]) value += prevLayer[i] * weights[i];
return thresholding(value);
}
/*
__________
/
________/
This could be an easy thresholding function - constant, except linear in the middle.
*/
inline double thresholding(double value){
return atan(value/10)*2/3.141592653589; //Between -pi/2 and +pi/2 degrees.
}
Neuron getMutated(){
Neuron n;
n.size = size;
n.weights.reserve(size + 1);
fori(size+1)
n.weights.push_back(weights[i] + randMut());
return n;
}
};
class NeuronLayer{
public:
vector<Neuron> neurons;
NeuronLayer(){}
public:
NeuronLayer(int prevSize, int size){
fori(size)
neurons.push_back(Neuron(prevSize));
}
vector<double> getVals(const vector<double>& prev){
//clock_t c = clock();
vector<double> b;
b.reserve(neurons.size());
fori(neurons.size())
b.push_back(neurons[i].getVal(prev));
//cout << " (" << (clock() - c) / (CLOCKS_PER_SEC / 1000.0) << "ms) ";
return b;
}
NeuronLayer getMutated(){
NeuronLayer n;
fori(neurons.size())
n.neurons.push_back(neurons[i].getMutated());
return n;
}
};
class NeuralNetwork{
private:
int inputSize;
vector<int> sizes;
vector<NeuronLayer> nlayers;
NeuralNetwork(){}
public:
NeuralNetwork(int is, const vector<int>& s){
inputSize = is;
sizes = s;
fori(sizes.size())
nlayers.push_back(NeuronLayer(i==0? inputSize : sizes[i-1], sizes[i]));
}
double getScore(const vector<vector<double> >& testdata, const vector<vector<double> >& correct){
double score = 0;
fori(testdata.size())//(Constant testdata between all things)
score += squaredDistance(correct[i], getVals(testdata[i]));
return score;
}
vector<double> getVals(vector<double> prev){
fori(nlayers.size())
prev = nlayers[i].getVals(prev);
return prev;
}
NeuralNetwork getMutated(){
NeuralNetwork n;
n.inputSize = inputSize;
n.sizes = sizes;
n.nlayers.reserve(nlayers.size());
fori(nlayers.size())
n.nlayers.push_back(nlayers[i].getMutated());
return n;
}
};
vector<double> correctFunction(vector<double> b, int outputSize){
b.clear();
b.resize(outputSize,0.5);
return b;
}
int main(){
const vector<int>sizes = { 5, 5, 5, 5, 5};
const int inputSize = 5;//How many doubles in the input vector. Slightly proportional to overall time.
const int netsPerGeneration = 10;//Number of networks per generation. Proportional to overall time.
const int generations = 100;//Number of generations. Proportional to overall time.
const int tests = 200;//Number of tests applied to each member of a generation. Proportional to overall time.
const int outputSize = sizes[sizes.size() - 1];
vector<vector<double> > testdata;
vector<vector<double> > correct;
vector<double> scores;
int best;
cout << "Program execution begun. Initiating first generation..." << endl;
vector<NeuralNetwork> nets;
fori(netsPerGeneration)
nets.push_back(NeuralNetwork(inputSize,sizes));
cout << "Initiated first generation. Beginning generations..." << endl;
for (int n = 0; n < generations; n++){
cout << "Gen" << n << "... ";
testdata = getTestdata(tests, inputSize);
correct.clear();
fori(testdata.size())
correct.push_back(correctFunction(testdata[i],sizes[sizes.size()-1]));
scores.clear();
//cout << "Scores: ";
fori(netsPerGeneration){
scores.push_back(nets[i].getScore(testdata, correct));
//cout << " " << scores[i];
}
best = 0;
//secondbest = 1;
fori(netsPerGeneration)
if (scores[i] < scores[best])
best = i;
//cout << ". ";
if (n < generations - 1){
cout << "Best = " << scores[best] << ". Mutating... ";
//randMut(1.0 / (n + 1));//It gets more and more precise. (Sets randMut plus/minus)
fori(netsPerGeneration)//Mutate the best and fill the rest of the spots with it.
if (i != best)nets[i] = nets[best].getMutated();
cout << "Done." << endl;
}
}
cout << "Done! Best score was " << scores[best] << ". Press any key to continue.";
cin.get();
}