ECF 1.5
Primitives.cpp
1#include "ECF/ECF.h"
2
3
4
5// custom terminal class - boolean vector
7{
8public:
9 std::vector<bool> value_;
10
11 BoolV()
12 {
13 nArguments_ = 0;
14 }
15 void execute(void* result, Tree::Tree& tree)
16 {
17 std::vector<bool>& res = *(std::vector<bool>*)result;
18 res = value_;
19 }
20 void setValue(void* value)
21 {
22 value_ = *(std::vector<bool>*)value;
23 }
24 ~BoolV()
25 { }
26};
27
28
29
30//
31// Boolean function primitives
32//
34{
35public:
36 Or()
37 {
38 nArguments_ = 2;
39 name_ = "OR";
40 }
41
42 void execute(void* result, Tree::Tree& tree)
43 {
44 std::vector<bool>& vOr = *(std::vector<bool>*)result;
45 uint size = (uint) vOr.size();
46
47 std::vector<bool> arg1(size), arg2(size);
48
49 getNextArgument(&arg1, tree);
50 getNextArgument(&arg2, tree);
51
52 for(uint i = 0; i < size; i++)
53 vOr[i] = arg1[i] || arg2[i];
54 }
55
56 ~Or()
57 { }
58};
59
61{
62public:
63 Nor()
64 {
65 nArguments_ = 2;
66 name_ = "NOR";
67 }
68
69 void execute(void* result, Tree::Tree& tree)
70 {
71 std::vector<bool>& vOr = *(std::vector<bool>*)result;
72 uint size = (uint)vOr.size();
73
74 std::vector<bool> arg1(size), arg2(size);
75
76 getNextArgument(&arg1, tree);
77 getNextArgument(&arg2, tree);
78
79 for (uint i = 0; i < size; i++)
80 //vOr[i] = (!arg1[i]) || (!arg2[i]);
81 vOr[i] = (!arg1[i]) && (!arg2[i]);
82 }
83
84 ~Nor()
85 { }
86};
87
88
90{
91public:
92 Xor()
93 {
94 nArguments_ = 2;
95 name_ = "XOR";
96 }
97
98 void execute(void* result, Tree::Tree& tree)
99 {
100 std::vector<bool>& vXor = *(std::vector<bool>*)result;
101 uint size = (uint) vXor.size();
102
103 std::vector<bool> arg1(size), arg2(size);
104
105 getNextArgument(&arg1, tree);
106 getNextArgument(&arg2, tree);
107
108 for(uint i = 0; i < size; i++)
109 vXor[i] = (arg1[i] && !arg2[i]) || (!arg1[i] && arg2[i]);
110 }
111
112 ~Xor()
113 { }
114};
115
116
118{
119public:
120 XNor()
121 {
122 nArguments_ = 2;
123 name_ = "XNOR";
124 }
125
126 void execute(void* result, Tree::Tree& tree)
127 {
128 std::vector<bool>& vXNor = *(std::vector<bool>*)result;
129 uint size = (uint) vXNor.size();
130
131 std::vector<bool> arg1(size), arg2(size);
132
133 getNextArgument(&arg1, tree);
134 getNextArgument(&arg2, tree);
135
136 for(uint i = 0; i < size; i++)
137 vXNor[i] = (!(arg1[i] && !arg2[i]) || (!arg1[i] && arg2[i]));
138 }
139
140 ~XNor()
141 { }
142};
143
144
146{
147public:
148 And()
149 {
150 nArguments_ = 2;
151 name_ = "AND";
152 }
153
154 void execute(void* result, Tree::Tree& tree)
155 {
156 std::vector<bool>& vAnd = *(std::vector<bool>*)result;
157 uint size = (uint) vAnd.size();
158
159 std::vector<bool> arg1(size), arg2(size);
160
161 getNextArgument(&arg1, tree);
162 getNextArgument(&arg2, tree);
163
164 for(uint i = 0; i < size; i++)
165 vAnd[i] = arg1[i] && arg2[i];
166 }
167
168 ~And()
169 { }
170};
171
172
174{
175public:
176 Nand()
177 {
178 nArguments_ = 2;
179 name_ = "NAND";
180 }
181
182 void execute(void* result, Tree::Tree& tree)
183 {
184 std::vector<bool>& vAnd = *(std::vector<bool>*)result;
185 uint size = (uint)vAnd.size();
186
187 std::vector<bool> arg1(size), arg2(size);
188
189 getNextArgument(&arg1, tree);
190 getNextArgument(&arg2, tree);
191
192 for (uint i = 0; i < size; i++)
193 vAnd[i] = (!arg1[i]) || (!arg2[i]);
194 }
195
196 ~Nand()
197 { }
198};
199
201{
202public:
203 And2()
204 {
205 nArguments_ = 2;
206 name_ = "AND2";
207 }
208
209 void execute(void* result, Tree::Tree& tree)
210 {
211 std::vector<bool>& vAnd2 = *(std::vector<bool>*)result;
212 uint size = (uint) vAnd2.size();
213
214 std::vector<bool> arg1(size), arg2(size);
215
216 getNextArgument(&arg1, tree);
217 getNextArgument(&arg2, tree);
218
219 for(uint i = 0; i < size; i++)
220 vAnd2[i] = arg1[i] && (!arg2[i]);
221 }
222
223 ~And2()
224 { }
225};
226
227
229{
230public:
231 Not()
232 {
233 nArguments_ = 1;
234 name_ = "NOT";
235 }
236
237 void execute(void* result, Tree::Tree& tree)
238 {
239 std::vector<bool>& vNot = *(std::vector<bool>*)result;
240 uint size = (uint) vNot.size();
241
242 std::vector<bool> arg1(size);
243 getNextArgument(&arg1, tree);
244
245 for(uint i = 0; i < size; i++)
246 vNot[i] = !arg1[i];
247 }
248
249 ~Not()
250 { }
251};
252
253
255{
256public:
257 If()
258 {
259 nArguments_ = 3;
260 name_ = "IF";
261 }
262
263 void execute(void* result, Tree::Tree& tree)
264 {
265 std::vector<bool>& res = *(std::vector<bool>*)result;
266 uint size = (uint) res.size();
267
268 std::vector<bool> arg(size), res1(size), res2(size);
269 getNextArgument(&arg, tree);
270 getNextArgument(&res1, tree);
271 getNextArgument(&res2, tree);
272
273 for(uint i = 0; i < size; i++)
274 if(arg[i]) {
275 res[i] = res1[i];
276 } else {
277 res[i] = res2[i];
278 }
279 }
280
281 ~If()
282 { }
283};
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:209
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:154
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:15
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:263
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:182
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:69
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:237
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:42
Base primitive class (Tree genotype).
Definition: Primitive.h:37
void getNextArgument(void *result, Tree &tree)
Execute next child node's primitive (execute next subtree).
Definition: Primitive.cpp:71
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:126
void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: Primitives.cpp:98