-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestFS.h
250 lines (244 loc) · 7.03 KB
/
TestFS.h
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
//
// TestFS.h
// LazyPredator
//
// Created by Craig Reynolds on 9/10/20.
// Copyright © 2020 Craig Reynolds. All rights reserved.
//
// Sample FunctionSets for testing and as examples.
#pragma once
#include "FunctionSet.h"
class TestFS
{
public:
// Simple test with only Float and Int types which can be evaluated.
static const FunctionSet& treeEval() { return tree_eval; }
// For testing tree eval for cases including construction class objects.
static const FunctionSet& treeEvalObjects() { return tree_eval_objects; }
// Simple set for testing crossover.
static const FunctionSet& crossover() { return cross_over; }
class ClassC
{
public:
ClassC(int i, int j) : i_(i), j_(j) {}
std::string to_string() const
{
return ("ClassC(" + std::to_string(i_) +
", " + std::to_string(j_) + ")");
}
private:
int i_;
int j_;
};
class ClassB
{
public:
ClassB(float f) : f_(f) {}
std::string to_string() const
{
return "ClassB(" + any_to_string<float>(f_) + ")";
}
private:
float f_;
};
class ClassA
{
public:
ClassA(const ClassB& b, ClassC c) : b_(b), c_(c)
{
constructor_count_++;
}
~ClassA()
{
destructor_count_++;
}
static int getLeakCount()
{
return constructor_count_ - destructor_count_;
}
std::string to_string() const
{
return ("ClassA(" + b_.to_string() + ", " + c_.to_string() + ")");
}
private:
const ClassB& b_;
const ClassC c_;
// Leak check. Count constructor/destructor calls. Should match at exit.
static inline int constructor_count_ = 0;
static inline int destructor_count_ = 0;
};
private:
static inline const FunctionSet tree_eval_objects =
{
{
{
"ClassA",
// ephemeral generator
nullptr,
// to_string
[](std::any a)
{
return std::any_cast<ClassA&>(a).to_string();
},
// jiggler
nullptr,
// deleter
[](std::any a)
{
if (a.has_value())
{
ClassA* t = std::any_cast<ClassA*>(a);
if (t) delete t;
}
}
},
{
"ClassB",
nullptr,
[](std::any a)
{
return std::any_cast<ClassB*>(a)->to_string();
},
nullptr
},
{
"ClassC",
nullptr,
[](std::any a)
{
return std::any_cast<ClassC>(a).to_string();
},
nullptr
},
{ "Float", 0.0f, 1.0f },
{ "Int", 0, 9 }
},
{
{
"ClassA", "ClassA", {"ClassB", "ClassC"},
[](GpTree& t)
{
return std::any(new ClassA(*t.evalSubtree<ClassB*>(0),
t.evalSubtree<ClassC>(1)));
}
},
{
"ClassB", "ClassB", {"Float"},
[](GpTree& t)
{
return std::any(new ClassB(t.evalSubtree<float>(0)));
}
},
{
"ClassC", "ClassC", {"Int", "Int"},
[](GpTree& t)
{
return std::any(ClassC(t.evalSubtree<int>(0),
t.evalSubtree<int>(1)));
}
}
}
};
// Moved "Float" to top in case we want to use that convention.
// std::string root_type = "Float";
static inline const FunctionSet tree_eval =
{
{
{ "Float", 0.0f, 1.0f },
{ "Int", 0, 9 }
},
{
{
"AddInt", "Int", {"Int", "Int"}, [](GpTree& t)
{
return std::any(t.evalSubtree<int>(0) +
t.evalSubtree<int>(1));
}
},
{
"AddFloat", "Float", {"Float", "Float"}, [](GpTree& t)
{
return std::any(t.evalSubtree<float>(0) +
t.evalSubtree<float>(1));
}
},
{
"Floor", "Int", {"Float"}, [](GpTree& t)
{
return std::any(int(std::floor(t.evalSubtree<float>(0))));
}
},
{
"Sqrt", "Float", {"Int"}, [](GpTree& t)
{
return std::any(float(std::sqrt(t.evalSubtree<int>(0))));
}
},
{
"Mult", "Float", {"Float", "Int"}, [](GpTree& t)
{
return std::any(t.evalSubtree<float>(0) *
t.evalSubtree<int>(1));
}
}
}
};
static inline const FunctionSet cross_over =
{
{
{ "Int", 0, 9 }
},
{
{
"P", "Int", {"Int"},
[](GpTree& t)
{
return std::any(t.evalSubtree<int>(0));
}
},
{
"PP", "Int", {"Int", "Int"},
[](GpTree& t)
{
return std::any(t.evalSubtree<int>(0) +
t.evalSubtree<int>(1));
}
},
{
"PPP", "Int", {"Int", "Int", "Int"},
[](GpTree& t)
{
return std::any(t.evalSubtree<int>(0) +
t.evalSubtree<int>(1) +
t.evalSubtree<int>(2));
}
},
{
"Q", "Int", {"Int"},
[](GpTree& t)
{
return std::any(t.evalSubtree<int>(0));
}
},
{
"QQ", "Int", {"Int", "Int"},
[](GpTree& t)
{
return std::any(t.evalSubtree<int>(0) +
t.evalSubtree<int>(1));
}
},
{
"QQQ", "Int", {"Int", "Int", "Int"},
[](GpTree& t)
{
return std::any(t.evalSubtree<int>(0) +
t.evalSubtree<int>(1) +
t.evalSubtree<int>(2));
}
}
},
// Crossover min_size, must be larger than a single Int leaf value.
2
};
};