Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.GP.StructureIdentification.TimeSeries/3.2/FunctionLibraryInjector.cs @ 2950

Last change on this file since 2950 was 686, checked in by gkronber, 16 years ago

fixed #322

File size: 9.7 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.Collections.Generic;
23using Core = HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Constraints;
26
27namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
28  public class FunctionLibraryInjector : Core.OperatorBase {
29    private const string TARGETVARIABLE = "TargetVariable";
30    private const string AUTOREGRESSIVE = "Autoregressive";
31    private const string ALLOWEDFEATURES = "AllowedFeatures";
32    private const string MINTIMEOFFSET = "MinTimeOffset";
33    private const string MAXTIMEOFFSET = "MaxTimeOffset";
34    private const string FUNCTIONLIBRARY = "FunctionLibrary";
35
36    private Variable variable;
37    private Differential differential;
38    private GPOperatorLibrary operatorLibrary;
39
40    public override string Description {
41      get { return @"Injects a default function library for time series modeling."; }
42    }
43
44    public FunctionLibraryInjector()
45      : base() {
46      AddVariableInfo(new Core.VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), Core.VariableKind.In));
47      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));
48      AddVariableInfo(new Core.VariableInfo(ALLOWEDFEATURES, "List of indexes of allowed features", typeof(ItemList<IntData>), Core.VariableKind.In));
49      AddVariableInfo(new Core.VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), Core.VariableKind.In));
50      AddVariableInfo(new Core.VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), Core.VariableKind.In));
51      AddVariableInfo(new Core.VariableInfo(FUNCTIONLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), Core.VariableKind.New));
52    }
53
54    public override Core.IOperation Apply(Core.IScope scope) {
55      IntData minTimeOffset = GetVariableValue<IntData>(MINTIMEOFFSET, scope, true);
56      IntData maxTimeOffset = GetVariableValue<IntData>(MAXTIMEOFFSET, scope, true);
57      ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>(ALLOWEDFEATURES, scope, true);
58      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
59      bool autoregressive = GetVariableValue<BoolData>(AUTOREGRESSIVE, scope, true).Data;
60
61      if(autoregressive) {
62        // make sure the target-variable occures in list of allowed features
63        if(!allowedFeatures.Exists(d => d.Data == targetVariable)) allowedFeatures.Add(new IntData(targetVariable));
64      } else {
65        // remove the target-variable in case it occures in allowed features
66        List<IntData> ts = allowedFeatures.FindAll(d => d.Data == targetVariable);
67        foreach(IntData t in ts) allowedFeatures.Remove(t);
68      }
69
70      InitDefaultOperatorLibrary();
71
72      int[] allowedIndexes = new int[allowedFeatures.Count];
73      for(int i = 0; i < allowedIndexes.Length; i++) {
74        allowedIndexes[i] = allowedFeatures[i].Data;
75      }
76
77      variable.SetConstraints(allowedIndexes, minTimeOffset.Data, maxTimeOffset.Data);
78      differential.SetConstraints(allowedIndexes, minTimeOffset.Data, maxTimeOffset.Data);
79
80      scope.AddVariable(new Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), operatorLibrary));
81      return null;
82    }
83
84    private void InitDefaultOperatorLibrary() {
85      variable = new Variable();
86      differential = new Differential();
87      Constant constant = new Constant();
88
89      Addition addition = new Addition();
90      And and = new And();
91      Average average = new Average();
92      Cosinus cosinus = new Cosinus();
93      Division division = new Division();
94      Equal equal = new Equal();
95      Exponential exponential = new Exponential();
96      GreaterThan greaterThan = new GreaterThan();
97      IfThenElse ifThenElse = new IfThenElse();
98      LessThan lessThan = new LessThan();
99      Logarithm logarithm = new Logarithm();
100      Multiplication multiplication = new Multiplication();
101      Not not = new Not();
102      Or or = new Or();
103      Power power = new Power();
104      Signum signum = new Signum();
105      Sinus sinus = new Sinus();
106      Sqrt sqrt = new Sqrt();
107      Subtraction subtraction = new Subtraction();
108      Tangens tangens = new Tangens();
109      Xor xor = new Xor();
110
111      IFunction[] booleanFunctions = new IFunction[] {
112        and,
113        equal,
114        greaterThan,
115        lessThan,
116        not,
117        or,
118        xor };
119      IFunction[] doubleFunctions = new IFunction[] {
120        variable,
121        differential,
122        constant,
123        addition,
124        average,
125        cosinus,
126        division,
127        exponential,
128        ifThenElse,
129        logarithm,
130        multiplication,
131        power,
132        signum,
133        sinus,
134        sqrt,
135        subtraction,
136        tangens};
137
138      SetAllowedSubOperators(and, booleanFunctions);
139      SetAllowedSubOperators(equal, doubleFunctions);
140      SetAllowedSubOperators(greaterThan, doubleFunctions);
141      SetAllowedSubOperators(lessThan, doubleFunctions);
142      SetAllowedSubOperators(not, booleanFunctions);
143      SetAllowedSubOperators(or, booleanFunctions);
144      SetAllowedSubOperators(xor, booleanFunctions);
145      SetAllowedSubOperators(addition, doubleFunctions);
146      SetAllowedSubOperators(average, doubleFunctions);
147      SetAllowedSubOperators(cosinus, doubleFunctions);
148      SetAllowedSubOperators(division, doubleFunctions);
149      SetAllowedSubOperators(exponential, doubleFunctions);
150      SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
151      SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
152      SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
153      SetAllowedSubOperators(logarithm, doubleFunctions);
154      SetAllowedSubOperators(multiplication, doubleFunctions);
155      SetAllowedSubOperators(power, doubleFunctions);
156      SetAllowedSubOperators(signum, doubleFunctions);
157      SetAllowedSubOperators(sinus, doubleFunctions);
158      SetAllowedSubOperators(sqrt, doubleFunctions);
159      SetAllowedSubOperators(subtraction, doubleFunctions);
160      SetAllowedSubOperators(tangens, doubleFunctions);
161
162      operatorLibrary = new GPOperatorLibrary();
163      operatorLibrary.GPOperatorGroup.AddOperator(variable);
164      operatorLibrary.GPOperatorGroup.AddOperator(differential);
165      operatorLibrary.GPOperatorGroup.AddOperator(constant);
166      operatorLibrary.GPOperatorGroup.AddOperator(addition);
167      operatorLibrary.GPOperatorGroup.AddOperator(average);
168      operatorLibrary.GPOperatorGroup.AddOperator(and);
169      operatorLibrary.GPOperatorGroup.AddOperator(cosinus);
170      operatorLibrary.GPOperatorGroup.AddOperator(division);
171      operatorLibrary.GPOperatorGroup.AddOperator(equal);
172      operatorLibrary.GPOperatorGroup.AddOperator(exponential);
173      operatorLibrary.GPOperatorGroup.AddOperator(greaterThan);
174      operatorLibrary.GPOperatorGroup.AddOperator(ifThenElse);
175      operatorLibrary.GPOperatorGroup.AddOperator(lessThan);
176      operatorLibrary.GPOperatorGroup.AddOperator(logarithm);
177      operatorLibrary.GPOperatorGroup.AddOperator(multiplication);
178      operatorLibrary.GPOperatorGroup.AddOperator(not);
179      operatorLibrary.GPOperatorGroup.AddOperator(power);
180      operatorLibrary.GPOperatorGroup.AddOperator(or);
181      operatorLibrary.GPOperatorGroup.AddOperator(signum);
182      operatorLibrary.GPOperatorGroup.AddOperator(sinus);
183      operatorLibrary.GPOperatorGroup.AddOperator(sqrt);
184      operatorLibrary.GPOperatorGroup.AddOperator(subtraction);
185      operatorLibrary.GPOperatorGroup.AddOperator(tangens);
186      operatorLibrary.GPOperatorGroup.AddOperator(xor);
187    }
188
189    private void SetAllowedSubOperators(IFunction f, IFunction[] gs) {
190      foreach(Core.IConstraint c in f.Constraints) {
191        if(c is SubOperatorTypeConstraint) {
192          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
193          typeConstraint.Clear();
194          foreach(IFunction g in gs) {
195            typeConstraint.AddOperator(g);
196          }
197        } else if(c is AllSubOperatorsTypeConstraint) {
198          AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
199          typeConstraint.Clear();
200          foreach(IFunction g in gs) {
201            typeConstraint.AddOperator(g);
202          }
203        }
204      }
205    }
206
207    private void SetAllowedSubOperators(IFunction f, int p, IFunction[] gs) {
208      foreach(Core.IConstraint c in f.Constraints) {
209        if(c is SubOperatorTypeConstraint) {
210          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
211          if(typeConstraint.SubOperatorIndex.Data == p) {
212            typeConstraint.Clear();
213            foreach(IFunction g in gs) {
214              typeConstraint.AddOperator(g);
215            }
216          }
217        }
218      }
219    }
220  }
221}
Note: See TracBrowser for help on using the repository browser.