Changeset 2222 for trunk/sources/HeuristicLab.GP.Boolean
- Timestamp:
- 08/03/09 12:26:42 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.GP.Boolean/3.3
- Files:
-
- 8 deleted
- 5 edited
- 10 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.Boolean/3.3/BooleanTreeInterpreter.cs
r2174 r2222 24 24 using System.Linq; 25 25 using System.Text; 26 using HeuristicLab.DataAnalysis;27 26 using HeuristicLab.Core; 28 27 using System.Xml; 29 28 using System.Diagnostics; 30 29 using HeuristicLab.Data; 30 using HeuristicLab.GP.Interfaces; 31 using HeuristicLab.DataAnalysis; 31 32 32 33 namespace HeuristicLab.GP.Boolean { … … 34 35 private const double EPSILON = 0.00001; 35 36 private Dataset dataset; 36 private List<LightWeightFunction> expression;37 private IFunctionTree tree; 37 38 private int targetVariable; 38 39 private int currentRow; 39 private int pc;40 40 41 public void Reset(Dataset dataset, BakedFunctionTree tree, int targetVariable) {41 public void Reset(Dataset dataset, IFunctionTree tree, int targetVariable) { 42 42 this.dataset = dataset; 43 this. expression = tree.LinearRepresentation;43 this.tree = tree; 44 44 this.targetVariable = targetVariable; 45 45 } … … 48 48 int errors = 0; 49 49 for (int i = start; i < end; i++) { 50 pc = 0;51 50 currentRow = i; 52 int result = Step( ) ? 1 : 0;51 int result = Step(tree) ? 1 : 0; 53 52 if (Math.Abs(result - dataset.GetValue(i, targetVariable)) > EPSILON) errors++; 54 53 } … … 56 55 } 57 56 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); 61 59 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 } 70 71 case SymbolTable.UNKNOWN: 71 72 default: 72 throw new InvalidOperationException(curFun.functionType.ToString()); 73 throw new UnknownFunctionException(t.Function.Name); 74 } 75 } 73 76 74 } 77 private bool IsAlmost(double x, double y) { 78 return Math.Abs(x - y) < EPSILON; 75 79 } 76 80 } -
trunk/sources/HeuristicLab.GP.Boolean/3.3/Evaluator.cs
r1529 r2222 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.GP.Interfaces; 28 29 using HeuristicLab.DataAnalysis; 29 30 … … 32 33 public Evaluator() 33 34 : base() { 34 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree representing the ant", typeof(BakedFunctionTree), VariableKind.In));35 AddVariableInfo(new VariableInfo("FunctionTree", "The function tree representing the boolean expression to evaluate", typeof(IGeneticProgrammingModel), VariableKind.In)); 35 36 AddVariableInfo(new VariableInfo("Dataset", "The boolean dataset (values 0.0 = false, 1.0=true)", typeof(Dataset), VariableKind.In)); 36 37 AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In)); … … 41 42 42 43 public override IOperation Apply(IScope scope) { 43 BakedFunctionTree tree = GetVariableValue<BakedFunctionTree>("FunctionTree", scope, true);44 IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true); 44 45 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 45 46 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; … … 48 49 49 50 BooleanTreeInterpreter interpreter = new BooleanTreeInterpreter(); 50 interpreter.Reset(dataset, tree, targetVariable);51 interpreter.Reset(dataset, gpModel.FunctionTree, targetVariable); 51 52 int errors = interpreter.GetNumberOfErrors(start, end); 52 53 -
trunk/sources/HeuristicLab.GP.Boolean/3.3/FunctionLibraryInjector.cs
r2165 r2222 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.DataAnalysis; 29 using HeuristicLab.Constraints; 29 using HeuristicLab.GP.Interfaces; 30 using HeuristicLab.GP.SantaFe; 30 31 31 32 namespace HeuristicLab.GP.Boolean { 32 public class FunctionLibraryInjector : OperatorBase { 33 private const string TARGETVARIABLE = "TargetVariable"; 34 private const string OPERATORLIBRARY = "FunctionLibrary"; 35 36 private GPOperatorLibrary operatorLibrary; 37 private Variable variable; 38 33 public class FunctionLibraryInjector : FunctionLibraryInjectorBase { 39 34 public override string Description { 40 35 get { return @"Injects a function library for boolean logic."; } … … 43 38 public FunctionLibraryInjector() 44 39 : base() { 45 AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In));46 AddVariableInfo(new VariableInfo(OPERATORLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New));47 40 } 48 41 49 public override IOperation Apply(IScope scope) { 50 int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data; 51 52 InitDefaultOperatorLibrary(); 53 54 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(OPERATORLIBRARY), operatorLibrary)); 55 return null; 56 } 57 58 private void InitDefaultOperatorLibrary() { 42 protected override FunctionLibrary CreateFunctionLibrary() { 59 43 And and = new And(); 60 44 Or or = new Or(); 61 //Not not = new Not();45 Not not = new Not(); 62 46 Nand nand = new Nand(); 63 47 Nor nor = new Nor(); 64 //Xor xor = new Xor();65 variable = new HeuristicLab.GP.Boolean.Variable();48 Xor xor = new Xor(); 49 Variable variable = new Variable(); 66 50 67 51 IFunction[] allFunctions = new IFunction[] { 68 52 and, 69 53 or, 70 //not,54 not, 71 55 nand, 72 56 nor, 73 //xor,57 xor, 74 58 variable 75 59 }; … … 77 61 SetAllowedSubOperators(and, allFunctions); 78 62 SetAllowedSubOperators(or, allFunctions); 79 //SetAllowedSubOperators(not, allFunctions);63 SetAllowedSubOperators(not, allFunctions); 80 64 SetAllowedSubOperators(nand, allFunctions); 81 65 SetAllowedSubOperators(nor, allFunctions); 82 //SetAllowedSubOperators(xor, allFunctions);66 SetAllowedSubOperators(xor, allFunctions); 83 67 84 operatorLibrary = new GPOperatorLibrary(); 85 operatorLibrary.GPOperatorGroup.AddOperator(and); 86 operatorLibrary.GPOperatorGroup.AddOperator(or); 87 //operatorLibrary.GPOperatorGroup.AddOperator(not); 88 operatorLibrary.GPOperatorGroup.AddOperator(nand); 89 operatorLibrary.GPOperatorGroup.AddOperator(nor); 90 //operatorLibrary.GPOperatorGroup.AddOperator(xor); 91 operatorLibrary.GPOperatorGroup.AddOperator(variable); 92 } 93 94 private void SetAllowedSubOperators(IFunction f, IFunction[] gs) { 95 foreach (IConstraint c in f.Constraints) { 96 if (c is SubOperatorTypeConstraint) { 97 SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint; 98 typeConstraint.Clear(); 99 foreach (IFunction g in gs) { 100 typeConstraint.AddOperator(g); 101 } 102 } else if (c is AllSubOperatorsTypeConstraint) { 103 AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint; 104 typeConstraint.Clear(); 105 foreach (IFunction g in gs) { 106 typeConstraint.AddOperator(g); 107 } 108 } 109 } 68 var functionLibrary = new FunctionLibrary(); 69 functionLibrary.AddFunction(and); 70 functionLibrary.AddFunction(or); 71 functionLibrary.AddFunction(not); 72 functionLibrary.AddFunction(nand); 73 functionLibrary.AddFunction(nor); 74 functionLibrary.AddFunction(xor); 75 functionLibrary.AddFunction(variable); 76 return functionLibrary; 110 77 } 111 78 } -
trunk/sources/HeuristicLab.GP.Boolean/3.3/HeuristicLab.GP.Boolean-3.3.csproj
r1534 r2222 81 81 <ItemGroup> 82 82 <Compile Include="BooleanTreeInterpreter.cs" /> 83 <Compile Include="Not.cs" /> 84 <Compile Include="Xor.cs" /> 85 <Compile Include="Or.cs" /> 86 <Compile Include="Nand.cs" /> 87 <Compile Include="Nor.cs" /> 83 <Compile Include="Symbols\And.cs" /> 84 <Compile Include="Symbols\Nand.cs" /> 85 <Compile Include="Symbols\Nor.cs" /> 86 <Compile Include="Symbols\Not.cs" /> 87 <Compile Include="Symbols\Or.cs" /> 88 <Compile Include="Symbols\SymbolTable.cs" /> 89 <Compile Include="Symbols\Variable.cs" /> 90 <Compile Include="Symbols\VariableFunctionTree.cs" /> 91 <Compile Include="Symbols\Xor.cs" /> 88 92 <Compile Include="Evaluator.cs" /> 89 93 <Compile Include="FunctionLibraryInjector.cs" /> 90 94 <Compile Include="HeuristicLabGPBooleanPlugin.cs" /> 91 <Compile Include="And.cs" />92 <Compile Include="Variable.cs" />93 95 <Compile Include="Properties\AssemblyInfo.cs" /> 94 <Compile Include="SymbolTable.cs" />95 96 </ItemGroup> 96 97 <ItemGroup> … … 99 100 </ItemGroup> 100 101 <ItemGroup> 101 <ProjectReference Include="..\..\HeuristicLab.Constraints\3.2\HeuristicLab.Constraints-3.2.csproj">102 <Project>{FCD62C6F-4793-4593-AE9A-0BDCA256EE99}</Project>103 <Name>HeuristicLab.Constraints-3.2</Name>104 </ProjectReference>105 102 <ProjectReference Include="..\..\HeuristicLab.Core\3.2\HeuristicLab.Core-3.2.csproj"> 106 103 <Project>{F43B59AB-2B8C-4570-BC1E-15592086517C}</Project> … … 114 111 <Project>{F473D9AF-3F09-4296-9F28-3C65118DAFFA}</Project> 115 112 <Name>HeuristicLab.Data-3.2</Name> 113 </ProjectReference> 114 <ProjectReference Include="..\..\HeuristicLab.GP.Interfaces\3.3\HeuristicLab.GP.Interfaces-3.3.csproj"> 115 <Project>{924B6BEA-9A99-40FE-9334-5C01E8D540EC}</Project> 116 <Name>HeuristicLab.GP.Interfaces-3.3</Name> 116 117 </ProjectReference> 117 118 <ProjectReference Include="..\..\HeuristicLab.GP\3.3\HeuristicLab.GP-3.3.csproj"> -
trunk/sources/HeuristicLab.GP.Boolean/3.3/HeuristicLabGPBooleanPlugin.cs
r1529 r2222 28 28 [ClassInfo(Name = "HeuristicLab.GP.Boolean-3.3")] 29 29 [PluginFile(Filename = "HeuristicLab.GP.Boolean-3.3.dll", Filetype = PluginFileType.Assembly)] 30 [Dependency(Dependency = "HeuristicLab.Constraints-3.2")]31 [Dependency(Dependency = "HeuristicLab.GP-3.3")]32 30 [Dependency(Dependency = "HeuristicLab.Core-3.2")] 33 31 [Dependency(Dependency = "HeuristicLab.Data-3.2")] 34 32 [Dependency(Dependency = "HeuristicLab.DataAnalysis-3.2")] 33 [Dependency(Dependency = "HeuristicLab.GP-3.3")] 34 [Dependency(Dependency = "HeuristicLab.GP.Interfaces-3.3")] 35 35 [Dependency(Dependency = "HeuristicLab.Operators-3.2")] 36 36 [Dependency(Dependency = "HeuristicLab.Random-3.2")]
Note: See TracChangeset
for help on using the changeset viewer.