Changeset 730
- Timestamp:
- 11/10/08 18:42:44 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.GP.Boolean
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.Boolean/BooleanTreeInterpreter.cs
r722 r730 34 34 private const double EPSILON = 0.00001; 35 35 private Dataset dataset; 36 private IFunctionTree tree;36 private List<LightWeightFunction> expression; 37 37 private int targetVariable; 38 38 private int currentRow; 39 private Dictionary<IFunctionTree, int> cachedIndex = new Dictionary<IFunctionTree, int>();39 private int pc; 40 40 41 public void Reset(Dataset dataset, IFunctionTree tree, int targetVariable) {41 public void Reset(Dataset dataset, BakedFunctionTree tree, int targetVariable) { 42 42 this.dataset = dataset; 43 this. tree = tree;43 this.expression = tree.LinearRepresentation; 44 44 this.targetVariable = targetVariable; 45 cachedIndex.Clear();46 45 } 47 46 48 internal int GetNumber MatchingInstances(int start, int end) {49 int matchingInstances = 0;47 internal int GetNumberOfErrors(int start, int end) { 48 int errors = 0; 50 49 for (int i = start; i < end; i++) { 50 pc = 0; 51 51 currentRow = i; 52 int result = Step( tree) ? 1 : 0;53 if (Math.Abs(result - dataset.GetValue(i, targetVariable)) < EPSILON) matchingInstances++;52 int result = Step() ? 1 : 0; 53 if (Math.Abs(result - dataset.GetValue(i, targetVariable)) > EPSILON) errors++; 54 54 } 55 return matchingInstances;55 return errors; 56 56 } 57 57 58 internal bool Step(IFunctionTree tree) { 59 int symbol = SymbolTable.MapFunction(tree.Function); 58 internal bool Step() { 59 LightWeightFunction curFun = expression[pc++]; 60 int symbol = SymbolTable.MapFunction(curFun.functionType); 60 61 switch (symbol) { 61 case SymbolTable.AND: return Step( tree.SubTrees[0]) && Step(tree.SubTrees[1]);62 case SymbolTable.OR: return Step( tree.SubTrees[0]) || Step(tree.SubTrees[1]);63 case SymbolTable.NOT: return !Step( tree.SubTrees[0]);64 case SymbolTable.XOR: return Step( tree.SubTrees[0]) ^ Step(tree.SubTrees[1]);65 case SymbolTable.NAND: return !(Step( tree.SubTrees[0]) && Step(tree.SubTrees[1]));66 case SymbolTable.NOR: return !(Step( tree.SubTrees[0]) || Step(tree.SubTrees[1]));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()); 67 68 case SymbolTable.VARIABLE: 68 int index; 69 if (cachedIndex.TryGetValue(tree, out index)==false) { 70 } else { 71 index = ((ConstrainedIntData)tree.LocalVariables.ToArray()[0].Value).Data; 72 cachedIndex[tree] = index; 73 } 74 return dataset.GetValue(currentRow, index) != 0.0; 69 return dataset.GetValue(currentRow, (int)curFun.data[0]) != 0.0; 75 70 case SymbolTable.UNKNOWN: 76 71 default: 77 throw new InvalidOperationException( tree.Function.ToString());72 throw new InvalidOperationException(curFun.functionType.ToString()); 78 73 79 74 } -
trunk/sources/HeuristicLab.GP.Boolean/Evaluator.cs
r720 r730 32 32 public Evaluator() 33 33 : base() { 34 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree representing the ant", typeof( IFunctionTree), VariableKind.In));34 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree representing the ant", typeof(BakedFunctionTree), VariableKind.In)); 35 35 AddVariableInfo(new VariableInfo("Dataset", "The boolean dataset (values 0.0 = false, 1.0=true)", typeof(Dataset), VariableKind.In)); 36 36 AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In)); 37 37 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 38 38 AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 39 AddVariableInfo(new VariableInfo(" MatchingCases", "", typeof(DoubleData), VariableKind.New | VariableKind.Out));39 AddVariableInfo(new VariableInfo("Errors", "", typeof(DoubleData), VariableKind.New | VariableKind.Out)); 40 40 } 41 41 42 42 public override IOperation Apply(IScope scope) { 43 IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);43 BakedFunctionTree tree = GetVariableValue<BakedFunctionTree>("FunctionTree", scope, true); 44 44 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 45 45 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; … … 49 49 BooleanTreeInterpreter interpreter = new BooleanTreeInterpreter(); 50 50 interpreter.Reset(dataset, tree, targetVariable); 51 int matchingCases = interpreter.GetNumberMatchingInstances(start, end);51 int errors = interpreter.GetNumberOfErrors(start, end); 52 52 53 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(" MatchingCases"), new DoubleData(matchingCases)));53 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Errors"), new DoubleData(errors))); 54 54 return null; 55 55 }
Note: See TracChangeset
for help on using the changeset viewer.