-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGP.cpp
More file actions
296 lines (242 loc) · 9.18 KB
/
Copy pathGP.cpp
File metadata and controls
296 lines (242 loc) · 9.18 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
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
#include "GP.h"
GP::GP() {
trainData = std::vector<Mushroom*>();
testData = std::vector<Mushroom*>();
population = std::vector<GP_Node*>();
}
GP::~GP() {}
void GP::addMushroomToTrain(Mushroom* m) {
trainData.push_back(m);
}
void GP::addMushroomToTest(Mushroom* m) {
testData.push_back(m);
}
void GP::generatePopulation() {
for (int i = 0; i < populationSize; i++) {
population.push_back(new GP_Node());
generateTree(maxDepth, population[i]);
float fitness = (std::rand() % 100) / 100.0 - 0.5;
population[i]->setFitness(fitness);
// std::cout << "Fitness: " << std::fixed << fitness << std::endl;
// print(population[i]);
}
// update best individual
for (int i = 0; i < populationSize; i++) {
if (population[i]->getFitness() > maxFitness) {
maxFitness = population[i]->getFitness();
bestIndividual = population[i];
}
}
}
void GP::tournamentSelection() {
for (int i = 0; i < populationSize; i++) {
GP_Node* parent1 = getRandomIndividual(population[i]);
GP_Node* parent2 = getRandomIndividual(population[i]);
GP_Node* pop = population[i];
crossover(parent1, parent2);
if (pop->getFitness() > maxFitness) {
maxFitness = pop->getFitness();
bestIndividual = pop;
}
}
}
GP_Node* GP::getRandomIndividual(GP_Node* h) {
return h->getRandomGP_Node();
}
void GP::crossover(GP_Node* parent1, GP_Node* parent2) {
if (std::rand() % 100 < crossoverRate * 100) {
GP_Node* temp = parent1->copy(parent1);
parent1->crossover(parent2);
parent2->crossover(temp);
}
mutation(parent1);
mutation(parent2);
}
void GP::mutation(GP_Node* individual) {
if (std::rand() % 100 < mutationRate * 100) {
individual->mutate();
}
}
void GP::generateTree(int depth, GP_Node* h) {
h->generateTree(depth, numFeatures, trainData);
}
void GP::train() {
std::ofstream file("GP_error.csv");
file << "Generation,TrainingAcc" << std::endl;
for (int generation = 0; generation < numGenerations; generation++) {
// for (int i = 0; i < populationSize; i++) {
// float fit = evaluateFitness(population[i], trainData[i]);
// population[i]->setFitness(fit);
// }
for (int i = 0; i < populationSize; i++) {
tournamentSelection();
}
float trainingAccuracy = calculateAccuracy(trainData, bestIndividual);
std::cout << "Generation " << generation << " - Training Accuracy: " << trainingAccuracy << std::endl;
std::ofstream file("GP_error.csv", std::ios_base::app);
file << generation << "," << trainingAccuracy << std::endl;
}
float testingAccuracy = calculateAccuracy(testData, bestIndividual);
std::cout << "Testing Accuracy: " << testingAccuracy << std::endl;
}
void GP::test() {
float correct = 0.0;
std::cout << "Output\t\tActual\tExpected" << std::endl;
std::vector<bool> allPredicted = std::vector<bool>();
std::vector<bool> allActual = std::vector<bool>();
std::vector<float> outputs = std::vector<float>();
for (int i = 0; i < testData.size(); i++) {
// float outputValue = feedforward(testData[i], false);
// bool predicted = classify(testData[i]);
// bool expected = testData[i]->getMushroomClass();
float outputValue = classify(testData[i], bestIndividual);
float predicted = outputValue < 0.8;
float expected = testData[i]->getMushroomClass();
allPredicted.push_back(predicted);
allActual.push_back(expected);
outputs.push_back(outputValue);
if (predicted == expected) {
std::cout << "\033[32m" << std::fixed << outputValue << "\t" << predicted << "\t\t" << expected << "\033[0m" << std::endl;
correct++;
}
else {
std::cout << "\033[31m" << std::fixed << outputValue << "\t" << predicted << "\t\t" << expected << "\033[0m" << std::endl;
}
}
std::cout << std::endl << "\033[34m" << "Statistics" << "\033[0m" << std::endl;
stats(allPredicted, allActual);
std::cout << std::endl << "\033[36m" << "Output Statistics:" << "\033[0m" << std::endl;
float mean = 0.0f;
for (int i = 0; i < outputs.size(); i++) {
mean += outputs[i];
}
mean /= outputs.size();
std::cout << "Mean: " << mean << std::endl;
float variance = 0.0f;
for (int i = 0; i < outputs.size(); i++) {
variance += pow(outputs[i] - mean, 2);
}
variance /= outputs.size();
std::cout << "Variance: " << variance << std::endl;
float stdDev = sqrt(variance);
std::cout << "Standard Deviation: " << stdDev << std::endl;
float maxVal = outputs[0];
float minVal = outputs[0];
for (int i = 0; i < outputs.size(); i++) {
if (outputs[i] < minVal) minVal = outputs[i];
if (outputs[i] > maxVal) maxVal = outputs[i];
}
float range = maxVal - minVal;
std::cout << "Range: " << range << std::endl << std::endl;
outputsToCSV();
}
// bool GP::classify(Mushroom* m, GP_Node* h) {
// float fitness = evaluateFitness(h);
// // std::cout << fitness << std::endl;
// return evaluateFitness(h) > 0.5;
// }
float GP::classify(Mushroom* m, GP_Node* h) {
float fitness = h->classifyBase(m);
return sigmoid(fitness);
}
void GP::print(GP_Node* head) {
head->print(head->getDepth());
}
// float GP::evaluateFitness(GP_Node* individual, Mushroom* data) {
// // std::cout << individual->getLeft() << std::endl;
// // std::cout << individual->getRight() << std::endl;
// // individual->evaluateFitness(trainData);
// // traverse the tree and evaluate the fitness starting at individual
// return individual->classify(data);
// }
// float GP::evaluateFitnessHelper(GP_Node* n) {
// // Implementation of the evaluateFitnessHelper function
// if (n == nullptr) {
// return 0.0f;
// }
// if (n->isLeaf()) {
// return n->getFitness();
// }
// float leftValue = 0.0f;
// float rightValue = 0.0f;
// if (n->getLeft() != nullptr) {
// leftValue = evaluateFitnessHelper(n->getLeft());
// }
// if (n->getRight() != nullptr) {
// rightValue = evaluateFitnessHelper(n->getRight());
// }
// switch (n->getOperation()) {
// case 0:
// return leftValue + rightValue;
// case 1:
// return leftValue - rightValue;
// case 2:
// return leftValue * rightValue;
// case 3:
// return rightValue != 0.0f ? leftValue / rightValue : 0.0f;
// case 4:
// return pow(leftValue, rightValue);
// case 5:
// return sin(leftValue);
// case 6:
// return cos(rightValue);
// default:
// return 0.0;
// }
// }
float GP::sigmoid(float x) {
return 1 / (1 + exp(-x));
}
void GP::stats(const std::vector<bool>& predicted, const std::vector<bool>& actual) {
int truePositive = 0;
int falsePositive = 0;
int trueNegative = 0;
int falseNegative = 0;
for (size_t i = 0; i < predicted.size(); ++i) {
if (predicted[i] == true && actual[i] == true) {
truePositive++;
} else if (predicted[i] == true && actual[i] == false) {
falsePositive++;
} else if (predicted[i] == false && actual[i] == true) {
falseNegative++;
} else if (predicted[i] == false && actual[i] == false) {
trueNegative++;
}
}
// Precision = True Positives / (True Positives + False Positives)
float precision = (float)(truePositive) / (truePositive + falsePositive);
// Recall = True Positives / (True Positives + False Negatives)
float recall = (float)(truePositive) / (truePositive + falseNegative);
// Accuracy = (True Positives + True Negatives) / Total
float accuracy = (float)(truePositive + trueNegative) / predicted.size();
// F1 Score = 2 * (Precision * Recall) / (Precision + Recall)
float f1Score = 2.0f * (precision * recall) / (precision + recall);
std::cout << "Precision: " << precision << std::endl;
std::cout << "Recall: " << recall << std::endl;
std::cout << "Accuracy: " << accuracy << std::endl;
std::cout << "F1 Score: " << f1Score << std::endl;
}
void GP::outputsToCSV() {
std::ofstream file("GP_outputs.csv");
file << "Output,Actual,Expected" << std::endl;
for (int i = 0; i < testData.size(); i++) {
float outputValue = classify(testData[i], bestIndividual);
bool predicted = outputValue < 0.8;
bool expected = testData[i]->getMushroomClass();
file << outputValue << "," << predicted << "," << expected << std::endl;
}
file.close();
std::cout << "Outputs written to GP_outputs.csv" << std::endl;
}
float GP::calculateAccuracy(const std::vector<Mushroom*>& data, GP_Node* individual) {
float correct = 0.0;
for (int i = 0; i < data.size(); i++) {
float outputValue = classify(data[i], individual);
bool predicted = outputValue < 0.8;
bool expected = data[i]->getMushroomClass();
if (predicted == expected) {
correct++;
}
}
return correct / data.size();
}