-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPNodeStruct.cpp
More file actions
176 lines (156 loc) · 4.69 KB
/
Copy pathGPNodeStruct.cpp
File metadata and controls
176 lines (156 loc) · 4.69 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
#include "GPNodeStruct.h"
GPNodeStruct::GPNodeStruct(const GPNodeStruct& other) {
value = other.value;
isLeaf = other.isLeaf;
for (const auto& child : other.children) {
children.push_back(new GPNodeStruct(*child));
}
}
// booleans will return either 0 or 1
double GPNodeStruct::fitness(const std::vector<double>& inputs, const std::vector<std::string>& colNames) const {
if (isLeaf) {
try {
return std::stod(value);
} catch (const std::invalid_argument&) {
for (int i = 0; i < colNames.size(); i++) {
if (colNames[i] == value) {
return inputs[i];
}
}
}
return INFINITY; // can't be calculated, therefore a bad fitness
}
double result = 0.0;
if (children.size() == 0) {
return INFINITY; // no children, can't be calculated
} else if (children.size() == 1) {
result = children[0]->fitness(inputs, colNames);
} else if (children.size() == 2) {
double left = children[0]->fitness(inputs, colNames);
double right = children[1]->fitness(inputs, colNames);
if (value == "+") {
result = left + right;
} else if (value == "-") {
result = left - right;
} else if (value == "*") {
result = left * right;
} else if (value == "/") {
result = protectedDiv(left, right);
} else if (value == "max") {
result = std::max(left, right);
} else if (value == "min") {
result = std::min(left, right);
} else if (value == "tanh") {
// result = 1.0 / (1.0 + std::exp(-left));
result = std::tanh(left);
} else if (value == "sin") {
result = std::sin(left);
} else if (value == "cos") {
result = std::cos(left);
} else if (value == "log") {
if (left <= 0) {
return 0.0;
}
result = std::log(left);
} else if (value == "and") {
result = static_cast<double>(static_cast<bool>(left) && static_cast<bool>(right));
} else if (value == "or") {
result = static_cast<double>(static_cast<bool>(left) || static_cast<bool>(right));
} else if (value == "not") {
result = static_cast<double>(!static_cast<bool>(left));
} else if (value == "<") {
result = static_cast<double>(left < right);
} else if (value == ">") {
result = static_cast<double>(left > right);
} else if (value == "<=") {
result = static_cast<double>(left <= right);
} else if (value == ">=") {
result = static_cast<double>(left >= right);
} else if (value == "==") {
result = static_cast<double>(left == right);
} else if (value == "!=") {
result = static_cast<double>(left != right);
}
}
return result;
}
// DFS
GPNodeStruct* GPNodeStruct::traverseToNth(int& n) const {
if (n == 0) {
return const_cast<GPNodeStruct*>(this);
}
n--;
for (const auto& child : children) {
GPNodeStruct* result = child->traverseToNth(n);
if (result) {
return result;
}
}
return nullptr;
}
GPNodeStruct* GPNodeStruct::findParent(int& n) const {
if (n == 0) {
return nullptr; // No parent for the root node
}
n--;
GPNodeStruct* parent = findParentHelper(n, nullptr);
return parent;
}
GPNodeStruct* GPNodeStruct::findParent(GPNodeStruct* child) const {
if (child == nullptr) {
return nullptr; // No parent for null child
}
for (const auto& c : children) {
if (c == child) {
return const_cast<GPNodeStruct*>(this); // Return this node as the parent
}
}
GPNodeStruct* parent = nullptr;
for (const auto& c : children) {
parent = c->findParent(child);
if (parent) return parent;
}
return nullptr; // No parent found
}
GPNodeStruct* GPNodeStruct::findParentHelper(int& n, GPNodeStruct* parent) const {
if (n == 0) {
return parent;
}
n--;
for (const auto& c : children) {
GPNodeStruct* childResult = c->findParentHelper(n, const_cast<GPNodeStruct*>(this));
if (childResult) return childResult;
}
return nullptr;
}
double GPNodeStruct::protectedDiv(const double& a, const double& b) const {
return (std::abs(b) < 1e-6) ? a : a / b;
}
int GPNodeStruct::treeSize() const {
if (children.empty()) {
return 1;
}
int size = 1; // Count this node
for (const auto& child : children) {
size += child->treeSize();
}
return size;
}
int GPNodeStruct::calcDepth() const {
if (isLeaf) {
return 0;
}
int depth0 = 0;
int depth1 = 0;
int depth2 = 0;
for (const auto& child : children) {
if (child->children.size() == 0) {
depth0 = std::max(depth0, 1);
} else if (child->children.size() == 1) {
depth1 = std::max(depth1, child->calcDepth());
} else if (child->children.size() == 2) {
depth2 = std::max(depth2, child->calcDepth());
}
}
return 1 + std::max(depth0, std::max(depth1, depth2));
}