Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.StructureIdentification/FunctionLibraryInjector.cs @ 4384

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

implemented #247 (FunctionLibraryInjector should have a parameter to switch between autoregressive and non-autoregressive modeling)

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