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)

Location:
trunk/sources/HeuristicLab.GP.Boolean
Files:
2 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:
  • trunk/sources/HeuristicLab.GP.Boolean/FunctionLibraryInjector.cs

    r720 r722  
    3131namespace HeuristicLab.GP.Boolean {
    3232  public class FunctionLibraryInjector : OperatorBase {
     33    private const string TARGETVARIABLE = "TargetVariable";
     34    private const string ALLOWEDFEATURES = "AllowedFeatures";
    3335    private const string OPERATORLIBRARY = "FunctionLibrary";
    3436
    3537    private GPOperatorLibrary operatorLibrary;
     38    private Variable variable;
    3639
    3740    public override string Description {
     
    4144    public FunctionLibraryInjector()
    4245      : base() {
     46      AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In));
     47      AddVariableInfo(new VariableInfo(ALLOWEDFEATURES, "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In));
    4348      AddVariableInfo(new VariableInfo(OPERATORLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New));
    4449    }
    4550
    4651    public override IOperation Apply(IScope scope) {
     52      ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>(ALLOWEDFEATURES, scope, true);
     53      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
     54
     55      // remove the target-variable in case it occures in allowed features
     56      List<IntData> ts = allowedFeatures.FindAll(d => d.Data == targetVariable);
     57      foreach (IntData t in ts) allowedFeatures.Remove(t);
     58
    4759      InitDefaultOperatorLibrary();
     60
     61      int[] allowedIndexes = new int[allowedFeatures.Count];
     62      for (int i = 0; i < allowedIndexes.Length; i++) {
     63        allowedIndexes[i] = allowedFeatures[i].Data;
     64      }
     65
     66      variable.SetConstraints(allowedIndexes);
     67
    4868      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(OPERATORLIBRARY), operatorLibrary));
    4969      return null;
     
    5777      Nor nor = new Nor();
    5878      Xor xor = new Xor();
    59       HeuristicLab.GP.Boolean.Variable variable = new HeuristicLab.GP.Boolean.Variable();
     79      variable = new HeuristicLab.GP.Boolean.Variable();
    6080
    6181      IFunction[] allFunctions = new IFunction[] {
     
    7494      SetAllowedSubOperators(nand, allFunctions);
    7595      SetAllowedSubOperators(nor, allFunctions);
    76       SetAllowedSubOperators(not, allFunctions);
     96      SetAllowedSubOperators(xor, allFunctions);
    7797
    7898      operatorLibrary = new GPOperatorLibrary();
     
    87107
    88108    private void SetAllowedSubOperators(IFunction f, IFunction[] gs) {
    89       foreach(IConstraint c in f.Constraints) {
    90         if(c is SubOperatorTypeConstraint) {
     109      foreach (IConstraint c in f.Constraints) {
     110        if (c is SubOperatorTypeConstraint) {
    91111          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
    92112          typeConstraint.Clear();
    93           foreach(IFunction g in gs) {
     113          foreach (IFunction g in gs) {
    94114            typeConstraint.AddOperator(g);
    95115          }
    96         } else if(c is AllSubOperatorsTypeConstraint) {
     116        } else if (c is AllSubOperatorsTypeConstraint) {
    97117          AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
    98118          typeConstraint.Clear();
    99           foreach(IFunction g in gs) {
     119          foreach (IFunction g in gs) {
    100120            typeConstraint.AddOperator(g);
    101121          }
Note: See TracChangeset for help on using the changeset viewer.