Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjector.cs @ 6479

Last change on this file since 6479 was 1911, checked in by gkronber, 16 years ago

Use default values for min- and max time offset and for the autoregressive modeling flag. #579 (Configurable FunctionLibraryInjector for GP.StructureIdenfication)

File size: 15.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using HeuristicLab.Constraints;
30using StructId = HeuristicLab.GP.StructureIdentification;
31
32namespace HeuristicLab.GP.StructureIdentification {
33  public class FunctionLibraryInjector : OperatorBase {
34    private const string FUNCTIONLIBRARY = "FunctionLibrary";
35    private const string TARGETVARIABLE = "TargetVariable";
36    private const string ALLOWEDFEATURES = "AllowedFeatures";
37    private const string AUTOREGRESSIVE = "Autoregressive";
38    private const string MINTIMEOFFSET = "MinTimeOffset";
39    private const string MAXTIMEOFFSET = "MaxTimeOffset";
40
41    private const string DIFFERENTIALS_ALLOWED = "Differentials";
42    private const string VARIABLES_ALLOWED = "Variables";
43    private const string CONSTANTS_ALLOWED = "Constants";
44    private const string ADDITION_ALLOWED = "Addition";
45    private const string AVERAGE_ALLOWED = "Average";
46    private const string AND_ALLOWED = "And";
47    private const string COSINUS_ALLOWED = "Cosinus";
48    private const string DIVISION_ALLOWED = "Division";
49    private const string EQUAL_ALLOWED = "Equal";
50    private const string EXPONENTIAL_ALLOWED = "Exponential";
51    private const string GREATERTHAN_ALLOWED = "GreaterThan";
52    private const string IFTHENELSE_ALLOWED = "IfThenElse";
53    private const string LESSTHAN_ALLOWED = "LessThan";
54    private const string LOGARTIHM_ALLOWED = "Logarithm";
55    private const string MULTIPLICATION_ALLOWED = "Multiplication";
56    private const string NOT_ALLOWED = "Not";
57    private const string POWER_ALLOWED = "Power";
58    private const string OR_ALLOWED = "Or";
59    private const string SIGNUM_ALLOWED = "Signum";
60    private const string SINUS_ALLOWED = "Sinus";
61    private const string SQRT_ALLOWED = "SquareRoot";
62    private const string SUBTRACTION_ALLOWED = "Subtraction";
63    private const string TANGENS_ALLOWED = "Tangens";
64    private const string XOR_ALLOWED = "Xor";
65
66
67    public override string Description {
68      get { return @"Injects a configurable function library for regression and classification problems."; }
69    }
70
71    public FunctionLibraryInjector()
72      : base() {
73      AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In));
74      AddVariableInfo(new VariableInfo(ALLOWEDFEATURES, "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In));
75      AddVariableInfo(new Core.VariableInfo(AUTOREGRESSIVE, "Switch to turn on/off autoregressive modeling (wether to allow the target variable as input)", typeof(BoolData), Core.VariableKind.In));
76      AddVariableInfo(new Core.VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), Core.VariableKind.In));
77      AddVariableInfo(new Core.VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), Core.VariableKind.In));
78      AddVariableInfo(new VariableInfo(FUNCTIONLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New));
79
80      AddVariable(DIFFERENTIALS_ALLOWED, false);
81      AddVariable(VARIABLES_ALLOWED, true);
82      AddVariable(CONSTANTS_ALLOWED, true);
83      AddVariable(ADDITION_ALLOWED, true);
84      AddVariable(AVERAGE_ALLOWED, false);
85      AddVariable(AND_ALLOWED, true);
86      AddVariable(COSINUS_ALLOWED, true);
87      AddVariable(DIVISION_ALLOWED, true);
88      AddVariable(EQUAL_ALLOWED, true);
89      AddVariable(EXPONENTIAL_ALLOWED, true);
90      AddVariable(GREATERTHAN_ALLOWED, true);
91      AddVariable(IFTHENELSE_ALLOWED, true);
92      AddVariable(LESSTHAN_ALLOWED, true);
93      AddVariable(LOGARTIHM_ALLOWED, true);
94      AddVariable(MULTIPLICATION_ALLOWED, true);
95      AddVariable(NOT_ALLOWED, true);
96      AddVariable(POWER_ALLOWED, true);
97      AddVariable(OR_ALLOWED, true);
98      AddVariable(SIGNUM_ALLOWED, true);
99      AddVariable(SINUS_ALLOWED, true);
100      AddVariable(SQRT_ALLOWED, true);
101      AddVariable(SUBTRACTION_ALLOWED, true);
102      AddVariable(TANGENS_ALLOWED, true);
103      AddVariable(XOR_ALLOWED, false);
104    }
105
106    private void AddVariable(string name, bool allowed) {
107      AddVariableInfo(new VariableInfo(name, name + " allowed", typeof(BoolData), Core.VariableKind.New));
108      GetVariableInfo(name).Local = true;
109      AddVariable(new Core.Variable(name, new BoolData(allowed)));
110    }
111
112    public override IOperation Apply(IScope scope) {
113      StructId.Variable variable;
114      GPOperatorLibrary operatorLibrary;
115
116      ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>(ALLOWEDFEATURES, scope, true);
117      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
118
119      // try to get minTimeOffset (use 0 as default if not available)
120      IItem minTimeOffsetItem = GetVariableValue(MINTIMEOFFSET, scope, true, false);
121      int minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;
122      // try to get maxTimeOffset (use 0 as default if not available)
123      IItem maxTimeOffsetItem = GetVariableValue(MAXTIMEOFFSET, scope, true, false);
124      int maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data;
125      // try to get flag autoregressive (use false as default if not available)
126      IItem autoRegItem = GetVariableValue(AUTOREGRESSIVE, scope, true, false);
127      bool autoregressive = autoRegItem == null ? false : ((BoolData)autoRegItem).Data;
128
129      if (autoregressive) {
130        // make sure the target-variable occures in list of allowed features
131        if (!allowedFeatures.Exists(d => d.Data == targetVariable)) allowedFeatures.Add(new IntData(targetVariable));
132      } else {
133        // remove the target-variable in case it occures in allowed features
134        List<IntData> ts = allowedFeatures.FindAll(d => d.Data == targetVariable);
135        foreach (IntData t in ts) allowedFeatures.Remove(t);
136      }
137
138      variable = new StructId.Variable();
139      StructId.Constant constant = new StructId.Constant();
140      StructId.Differential differential = new Differential();
141      StructId.Addition addition = new StructId.Addition();
142      StructId.And and = new StructId.And();
143      StructId.Average average = new StructId.Average();
144      StructId.Cosinus cosinus = new StructId.Cosinus();
145      StructId.Division division = new StructId.Division();
146      StructId.Equal equal = new StructId.Equal();
147      StructId.Exponential exponential = new StructId.Exponential();
148      StructId.GreaterThan greaterThan = new StructId.GreaterThan();
149      StructId.IfThenElse ifThenElse = new StructId.IfThenElse();
150      StructId.LessThan lessThan = new StructId.LessThan();
151      StructId.Logarithm logarithm = new StructId.Logarithm();
152      StructId.Multiplication multiplication = new StructId.Multiplication();
153      StructId.Not not = new StructId.Not();
154      StructId.Or or = new StructId.Or();
155      StructId.Power power = new StructId.Power();
156      StructId.Signum signum = new StructId.Signum();
157      StructId.Sinus sinus = new StructId.Sinus();
158      StructId.Sqrt sqrt = new StructId.Sqrt();
159      StructId.Subtraction subtraction = new StructId.Subtraction();
160      StructId.Tangens tangens = new StructId.Tangens();
161      StructId.Xor xor = new StructId.Xor();
162
163
164      List<IOperator> booleanFunctions = new List<IOperator>();
165      ConditionalAddOperator(AND_ALLOWED, and, booleanFunctions);
166      ConditionalAddOperator(EQUAL_ALLOWED, equal, booleanFunctions);
167      ConditionalAddOperator(GREATERTHAN_ALLOWED, greaterThan, booleanFunctions);
168      ConditionalAddOperator(LESSTHAN_ALLOWED, lessThan, booleanFunctions);
169      ConditionalAddOperator(NOT_ALLOWED, not, booleanFunctions);
170      ConditionalAddOperator(OR_ALLOWED, or, booleanFunctions);
171      ConditionalAddOperator(XOR_ALLOWED, xor, booleanFunctions);
172
173      List<IOperator> doubleFunctions = new List<IOperator>();
174      ConditionalAddOperator(DIFFERENTIALS_ALLOWED, differential, doubleFunctions);
175      ConditionalAddOperator(VARIABLES_ALLOWED, variable, doubleFunctions);
176      ConditionalAddOperator(CONSTANTS_ALLOWED, constant, doubleFunctions);
177      ConditionalAddOperator(ADDITION_ALLOWED, addition, doubleFunctions);
178      ConditionalAddOperator(AVERAGE_ALLOWED, average, doubleFunctions);
179      ConditionalAddOperator(COSINUS_ALLOWED, cosinus, doubleFunctions);
180      ConditionalAddOperator(DIVISION_ALLOWED, division, doubleFunctions);
181      ConditionalAddOperator(EXPONENTIAL_ALLOWED, exponential, doubleFunctions);
182      ConditionalAddOperator(IFTHENELSE_ALLOWED, ifThenElse, doubleFunctions);
183      ConditionalAddOperator(LOGARTIHM_ALLOWED, logarithm, doubleFunctions);
184      ConditionalAddOperator(MULTIPLICATION_ALLOWED, multiplication, doubleFunctions);
185      ConditionalAddOperator(POWER_ALLOWED, power, doubleFunctions);
186      ConditionalAddOperator(SIGNUM_ALLOWED, signum, doubleFunctions);
187      ConditionalAddOperator(SINUS_ALLOWED, sinus, doubleFunctions);
188      ConditionalAddOperator(SQRT_ALLOWED, sqrt, doubleFunctions);
189      ConditionalAddOperator(SUBTRACTION_ALLOWED, subtraction, doubleFunctions);
190      ConditionalAddOperator(TANGENS_ALLOWED, tangens, doubleFunctions);
191
192      SetAllowedSubOperators(and, booleanFunctions);
193      SetAllowedSubOperators(equal, doubleFunctions);
194      SetAllowedSubOperators(greaterThan, doubleFunctions);
195      SetAllowedSubOperators(lessThan, doubleFunctions);
196      SetAllowedSubOperators(not, booleanFunctions);
197      SetAllowedSubOperators(or, booleanFunctions);
198      SetAllowedSubOperators(xor, booleanFunctions);
199      SetAllowedSubOperators(addition, doubleFunctions);
200      SetAllowedSubOperators(average, doubleFunctions);
201      SetAllowedSubOperators(cosinus, doubleFunctions);
202      SetAllowedSubOperators(division, doubleFunctions);
203      SetAllowedSubOperators(exponential, doubleFunctions);
204      SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
205      SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
206      SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
207      SetAllowedSubOperators(logarithm, doubleFunctions);
208      SetAllowedSubOperators(multiplication, doubleFunctions);
209      SetAllowedSubOperators(power, doubleFunctions);
210      SetAllowedSubOperators(signum, doubleFunctions);
211      SetAllowedSubOperators(sinus, doubleFunctions);
212      SetAllowedSubOperators(sqrt, doubleFunctions);
213      SetAllowedSubOperators(subtraction, doubleFunctions);
214      SetAllowedSubOperators(tangens, doubleFunctions);
215
216      operatorLibrary = new GPOperatorLibrary();
217      ConditionalAddOperator(DIFFERENTIALS_ALLOWED, operatorLibrary, differential);
218      ConditionalAddOperator(VARIABLES_ALLOWED, operatorLibrary, variable);
219      ConditionalAddOperator(CONSTANTS_ALLOWED, operatorLibrary, constant);
220      ConditionalAddOperator(ADDITION_ALLOWED, operatorLibrary, addition);
221      ConditionalAddOperator(AVERAGE_ALLOWED, operatorLibrary, average);
222      ConditionalAddOperator(AND_ALLOWED, operatorLibrary, and);
223      ConditionalAddOperator(COSINUS_ALLOWED, operatorLibrary, cosinus);
224      ConditionalAddOperator(DIVISION_ALLOWED, operatorLibrary, division);
225      ConditionalAddOperator(EQUAL_ALLOWED, operatorLibrary, equal);
226      ConditionalAddOperator(EXPONENTIAL_ALLOWED, operatorLibrary, exponential);
227      ConditionalAddOperator(GREATERTHAN_ALLOWED, operatorLibrary, greaterThan);
228      ConditionalAddOperator(IFTHENELSE_ALLOWED, operatorLibrary, ifThenElse);
229      ConditionalAddOperator(LESSTHAN_ALLOWED, operatorLibrary, lessThan);
230      ConditionalAddOperator(LOGARTIHM_ALLOWED, operatorLibrary, logarithm);
231      ConditionalAddOperator(MULTIPLICATION_ALLOWED, operatorLibrary, multiplication);
232      ConditionalAddOperator(NOT_ALLOWED, operatorLibrary, not);
233      ConditionalAddOperator(POWER_ALLOWED, operatorLibrary, power);
234      ConditionalAddOperator(OR_ALLOWED, operatorLibrary, or);
235      ConditionalAddOperator(SIGNUM_ALLOWED, operatorLibrary, signum);
236      ConditionalAddOperator(SINUS_ALLOWED, operatorLibrary, sinus);
237      ConditionalAddOperator(SQRT_ALLOWED, operatorLibrary, sqrt);
238      ConditionalAddOperator(SUBTRACTION_ALLOWED, operatorLibrary, subtraction);
239      ConditionalAddOperator(TANGENS_ALLOWED, operatorLibrary, tangens);
240      ConditionalAddOperator(XOR_ALLOWED, operatorLibrary, xor);
241
242      int[] allowedIndexes = new int[allowedFeatures.Count];
243      for (int i = 0; i < allowedIndexes.Length; i++) {
244        allowedIndexes[i] = allowedFeatures[i].Data;
245      }
246
247      variable.SetConstraints(allowedIndexes, minTimeOffset, maxTimeOffset);
248      differential.SetConstraints(allowedIndexes, minTimeOffset, maxTimeOffset);
249
250      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), operatorLibrary));
251
252      return null;
253    }
254
255    private void ConditionalAddOperator(string condName, IOperator op, List<IOperator> list) {
256      if (GetVariableValue<BoolData>(condName, null, false).Data) list.Add(op);
257    }
258
259    private void ConditionalAddOperator(string condName, GPOperatorLibrary operatorLibrary, IOperator op) {
260      if (GetVariableValue<BoolData>(condName, null, false).Data) operatorLibrary.GPOperatorGroup.AddOperator(op);
261    }
262
263    private void SetAllowedSubOperators(IFunction f, List<IOperator> gs) {
264      foreach (IConstraint c in f.Constraints) {
265        if (c is SubOperatorTypeConstraint) {
266          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
267          typeConstraint.Clear();
268          foreach (IOperator g in gs) {
269            typeConstraint.AddOperator(g);
270          }
271        } else if (c is AllSubOperatorsTypeConstraint) {
272          AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
273          typeConstraint.Clear();
274          foreach (IOperator g in gs) {
275            typeConstraint.AddOperator(g);
276          }
277        }
278      }
279    }
280
281    private void SetAllowedSubOperators(IFunction f, int p, List<IOperator> gs) {
282      foreach (IConstraint c in f.Constraints) {
283        if (c is SubOperatorTypeConstraint) {
284          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
285          if (typeConstraint.SubOperatorIndex.Data == p) {
286            typeConstraint.Clear();
287            foreach (IOperator g in gs) {
288              typeConstraint.AddOperator(g);
289            }
290          }
291        }
292      }
293    }
294  }
295}
Note: See TracBrowser for help on using the repository browser.