Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/03/09 12:26:42 (15 years ago)
Author:
gkronber
Message:

Merged changes from GP-refactoring branch back into the trunk #713.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.Boolean/3.3/BooleanTreeInterpreter.cs

    r2174 r2222  
    2424using System.Linq;
    2525using System.Text;
    26 using HeuristicLab.DataAnalysis;
    2726using HeuristicLab.Core;
    2827using System.Xml;
    2928using System.Diagnostics;
    3029using HeuristicLab.Data;
     30using HeuristicLab.GP.Interfaces;
     31using HeuristicLab.DataAnalysis;
    3132
    3233namespace HeuristicLab.GP.Boolean {
     
    3435    private const double EPSILON = 0.00001;
    3536    private Dataset dataset;
    36     private List<LightWeightFunction> expression;
     37    private IFunctionTree tree;
    3738    private int targetVariable;
    3839    private int currentRow;
    39     private int pc;
    4040
    41     public void Reset(Dataset dataset, BakedFunctionTree tree, int targetVariable) {
     41    public void Reset(Dataset dataset, IFunctionTree tree, int targetVariable) {
    4242      this.dataset = dataset;
    43       this.expression = tree.LinearRepresentation;
     43      this.tree = tree;
    4444      this.targetVariable = targetVariable;
    4545    }
     
    4848      int errors = 0;
    4949      for (int i = start; i < end; i++) {
    50         pc = 0;
    5150        currentRow = i;
    52         int result = Step() ? 1 : 0;
     51        int result = Step(tree) ? 1 : 0;
    5352        if (Math.Abs(result - dataset.GetValue(i, targetVariable)) > EPSILON) errors++;
    5453      }
     
    5655    }
    5756
    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);
    6159      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          }
    7071        case SymbolTable.UNKNOWN:
    7172        default:
    72           throw new InvalidOperationException(curFun.functionType.ToString());
     73          throw new UnknownFunctionException(t.Function.Name);
     74      }
     75    }
    7376
    74       }
     77    private bool IsAlmost(double x, double y) {
     78      return Math.Abs(x - y) < EPSILON;
    7579    }
    7680  }
Note: See TracChangeset for help on using the changeset viewer.