Free cookie consent management tool by TermsFeed Policy Generator

Changeset 730


Ignore:
Timestamp:
11/10/08 18:42:44 (16 years ago)
Author:
gkronber
Message:

fixed bugs in plugin HL.GP.Boolean #340 (Plugin for genetic programming for boolean logic)

Location:
trunk/sources/HeuristicLab.GP.Boolean
Files:
2 edited

Legend:

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

    r722 r730  
    3434    private const double EPSILON = 0.00001;
    3535    private Dataset dataset;
    36     private IFunctionTree tree;
     36    private List<LightWeightFunction> expression;
    3737    private int targetVariable;
    3838    private int currentRow;
    39     private Dictionary<IFunctionTree, int> cachedIndex = new Dictionary<IFunctionTree, int>();
     39    private int pc;
    4040
    41     public void Reset(Dataset dataset, IFunctionTree tree, int targetVariable) {
     41    public void Reset(Dataset dataset, BakedFunctionTree tree, int targetVariable) {
    4242      this.dataset = dataset;
    43       this.tree = tree;
     43      this.expression = tree.LinearRepresentation;
    4444      this.targetVariable = targetVariable;
    45       cachedIndex.Clear();
    4645    }
    4746
    48     internal int GetNumberMatchingInstances(int start, int end) {
    49       int matchingInstances = 0;
     47    internal int GetNumberOfErrors(int start, int end) {
     48      int errors = 0;
    5049      for (int i = start; i < end; i++) {
     50        pc = 0;
    5151        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++;
    5454      }
    55       return matchingInstances;
     55      return errors;
    5656    }
    5757
    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);
    6061      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());
    6768        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;
    7570        case SymbolTable.UNKNOWN:
    7671        default:
    77           throw new InvalidOperationException(tree.Function.ToString());
     72          throw new InvalidOperationException(curFun.functionType.ToString());
    7873
    7974      }
  • trunk/sources/HeuristicLab.GP.Boolean/Evaluator.cs

    r720 r730  
    3232    public Evaluator()
    3333      : 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));
    3535      AddVariableInfo(new VariableInfo("Dataset", "The boolean dataset (values 0.0 = false, 1.0=true)", typeof(Dataset), VariableKind.In));
    3636      AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In));
    3737      AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
    3838      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));
    4040    }
    4141
    4242    public override IOperation Apply(IScope scope) {
    43       IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
     43      BakedFunctionTree tree = GetVariableValue<BakedFunctionTree>("FunctionTree", scope, true);
    4444      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    4545      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
     
    4949      BooleanTreeInterpreter interpreter = new BooleanTreeInterpreter();
    5050      interpreter.Reset(dataset, tree, targetVariable);
    51       int matchingCases = interpreter.GetNumberMatchingInstances(start, end);
     51      int errors = interpreter.GetNumberOfErrors(start, end);
    5252
    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)));
    5454      return null;
    5555    }
Note: See TracChangeset for help on using the changeset viewer.