- Timestamp:
- 08/03/09 12:26:42 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.Boolean/3.3/BooleanTreeInterpreter.cs
r2174 r2222 24 24 using System.Linq; 25 25 using System.Text; 26 using HeuristicLab.DataAnalysis;27 26 using HeuristicLab.Core; 28 27 using System.Xml; 29 28 using System.Diagnostics; 30 29 using HeuristicLab.Data; 30 using HeuristicLab.GP.Interfaces; 31 using HeuristicLab.DataAnalysis; 31 32 32 33 namespace HeuristicLab.GP.Boolean { … … 34 35 private const double EPSILON = 0.00001; 35 36 private Dataset dataset; 36 private List<LightWeightFunction> expression;37 private IFunctionTree tree; 37 38 private int targetVariable; 38 39 private int currentRow; 39 private int pc;40 40 41 public void Reset(Dataset dataset, BakedFunctionTree tree, int targetVariable) {41 public void Reset(Dataset dataset, IFunctionTree tree, int targetVariable) { 42 42 this.dataset = dataset; 43 this. expression = tree.LinearRepresentation;43 this.tree = tree; 44 44 this.targetVariable = targetVariable; 45 45 } … … 48 48 int errors = 0; 49 49 for (int i = start; i < end; i++) { 50 pc = 0;51 50 currentRow = i; 52 int result = Step( ) ? 1 : 0;51 int result = Step(tree) ? 1 : 0; 53 52 if (Math.Abs(result - dataset.GetValue(i, targetVariable)) > EPSILON) errors++; 54 53 } … … 56 55 } 57 56 58 internal bool Step() { 59 LightWeightFunction curFun = expression[pc++]; 60 int symbol = SymbolTable.MapFunction(curFun.functionType); 57 internal bool Step(IFunctionTree t) { 58 int symbol = SymbolTable.MapFunction(t.Function); 61 59 switch (symbol) { 62 case SymbolTable.AND: return Step() & Step(); 63 case SymbolTable.OR: return Step() | Step(); 64 case SymbolTable.NOT: return !Step(); 65 case SymbolTable.XOR: return Step() ^ Step(); 66 case SymbolTable.NAND: return !(Step() & Step()); 67 case SymbolTable.NOR: return !(Step() | Step()); 68 case SymbolTable.VARIABLE: 69 return dataset.GetValue(currentRow, (int)curFun.localData[0]) != 0.0; 60 case SymbolTable.AND: return Step(t.SubTrees[0]) && Step(t.SubTrees[1]); 61 case SymbolTable.OR: return Step(t.SubTrees[0]) || Step(t.SubTrees[1]); 62 case SymbolTable.NOT: return !Step(t.SubTrees[0]); 63 case SymbolTable.XOR: return Step(t.SubTrees[0]) ^ Step(t.SubTrees[1]); 64 case SymbolTable.NAND: return !(Step(t.SubTrees[0]) && Step(t.SubTrees[1])); 65 case SymbolTable.NOR: return !(Step(t.SubTrees[0]) || Step(t.SubTrees[1])); 66 case SymbolTable.VARIABLE: { 67 var varNode = (VariableFunctionTree)t; 68 int index = dataset.GetVariableIndex(varNode.VariableName); 69 return !IsAlmost(dataset.GetValue(currentRow, index), 0.0); 70 } 70 71 case SymbolTable.UNKNOWN: 71 72 default: 72 throw new InvalidOperationException(curFun.functionType.ToString()); 73 throw new UnknownFunctionException(t.Function.Name); 74 } 75 } 73 76 74 } 77 private bool IsAlmost(double x, double y) { 78 return Math.Abs(x - y) < EPSILON; 75 79 } 76 80 }
Note: See TracChangeset
for help on using the changeset viewer.