-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem.h
177 lines (142 loc) · 4.86 KB
/
problem.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
#ifndef PROBLEM_H
#define PROBLEM_H
#include <vector>
#include "snode.h"
#include "sortedarray.h"
/*
* Sample GP test cases.
*/
class Problem
{
public:
Problem();
virtual ~Problem();
int getNumInputs() { return _numInputs; }
/*
* Get the number of test cases.
*/
int getNumFitnessCases() { return _testCases.size(); }
/*
* Get the inputs for the given test case
*/
int* getInputs(int fitnessCase);
std::vector<int>& getOutputs() { return _outputs; }
/*
* Get the expected output for the given test case
*/
int getOutput(int fitnessCase) { return _outputs[fitnessCase]; }
/*
* Get the current set of results that were evaluate()d.
*/
std::vector<int>& getTestCaseResults(int i) { return _testCaseResults[i]; }
/*
* Get the operators allowed for solving the
* problem.
*/
std::vector<SNode::Op>& getOps() { return _ops; }
/*
* Return true if an individual has hit the target
* fitness (this individual is a solution to the problem).
*/
virtual bool hitTargetFitness(
const std::vector<int> &values) = 0;
/*
* Optimized inner loop for evaluating all test cases.
* Don't let that virtual fool you! :)
*/
virtual void evaluate(const std::vector<SNode> &nodes,
std::vector<int> &outFitness) = 0;
virtual void evaluate(const std::vector<SNode> &nodes,
const SortedArray<int> &changedNodes,
std::vector<int> &outFitness) = 0;
void initTestCaseResults(int numNodes);
protected:
class TestCase {
public:
TestCase() : inputs(0) { }
int* inputs; // each test case has _numInputs
// number of inputs.
};
typedef int(*EvalNodeFunc)(SNode::Op, int, int, int, const std::vector<int> &);
typedef int(*CalcFitnessFunc)(int, int);
template<EvalNodeFunc evalNode, CalcFitnessFunc calcFitness>
void _evaluateAll(const std::vector<SNode> &nodes,
std::vector<int> &outFitness);
template<EvalNodeFunc evalNode, CalcFitnessFunc calcFitness>
void _evaluate(const std::vector<SNode>& nodes,
const SortedArray<int>& changedNodes,
std::vector<int>& outFitness);
int _numInputs;
std::vector<TestCase> _testCases;
std::vector<int> _outputs;
std::vector<std::vector<int> > _testCaseResults;
std::vector<SNode::Op> _ops;
};
/*
* The 6-mux problem.
* Two address inputs select one of four data inputs.
* The function set is {AND, OR, NOT, IF}.
* Fitness evaluation is exhaustive over all inputs, with an
* individuals fitness equal to the number of matches with
* the expected results.
*/
class ProblemMultiplexer : public Problem {
public:
ProblemMultiplexer();
virtual bool hitTargetFitness(
const std::vector<int>& values);
virtual void evaluate(const std::vector<SNode> &nodes,
const SortedArray<int> &changedNodes,
std::vector<int> &outFitness);
virtual void evaluate(const std::vector<SNode> &nodes,
std::vector<int> &outFitness);
protected:
void init();
};
/*
* Even parity problem.
* For the given number of input select 1 if the number of
* inputs with values set to 1 is even else select 0.
* The function set is {AND, OR, NAND, NOR}.
* Fitness evaluation is exhaustive over all inputs, with an
* individuals fitness equal to the number of matches with
* the expected results.
*/
class ProblemEvenParity : public Problem {
public:
ProblemEvenParity(int inputs);
virtual bool hitTargetFitness(
const std::vector<int>& values);
virtual void evaluate(const std::vector<SNode> &nodes,
const SortedArray<int> &changedNodes,
std::vector<int> &outFitness);
virtual void evaluate(const std::vector<SNode> &nodes,
std::vector<int> &outFitness);
protected:
void init();
};
/*
* Sample symbolic regression problem:
* 4x^4 – 3x^3 + 2x^2 -x
*
* The fitness evaluation is done on integers ranging from
* -16 to 16, with an individuals fitness equal to
* difference between it's value and the expected value.
* Differences are always expressed as negative values.
*
* The function set is {ADD, SUB, MULT, DIV},
*/
class ProblemSymbolicRegression : public Problem {
public:
ProblemSymbolicRegression();
virtual bool hitTargetFitness(
const std::vector<int>& values);
virtual void evaluate(const std::vector<SNode> &nodes,
const SortedArray<int> &changedNodes,
std::vector<int> &outFitness);
virtual void evaluate(const std::vector<SNode> &nodes,
std::vector<int> &outFitness);
protected:
void init();
};
#endif // PROBLEM_H