Changeset 2222 for trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjector.cs
- Timestamp:
- 08/03/09 12:26:42 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjector.cs
r2174 r2222 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 using System.Text;25 using System.Xml;26 23 using HeuristicLab.Core; 27 24 using HeuristicLab.Data; 28 using HeuristicLab.DataAnalysis; 29 using HeuristicLab.Constraints; 30 using StructId = HeuristicLab.GP.StructureIdentification; 25 using HeuristicLab.GP.Interfaces; 26 using HeuristicLab.GP.SantaFe; 31 27 32 28 namespace HeuristicLab.GP.StructureIdentification { 33 public class FunctionLibraryInjector : OperatorBase { 34 private const string FUNCTIONLIBRARY = "FunctionLibrary"; 35 private const string TARGETVARIABLE = "TargetVariable"; 29 public class FunctionLibraryInjector : FunctionLibraryInjectorBase { 36 30 private const string MINTIMEOFFSET = "MinTimeOffset"; 37 31 private const string MAXTIMEOFFSET = "MaxTimeOffset"; … … 62 56 private const string XOR_ALLOWED = "Xor"; 63 57 58 private int minTimeOffset; 59 private int maxTimeOffset; 64 60 65 61 public override string Description { … … 69 65 public FunctionLibraryInjector() 70 66 : base() { 71 AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In));72 67 AddVariableInfo(new VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), VariableKind.In)); 73 68 AddVariableInfo(new VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), VariableKind.In)); 74 AddVariableInfo(new VariableInfo(FUNCTIONLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New));75 69 76 70 AddVariable(DIFFERENTIALS_ALLOWED, false); … … 107 101 108 102 public override IOperation Apply(IScope scope) { 109 StructId.Variable variable;110 GPOperatorLibrary operatorLibrary;111 int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;112 113 103 // try to get minTimeOffset (use 0 as default if not available) 114 104 IItem minTimeOffsetItem = GetVariableValue(MINTIMEOFFSET, scope, true, false); 115 intminTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;105 minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data; 116 106 // try to get maxTimeOffset (use 0 as default if not available) 117 107 IItem maxTimeOffsetItem = GetVariableValue(MAXTIMEOFFSET, scope, true, false); 118 int maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data; 119 120 variable = new StructId.Variable(); 121 StructId.Constant constant = new StructId.Constant(); 122 StructId.Differential differential = new Differential(); 123 StructId.Addition addition = new StructId.Addition(); 124 StructId.And and = new StructId.And(); 125 StructId.Average average = new StructId.Average(); 126 StructId.Cosinus cosinus = new StructId.Cosinus(); 127 StructId.Division division = new StructId.Division(); 128 StructId.Equal equal = new StructId.Equal(); 129 StructId.Exponential exponential = new StructId.Exponential(); 130 StructId.GreaterThan greaterThan = new StructId.GreaterThan(); 131 StructId.IfThenElse ifThenElse = new StructId.IfThenElse(); 132 StructId.LessThan lessThan = new StructId.LessThan(); 133 StructId.Logarithm logarithm = new StructId.Logarithm(); 134 StructId.Multiplication multiplication = new StructId.Multiplication(); 135 StructId.Not not = new StructId.Not(); 136 StructId.Or or = new StructId.Or(); 137 StructId.Power power = new StructId.Power(); 138 StructId.Signum signum = new StructId.Signum(); 139 StructId.Sinus sinus = new StructId.Sinus(); 140 StructId.Sqrt sqrt = new StructId.Sqrt(); 141 StructId.Subtraction subtraction = new StructId.Subtraction(); 142 StructId.Tangens tangens = new StructId.Tangens(); 143 StructId.Xor xor = new StructId.Xor(); 144 145 146 List<IOperator> booleanFunctions = new List<IOperator>(); 147 ConditionalAddOperator(AND_ALLOWED, and, booleanFunctions); 148 ConditionalAddOperator(EQUAL_ALLOWED, equal, booleanFunctions); 149 ConditionalAddOperator(GREATERTHAN_ALLOWED, greaterThan, booleanFunctions); 150 ConditionalAddOperator(LESSTHAN_ALLOWED, lessThan, booleanFunctions); 151 ConditionalAddOperator(NOT_ALLOWED, not, booleanFunctions); 152 ConditionalAddOperator(OR_ALLOWED, or, booleanFunctions); 153 ConditionalAddOperator(XOR_ALLOWED, xor, booleanFunctions); 154 155 List<IOperator> doubleFunctions = new List<IOperator>(); 156 ConditionalAddOperator(DIFFERENTIALS_ALLOWED, differential, doubleFunctions); 157 ConditionalAddOperator(VARIABLES_ALLOWED, variable, doubleFunctions); 158 ConditionalAddOperator(CONSTANTS_ALLOWED, constant, doubleFunctions); 159 ConditionalAddOperator(ADDITION_ALLOWED, addition, doubleFunctions); 160 ConditionalAddOperator(AVERAGE_ALLOWED, average, doubleFunctions); 161 ConditionalAddOperator(COSINUS_ALLOWED, cosinus, doubleFunctions); 162 ConditionalAddOperator(DIVISION_ALLOWED, division, doubleFunctions); 163 ConditionalAddOperator(EXPONENTIAL_ALLOWED, exponential, doubleFunctions); 164 ConditionalAddOperator(IFTHENELSE_ALLOWED, ifThenElse, doubleFunctions); 165 ConditionalAddOperator(LOGARTIHM_ALLOWED, logarithm, doubleFunctions); 166 ConditionalAddOperator(MULTIPLICATION_ALLOWED, multiplication, doubleFunctions); 167 ConditionalAddOperator(POWER_ALLOWED, power, doubleFunctions); 168 ConditionalAddOperator(SIGNUM_ALLOWED, signum, doubleFunctions); 169 ConditionalAddOperator(SINUS_ALLOWED, sinus, doubleFunctions); 170 ConditionalAddOperator(SQRT_ALLOWED, sqrt, doubleFunctions); 171 ConditionalAddOperator(SUBTRACTION_ALLOWED, subtraction, doubleFunctions); 172 ConditionalAddOperator(TANGENS_ALLOWED, tangens, doubleFunctions); 108 maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data; 109 110 return base.Apply(scope); 111 } 112 113 protected override FunctionLibrary CreateFunctionLibrary() { 114 FunctionLibrary functionLibrary = new FunctionLibrary(); 115 116 Variable variable = new Variable(); 117 Constant constant = new Constant(); 118 Differential differential = new Differential(); 119 Addition addition = new Addition(); 120 And and = new And(); 121 Average average = new Average(); 122 Cosinus cosinus = new Cosinus(); 123 Division division = new Division(); 124 Equal equal = new Equal(); 125 Exponential exponential = new Exponential(); 126 GreaterThan greaterThan = new GreaterThan(); 127 IfThenElse ifThenElse = new IfThenElse(); 128 LessThan lessThan = new LessThan(); 129 Logarithm logarithm = new Logarithm(); 130 Multiplication multiplication = new Multiplication(); 131 Not not = new Not(); 132 Or or = new Or(); 133 Power power = new Power(); 134 Signum signum = new Signum(); 135 Sinus sinus = new Sinus(); 136 Sqrt sqrt = new Sqrt(); 137 Subtraction subtraction = new Subtraction(); 138 Tangens tangens = new Tangens(); 139 Xor xor = new Xor(); 140 141 142 List<IFunction> booleanFunctions = new List<IFunction>(); 143 ConditionalAddFunction(AND_ALLOWED, and, booleanFunctions); 144 ConditionalAddFunction(EQUAL_ALLOWED, equal, booleanFunctions); 145 ConditionalAddFunction(GREATERTHAN_ALLOWED, greaterThan, booleanFunctions); 146 ConditionalAddFunction(LESSTHAN_ALLOWED, lessThan, booleanFunctions); 147 ConditionalAddFunction(NOT_ALLOWED, not, booleanFunctions); 148 ConditionalAddFunction(OR_ALLOWED, or, booleanFunctions); 149 ConditionalAddFunction(XOR_ALLOWED, xor, booleanFunctions); 150 151 List<IFunction> doubleFunctions = new List<IFunction>(); 152 ConditionalAddFunction(DIFFERENTIALS_ALLOWED, differential, doubleFunctions); 153 ConditionalAddFunction(VARIABLES_ALLOWED, variable, doubleFunctions); 154 ConditionalAddFunction(CONSTANTS_ALLOWED, constant, doubleFunctions); 155 ConditionalAddFunction(ADDITION_ALLOWED, addition, doubleFunctions); 156 ConditionalAddFunction(AVERAGE_ALLOWED, average, doubleFunctions); 157 ConditionalAddFunction(COSINUS_ALLOWED, cosinus, doubleFunctions); 158 ConditionalAddFunction(DIVISION_ALLOWED, division, doubleFunctions); 159 ConditionalAddFunction(EXPONENTIAL_ALLOWED, exponential, doubleFunctions); 160 ConditionalAddFunction(IFTHENELSE_ALLOWED, ifThenElse, doubleFunctions); 161 ConditionalAddFunction(LOGARTIHM_ALLOWED, logarithm, doubleFunctions); 162 ConditionalAddFunction(MULTIPLICATION_ALLOWED, multiplication, doubleFunctions); 163 ConditionalAddFunction(POWER_ALLOWED, power, doubleFunctions); 164 ConditionalAddFunction(SIGNUM_ALLOWED, signum, doubleFunctions); 165 ConditionalAddFunction(SINUS_ALLOWED, sinus, doubleFunctions); 166 ConditionalAddFunction(SQRT_ALLOWED, sqrt, doubleFunctions); 167 ConditionalAddFunction(SUBTRACTION_ALLOWED, subtraction, doubleFunctions); 168 ConditionalAddFunction(TANGENS_ALLOWED, tangens, doubleFunctions); 173 169 174 170 SetAllowedSubOperators(and, booleanFunctions); … … 196 192 SetAllowedSubOperators(tangens, doubleFunctions); 197 193 198 operatorLibrary = new GPOperatorLibrary(); 199 ConditionalAddOperator(DIFFERENTIALS_ALLOWED, operatorLibrary, differential); 200 ConditionalAddOperator(VARIABLES_ALLOWED, operatorLibrary, variable); 201 ConditionalAddOperator(CONSTANTS_ALLOWED, operatorLibrary, constant); 202 ConditionalAddOperator(ADDITION_ALLOWED, operatorLibrary, addition); 203 ConditionalAddOperator(AVERAGE_ALLOWED, operatorLibrary, average); 204 ConditionalAddOperator(AND_ALLOWED, operatorLibrary, and); 205 ConditionalAddOperator(COSINUS_ALLOWED, operatorLibrary, cosinus); 206 ConditionalAddOperator(DIVISION_ALLOWED, operatorLibrary, division); 207 ConditionalAddOperator(EQUAL_ALLOWED, operatorLibrary, equal); 208 ConditionalAddOperator(EXPONENTIAL_ALLOWED, operatorLibrary, exponential); 209 ConditionalAddOperator(GREATERTHAN_ALLOWED, operatorLibrary, greaterThan); 210 ConditionalAddOperator(IFTHENELSE_ALLOWED, operatorLibrary, ifThenElse); 211 ConditionalAddOperator(LESSTHAN_ALLOWED, operatorLibrary, lessThan); 212 ConditionalAddOperator(LOGARTIHM_ALLOWED, operatorLibrary, logarithm); 213 ConditionalAddOperator(MULTIPLICATION_ALLOWED, operatorLibrary, multiplication); 214 ConditionalAddOperator(NOT_ALLOWED, operatorLibrary, not); 215 ConditionalAddOperator(POWER_ALLOWED, operatorLibrary, power); 216 ConditionalAddOperator(OR_ALLOWED, operatorLibrary, or); 217 ConditionalAddOperator(SIGNUM_ALLOWED, operatorLibrary, signum); 218 ConditionalAddOperator(SINUS_ALLOWED, operatorLibrary, sinus); 219 ConditionalAddOperator(SQRT_ALLOWED, operatorLibrary, sqrt); 220 ConditionalAddOperator(SUBTRACTION_ALLOWED, operatorLibrary, subtraction); 221 ConditionalAddOperator(TANGENS_ALLOWED, operatorLibrary, tangens); 222 ConditionalAddOperator(XOR_ALLOWED, operatorLibrary, xor); 194 ConditionalAddOperator(DIFFERENTIALS_ALLOWED, functionLibrary, differential); 195 ConditionalAddOperator(VARIABLES_ALLOWED, functionLibrary, variable); 196 ConditionalAddOperator(CONSTANTS_ALLOWED, functionLibrary, constant); 197 ConditionalAddOperator(ADDITION_ALLOWED, functionLibrary, addition); 198 ConditionalAddOperator(AVERAGE_ALLOWED, functionLibrary, average); 199 ConditionalAddOperator(AND_ALLOWED, functionLibrary, and); 200 ConditionalAddOperator(COSINUS_ALLOWED, functionLibrary, cosinus); 201 ConditionalAddOperator(DIVISION_ALLOWED, functionLibrary, division); 202 ConditionalAddOperator(EQUAL_ALLOWED, functionLibrary, equal); 203 ConditionalAddOperator(EXPONENTIAL_ALLOWED, functionLibrary, exponential); 204 ConditionalAddOperator(GREATERTHAN_ALLOWED, functionLibrary, greaterThan); 205 ConditionalAddOperator(IFTHENELSE_ALLOWED, functionLibrary, ifThenElse); 206 ConditionalAddOperator(LESSTHAN_ALLOWED, functionLibrary, lessThan); 207 ConditionalAddOperator(LOGARTIHM_ALLOWED, functionLibrary, logarithm); 208 ConditionalAddOperator(MULTIPLICATION_ALLOWED, functionLibrary, multiplication); 209 ConditionalAddOperator(NOT_ALLOWED, functionLibrary, not); 210 ConditionalAddOperator(POWER_ALLOWED, functionLibrary, power); 211 ConditionalAddOperator(OR_ALLOWED, functionLibrary, or); 212 ConditionalAddOperator(SIGNUM_ALLOWED, functionLibrary, signum); 213 ConditionalAddOperator(SINUS_ALLOWED, functionLibrary, sinus); 214 ConditionalAddOperator(SQRT_ALLOWED, functionLibrary, sqrt); 215 ConditionalAddOperator(SUBTRACTION_ALLOWED, functionLibrary, subtraction); 216 ConditionalAddOperator(TANGENS_ALLOWED, functionLibrary, tangens); 217 ConditionalAddOperator(XOR_ALLOWED, functionLibrary, xor); 223 218 224 219 variable.SetConstraints(minTimeOffset, maxTimeOffset); 225 220 differential.SetConstraints(minTimeOffset, maxTimeOffset); 226 221 227 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), operatorLibrary)); 228 229 return null; 230 } 231 232 private void ConditionalAddOperator(string condName, IOperator op, List<IOperator> list) { 233 if (GetVariableValue<BoolData>(condName, null, false).Data) list.Add(op); 234 } 235 236 private void ConditionalAddOperator(string condName, GPOperatorLibrary operatorLibrary, IOperator op) { 237 if (GetVariableValue<BoolData>(condName, null, false).Data) operatorLibrary.GPOperatorGroup.AddOperator(op); 238 } 239 240 private void SetAllowedSubOperators(IFunction f, List<IOperator> gs) { 241 foreach (IConstraint c in f.Constraints) { 242 if (c is SubOperatorTypeConstraint) { 243 SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint; 244 typeConstraint.Clear(); 245 foreach (IOperator g in gs) { 246 typeConstraint.AddOperator(g); 247 } 248 } else if (c is AllSubOperatorsTypeConstraint) { 249 AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint; 250 typeConstraint.Clear(); 251 foreach (IOperator g in gs) { 252 typeConstraint.AddOperator(g); 253 } 254 } 255 } 256 } 257 258 private void SetAllowedSubOperators(IFunction f, int p, List<IOperator> gs) { 259 foreach (IConstraint c in f.Constraints) { 260 if (c is SubOperatorTypeConstraint) { 261 SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint; 262 if (typeConstraint.SubOperatorIndex.Data == p) { 263 typeConstraint.Clear(); 264 foreach (IOperator g in gs) { 265 typeConstraint.AddOperator(g); 266 } 267 } 268 } 269 } 222 return functionLibrary; 223 } 224 225 private void ConditionalAddFunction(string condName, IFunction fun, List<IFunction> list) { 226 if (GetVariableValue<BoolData>(condName, null, false).Data) list.Add(fun); 227 } 228 229 private void ConditionalAddOperator(string condName, FunctionLibrary functionLib, IFunction op) { 230 if (GetVariableValue<BoolData>(condName, null, false).Data) functionLib.AddFunction(op); 270 231 } 271 232 }
Note: See TracChangeset
for help on using the changeset viewer.