Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/08/08 13:55:59 (16 years ago)
Author:
gkronber
Message:

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

File:
1 edited

Legend:

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

    r720 r722  
    3737    private int targetVariable;
    3838    private int currentRow;
     39    private Dictionary<IFunctionTree, int> cachedIndex = new Dictionary<IFunctionTree, int>();
    3940
    4041    public void Reset(Dataset dataset, IFunctionTree tree, int targetVariable) {
     
    4243      this.tree = tree;
    4344      this.targetVariable = targetVariable;
     45      cachedIndex.Clear();
    4446    }
    4547
    4648    internal int GetNumberMatchingInstances(int start, int end) {
    4749      int matchingInstances = 0;
    48       for(int i = start; i < end; i++) {
     50      for (int i = start; i < end; i++) {
    4951        currentRow = i;
    5052        int result = Step(tree) ? 1 : 0;
    51         if(result - dataset.GetValue(i, targetVariable) < EPSILON) matchingInstances++;
     53        if (Math.Abs(result - dataset.GetValue(i, targetVariable)) < EPSILON) matchingInstances++;
    5254      }
    5355      return matchingInstances;
     
    5658    internal bool Step(IFunctionTree tree) {
    5759      int symbol = SymbolTable.MapFunction(tree.Function);
    58       switch(symbol) {
    59         case SymbolTable.AND: return Step(tree.SubTrees[0]) & Step(tree.SubTrees[0]);
    60         case SymbolTable.OR: return Step(tree.SubTrees[0]) | Step(tree.SubTrees[0]);
     60      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]);
    6163        case SymbolTable.NOT: return !Step(tree.SubTrees[0]);
    6264        case SymbolTable.XOR: return Step(tree.SubTrees[0]) ^ Step(tree.SubTrees[1]);
    63         case SymbolTable.NAND: return !(Step(tree.SubTrees[0]) & Step(tree.SubTrees[0]));
    64         case SymbolTable.NOR: return !(Step(tree.SubTrees[0]) | Step(tree.SubTrees[0]));
     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]));
    6567        case SymbolTable.VARIABLE:
    66           int index = ((ConstrainedIntData)tree.LocalVariables.ToArray()[0].Value).Data;
    67           if(dataset.GetValue(currentRow, targetVariable) == 0.0) return false;
    68           else return true;
     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;
    6975        case SymbolTable.UNKNOWN:
    7076        default:
Note: See TracChangeset for help on using the changeset viewer.