Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/FunctionLibraryInjector.cs @ 656

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

merged changesets r644:647 and r651:655 from the GpPluginsRefactoringBranch back into the trunk (#177)

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