-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGA.cpp
More file actions
213 lines (172 loc) · 6.21 KB
/
Copy pathGA.cpp
File metadata and controls
213 lines (172 loc) · 6.21 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
//
// Created by Connor O'Leary on 11/9/15.
//
#include "GA.h"
#include "float.h"
#include <random>
#include <iostream>
#include <fstream>
#include <string.h>
#include <algorithm>
GA::GA(int _maxGenerations, double _targetError, int _inputNodes, int _hiddenNodes,
int _hiddenLayers, int _outputNodes,
string _activateHidden, string _activateOutput) {
// Assign ivars
maxGenerations = _maxGenerations;
targetMSE = _targetError;
inputNodesN = _inputNodes;
hiddenNodesPerLayer = _hiddenNodes;
hiddenLayerCount = _hiddenLayers;
outputNodesN = _outputNodes;
hiddenActivation = _activateHidden;
outputActivation = _activateOutput;
// Assign learning rates heuristically
overallLearningRate = 1 / pow(2 * 50, 0.5);
cwLearningRate = 1 / pow(2 * pow(50, 0.5), 0.5);
}
MultilayerNN GA::train(vector<vector<double>> *_dataset) {
int lowDeltaCounter = 0;
/*random_device rd; // Initialize random device & distribution
uniform_int_distribution<u_long> dist(0, 50 - 1);
normal_distribution<double> norm(0, 1);*/
double currentMinimumError = DBL_MAX;
double previousMinimumError;
vector<Chromosome> selectionChroms(50 * children_parent_ratio + 50);
Chromosome currentMin;
fstream resultStream;
resultStream.open("run_GA.csv", ofstream::out | ofstream::trunc);
// Check for stream error
if (resultStream.fail()) {
cerr << "open stream failure at rs: " << strerror(errno) << '\n';
}
// Save dataset
dataset = *_dataset;
// Init population for this training run
populationSetup();
// Initial network run to get errors
runNetworks();
// Generation loop
// Continue until max gens. reached or error reduces to threshold
while (generation <= maxGenerations && targetMSE < currentMinimumError && lowDeltaCounter < 100) {
// Clear offspring
offspring.clear();
selectionChroms.clear();
// Generate offspring, add into temporary offspring pool
for (int i = 0; i < population.size()/2; i++) {
// select the best two parents out of ten random chromosomes
random_shuffle(population.begin(), population.end());
Chromosome parents[2];
selection(parents);
// Create child via recombination
Chromosome children[2]={population[0],population[1]};
crossover(parents,children);
offspring.push_back(children[0]);
offspring.push_back(children[1]);
}
if (population.size()%2==1){
// if the population is odd, add one more child
random_shuffle(population.begin(), population.end());
Chromosome parents[2];
selection(parents);
// Create child via recombination
Chromosome *children;
crossover(parents, children);
offspring.push_back(children[0]);
}
// Overall random number from N(0,1) for this generation
//double globalTerm = norm(rd);
// Mutate offspring
for (auto &c : offspring) {
mutate(c);
}
// Run offspring networks
for (int i = 0; i < offspring.size(); i++) {
offspring[i].nn.run(dataset);
}
// Clear population
population.clear();
population=offspring;
previousMinimumError=currentMinimumError;
//find minimum error
for (int i = 0; i < population.size(); i++) {
if(population[i].nn.lastMSE<currentMinimumError) {
currentMinimumError=population[i].nn.lastMSE;
}
}
if(previousMinimumError>currentMinimumError){
lowDeltaCounter=0;
cout << "Generation " << generation << ": " << currentMinimumError << endl;
}
else{
lowDeltaCounter++;
}
// Output result every 50 gens
if (generation % 50 == 0) {
resultStream << generation << "," << currentMinimumError << endl;
}
//cout << "Generation " << generation << ": " << currentMinimumError << endl;
// Next generation
generation++;
}
resultStream.close();
return currentMin.nn;
}
void GA::runNetworks() {
for (int i = 0; i < population.size(); i++) {
population[i].nn.run(dataset);
}
}
void GA::selection(Chromosome parents[2]) {
//get the first 10 chromosomes and use select the best
parents[0]=population[0];
parents[1]=population[1];
// Create child network by taking average of each weight from parents 7t80
for (int i = 1; i < 25; i++) {
if(parents[0].nn.lastMSE>population[i].nn.lastMSE){
parents[1]=parents[0];
parents[0]=population[i];
}
}
}
GA::Chromosome* GA::crossover(Chromosome* p, Chromosome* two_children) {
//two_children = {p[0],p[1]};
double temp;
// Create child network by taking average of each weight from parents 7t80
for (int i = 0; i < two_children[0].nn.weights.size(); i++) {
for (int j = 0; j < two_children[0].nn.weights[i].size(); j++) {
//crossover the individual value a 5th of the time
if(rand()>__RAND_MAX/10){
temp=two_children[0].nn.weights[i][j];
two_children[0].nn.weights[i][j]=two_children[1].nn.weights[i][j];
two_children[1].nn.weights[i][j]=temp;
}
}
}
// Child step size is average of parents'
/*child.stepSize = (p1.stepSize + p2.stepSize) / 2;*/
return &two_children[0];
}
void GA::mutate(Chromosome &c) {
normal_distribution<double> norm(0, 1);
random_device rd;
// Mutate object function: mutate each weight
for (int i = 0; i < c.nn.weights.size(); i++) {
for (int j = 0; j < c.nn.weights[i].size(); j++) {
if (rand()<__RAND_MAX/2) {
c.nn.weights[i][j] += c.nn.weights[i][j] * norm(rd);
}
}
}
}
void GA::populationSetup() {
generation = 0;
population.clear();
// init networks
initPopulation();
// Init population
for (auto &nn : networks) {
Chromosome *p = new Chromosome();
p->nn = nn;
population.push_back(*p);
}
}