-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFunctionEvaluator.java
More file actions
294 lines (250 loc) · 7.27 KB
/
FunctionEvaluator.java
File metadata and controls
294 lines (250 loc) · 7.27 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
package gp_project;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
public class FunctionEvaluator {
private static double overallBestFitness = 10000;
private static double generationbestFitness = 10000;
public static double getGenerationbestFitness() {
return generationbestFitness;
}
/**
* Parses out the operator type from a string and returns an enumerated type
*
* @param str - String that contains an operator
* @return -type of operator found
*/
public static OperatorType getOperator(String str) {
if (str.equals("+")) {
return OperatorType.ADD;
} else if (str.equals("-")) {
return OperatorType.SUBTRACT;
} else if (str.equals("/")) {
return OperatorType.DIVIDE;
} else if (str.equals("*")) {
return OperatorType.MULTIPLY;
} else {
String msg = "Invalid Operator: ";
msg += str;
throw new IllegalArgumentException(msg);
}
}
/**
* Parse a string and determine if it is an integer
*
* @param str
* @return true if the string is an integer
*/
public static boolean isInteger(String str) {
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
i = 1;
}
for (; i < length; i++) {
char c = str.charAt(i);
if (c <= '/' || c >= ':') {
return false;
}
}
return true;
}
/**
* Parse a string and determine if it is a variable
*
* @param str
* @return
*/
public static boolean isVariable(String str) {
return str.equals("x");
}
private Tree m_bestFit;
/**
* Evaluate a function at a particular x value and return the y value (EvaluationResult class stores y value)
*
* @param postOrderList - List of operators and operands already parse in post order method
* @param d - value at which to evaluate the tree
* @return -result of evaluation. If evaluation involved division by zero, the result is invalid (EvaluationResult member variable isValid set to false)
*/
public EvaluationResult evaluate(List<String> postOrderList, double d) {
EvaluationResult result = new EvaluationResult();
Stack<Double> st = new Stack<Double>();
for (String nodeValue : postOrderList) {
if (isInteger(nodeValue)) {
st.push((double) Integer.parseInt(nodeValue));
} else if (isVariable(nodeValue)) {
st.push((double) d);
} else {
if (st.size() < 2) {
throw new IllegalArgumentException(
"Invalid Tree encountered");
}
double num1 = st.pop();
double num2 = st.pop();
switch (getOperator(nodeValue)) {
case ADD:
st.push(num1 + num2);
break;
case MULTIPLY:
st.push(num1 * num2);
break;
case DIVIDE:
if (num1 == 0) {
result.isValid = false;
}
st.push(num2 / num1);
break;
case SUBTRACT:
st.push(num2 - num1);
break;
default:
throw new IllegalArgumentException("Invalid Operator");
}
}
}
if (st.size() > 1) {
throw new IllegalArgumentException("Invalid Tree encountered");
}
result.yValue = st.pop();
return result;
}
/**
* Evaluate a function at a particular x value and return the y value (EvaluationResult class stores y value)
*
* @param tree - Tree to evaluate
* @param d - value at which to evaluate the tree
* @return -result of evaluation. If evaluation involved division by zero, the result is invalid (EvaluationResult member variable isValid set to false)
*/
public EvaluationResult evaluateFunction(Tree tree, double d) {
List<String> postOrderList = new ArrayList<String>();
getPostOrderList(tree.getRootNode(), postOrderList);
// System.out.println("evaluateFunction");
// System.out.println(Arrays.toString(postOrderList.toArray()));
return evaluate(postOrderList, d);
}
/**
*
* Evaluate all trees in the population to find their fitness values.
* If we find the tree we are looking for, store it in m_bestFit
*
* @param pop
* @return return true to continue, false if tree is found
*/
public boolean evaluatePop(Population pop) {
generationbestFitness = 100000;
List<Tree> list = pop.getTrees();
for (Iterator<Tree> iter = list.iterator(); iter.hasNext();) {
Tree func = iter.next();
FitnessResult fitResult = getFitnessValue(func);
if (fitResult.isValid) {
if (fitResult.fitnessValue < generationbestFitness) {
generationbestFitness = fitResult.fitnessValue;
}
if (fitResult.fitnessValue < overallBestFitness) {
overallBestFitness = fitResult.fitnessValue;
m_bestFit = func;
}
if (fitResult.fitnessValue == 0) //If the function we need to find is a more complex polynomial, consider changing this to something greater than zero
{
System.out.println("----Found best fit tree--------");
return false;
}
} else {
iter.remove();
}
}
return true;
}
public double getBestFitness() {
return overallBestFitness;
}
public Tree getBestTree() {
return m_bestFit;
}
/**
* Find the fitness value for a given tree
*
* @param tree
* @return
*/
FitnessResult getFitnessValue(Tree tree) {
FitnessResult result = new FitnessResult();
GPConfig config = GPConfig.getInstance();
List<TrainingDataPair> tdList = config.getTrainingData();
/*
* for each point in training data, evaluate function at that point and
* find the difference. Sum the differences to get the overall fitness
*/
double fitness = 0;
/*
* Todo possibly reconsider this behavior. May not want to throw out the
* whole function just because one evaluation involved division by zero.
* Will NEED to reconsider if the prof asks us to find y = 1/x
*/
for (TrainingDataPair pair : tdList) {
EvaluationResult evalResult = evaluateFunction(tree,
pair.getxValue());
if (!evalResult.isValid) {
result.isValid = false;
}
double diff = Math.abs(evalResult.yValue - pair.getyValue());
fitness += diff;
}
tree.setFitnessValue(fitness);
result.fitnessValue = fitness;
return result;
}
/**
* Parse the tree if post order traversal and store the results in list of strings
*
* @param node
* @param list
*/
public void getPostOrderList(Node node, List<String> list) {
if (node == null)
return;
getPostOrderList(node.getLeftNode(), list);
getPostOrderList(node.getRightNode(), list);
list.add(node.getData());
}
/**
* Parse the tree if post order traversal and store the results in list of nodes
*
* Similar to the function above, but returns a list of nodes
* Need to use this function for crossover since we need more context than just the value at that node
*
* @param node
* @param list
*/
public void getPostOrderNodeList(Node node, List<Node> list) {
if (node == null)
return;
getPostOrderNodeList(node.getLeftNode(), list);
getPostOrderNodeList(node.getRightNode(), list);
list.add(node);
}
public void getInOrderNodeList(Node node, List<String> list) {
if (node == null)
return;
boolean useParen = false;
if ((node.getLeftNode() != null) && (node.getRightNode() != null))
useParen = true;
if(useParen)
list.add("(");
getInOrderNodeList(node.getLeftNode(), list);
list.add(node.getData());
getInOrderNodeList(node.getRightNode(), list);
if(useParen)
list.add(")");
}
}