Changeset 424 for trunk/sources/HeuristicLab.Functions
- Timestamp:
- 08/02/08 22:45:52 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs
r396 r424 27 27 using HeuristicLab.Core; 28 28 using System.Xml; 29 using System.Diagnostics; 29 30 30 31 namespace HeuristicLab.Functions { 31 32 internal class BakedTreeEvaluator : IEvaluator { 32 33 private const int MAX_TREE_SIZE = 4096; 34 private const double EPSILON = 1.0e-7; 33 35 34 36 private class Instr { … … 175 177 return Math.Tan(EvaluateBakedCode()); 176 178 } 177 case EvaluatorSymbolTable.AND: { 179 case EvaluatorSymbolTable.AND: { // only defined for inputs 1 and 0 178 180 double result = 1.0; 179 181 // have to evaluate all sub-trees, skipping would probably not lead to a big gain because 180 182 // we have to iterate over the linear structure anyway 181 183 for(int i = 0; i < currInstr.arity; i++) { 182 double x = Math.Round(EvaluateBakedCode());183 if(x == 0 || x == 1.0) result *= x;184 else result = double.NaN;184 double x = EvaluateBakedCode(); 185 Debug.Assert(x == 0.0 || x == 1.0); 186 result *= x; 185 187 } 186 188 return result; … … 189 191 double x = EvaluateBakedCode(); 190 192 double y = EvaluateBakedCode(); 191 if( x == y) return 1.0; else return 0.0;193 if(Math.Abs(x - y) < EPSILON) return 1.0; else return 0.0; 192 194 } 193 195 case EvaluatorSymbolTable.GT: { … … 197 199 else return 0.0; 198 200 } 199 case EvaluatorSymbolTable.IFTE: { 200 double condition = Math.Round(EvaluateBakedCode());201 double x = EvaluateBakedCode();202 double y= EvaluateBakedCode();203 if(condition < .5) return x;204 else if(condition >= .5) return y;205 else return double.NaN;201 case EvaluatorSymbolTable.IFTE: { // only defined for condition 0 or 1 202 double condition = EvaluateBakedCode(); 203 Debug.Assert(condition == 0.0 || condition == 1.0); 204 double x = EvaluateBakedCode(); 205 double y = EvaluateBakedCode(); 206 if(condition == 0.0) return x; 207 else return y; 206 208 } 207 209 case EvaluatorSymbolTable.LT: { … … 211 213 else return 0.0; 212 214 } 213 case EvaluatorSymbolTable.NOT: { 214 double result = Math.Round(EvaluateBakedCode()); 215 case EvaluatorSymbolTable.NOT: { // only defined for inputs 0 or 1 216 double result = EvaluateBakedCode(); 217 Debug.Assert(result == 0.0 || result == 1.0); 215 218 if(result == 0.0) return 1.0; 216 else if(result == 1.0) return 0.0; 217 else return double.NaN; 218 } 219 case EvaluatorSymbolTable.OR: { 219 else return 0.0; 220 } 221 case EvaluatorSymbolTable.OR: { // only defined for inputs 0 or 1 220 222 double result = 0.0; // default is false 221 223 for(int i = 0; i < currInstr.arity; i++) { 222 double x = Math.Round(EvaluateBakedCode());223 if(x == 1.0 && result == 0.0) result = 1.0; // found first true (1.0) => set to true224 else if(x != 0.0) result = double.NaN; // if it was not true it can only be false (0.0) all other cases are undefined => (NaN)224 double x = EvaluateBakedCode(); 225 Debug.Assert(x == 0.0 || x == 1.0); 226 if(x == 1.0) result = 1.0; 225 227 } 226 228 return result; 227 229 } 228 case EvaluatorSymbolTable.XOR: { 229 double x = Math.Round(EvaluateBakedCode()); 230 double y = Math.Round(EvaluateBakedCode()); 231 if(x == 0.0 && y == 0.0) return 0.0; 232 if(x == 1.0 && y == 0.0) return 1.0; 233 if(x == 0.0 && y == 1.0) return 1.0; 234 if(x == 1.0 && y == 1.0) return 0.0; 235 return double.NaN; 230 case EvaluatorSymbolTable.XOR: { // only defined for inputs 0 or 1 231 double x = EvaluateBakedCode(); 232 double y = EvaluateBakedCode(); 233 return Math.Abs(x - y); 236 234 } 237 235 default: {
Note: See TracChangeset
for help on using the changeset viewer.