Changeset 722 for trunk/sources/HeuristicLab.GP.Boolean
- Timestamp:
- 11/08/08 13:55:59 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.GP.Boolean
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.Boolean/BooleanTreeInterpreter.cs
r720 r722 37 37 private int targetVariable; 38 38 private int currentRow; 39 private Dictionary<IFunctionTree, int> cachedIndex = new Dictionary<IFunctionTree, int>(); 39 40 40 41 public void Reset(Dataset dataset, IFunctionTree tree, int targetVariable) { … … 42 43 this.tree = tree; 43 44 this.targetVariable = targetVariable; 45 cachedIndex.Clear(); 44 46 } 45 47 46 48 internal int GetNumberMatchingInstances(int start, int end) { 47 49 int matchingInstances = 0; 48 for (int i = start; i < end; i++) {50 for (int i = start; i < end; i++) { 49 51 currentRow = i; 50 52 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++; 52 54 } 53 55 return matchingInstances; … … 56 58 internal bool Step(IFunctionTree tree) { 57 59 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]); 61 63 case SymbolTable.NOT: return !Step(tree.SubTrees[0]); 62 64 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])); 65 67 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; 69 75 case SymbolTable.UNKNOWN: 70 76 default: -
trunk/sources/HeuristicLab.GP.Boolean/FunctionLibraryInjector.cs
r720 r722 31 31 namespace HeuristicLab.GP.Boolean { 32 32 public class FunctionLibraryInjector : OperatorBase { 33 private const string TARGETVARIABLE = "TargetVariable"; 34 private const string ALLOWEDFEATURES = "AllowedFeatures"; 33 35 private const string OPERATORLIBRARY = "FunctionLibrary"; 34 36 35 37 private GPOperatorLibrary operatorLibrary; 38 private Variable variable; 36 39 37 40 public override string Description { … … 41 44 public FunctionLibraryInjector() 42 45 : 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)); 43 48 AddVariableInfo(new VariableInfo(OPERATORLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New)); 44 49 } 45 50 46 51 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 47 59 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 48 68 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(OPERATORLIBRARY), operatorLibrary)); 49 69 return null; … … 57 77 Nor nor = new Nor(); 58 78 Xor xor = new Xor(); 59 HeuristicLab.GP.Boolean.Variablevariable = new HeuristicLab.GP.Boolean.Variable();79 variable = new HeuristicLab.GP.Boolean.Variable(); 60 80 61 81 IFunction[] allFunctions = new IFunction[] { … … 74 94 SetAllowedSubOperators(nand, allFunctions); 75 95 SetAllowedSubOperators(nor, allFunctions); 76 SetAllowedSubOperators( not, allFunctions);96 SetAllowedSubOperators(xor, allFunctions); 77 97 78 98 operatorLibrary = new GPOperatorLibrary(); … … 87 107 88 108 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) { 91 111 SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint; 92 112 typeConstraint.Clear(); 93 foreach (IFunction g in gs) {113 foreach (IFunction g in gs) { 94 114 typeConstraint.AddOperator(g); 95 115 } 96 } else if (c is AllSubOperatorsTypeConstraint) {116 } else if (c is AllSubOperatorsTypeConstraint) { 97 117 AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint; 98 118 typeConstraint.Clear(); 99 foreach (IFunction g in gs) {119 foreach (IFunction g in gs) { 100 120 typeConstraint.AddOperator(g); 101 121 }
Note: See TracChangeset
for help on using the changeset viewer.