Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjector.cs @ 2165

Last change on this file since 2165 was 2165, checked in by gkronber, 15 years ago

Removed variable AllowedFeatures in all modeling algorithms. #709

File size: 14.1 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 NUMBEROFINPUTVARIABLES = "NumberOfInputVariables";
35    private const string FUNCTIONLIBRARY = "FunctionLibrary";
36    private const string TARGETVARIABLE = "TargetVariable";
37    private const string MINTIMEOFFSET = "MinTimeOffset";
38    private const string MAXTIMEOFFSET = "MaxTimeOffset";
39
40    private const string DIFFERENTIALS_ALLOWED = "Differentials";
41    private const string VARIABLES_ALLOWED = "Variables";
42    private const string CONSTANTS_ALLOWED = "Constants";
43    private const string ADDITION_ALLOWED = "Addition";
44    private const string AVERAGE_ALLOWED = "Average";
45    private const string AND_ALLOWED = "And";
46    private const string COSINUS_ALLOWED = "Cosinus";
47    private const string DIVISION_ALLOWED = "Division";
48    private const string EQUAL_ALLOWED = "Equal";
49    private const string EXPONENTIAL_ALLOWED = "Exponential";
50    private const string GREATERTHAN_ALLOWED = "GreaterThan";
51    private const string IFTHENELSE_ALLOWED = "IfThenElse";
52    private const string LESSTHAN_ALLOWED = "LessThan";
53    private const string LOGARTIHM_ALLOWED = "Logarithm";
54    private const string MULTIPLICATION_ALLOWED = "Multiplication";
55    private const string NOT_ALLOWED = "Not";
56    private const string POWER_ALLOWED = "Power";
57    private const string OR_ALLOWED = "Or";
58    private const string SIGNUM_ALLOWED = "Signum";
59    private const string SINUS_ALLOWED = "Sinus";
60    private const string SQRT_ALLOWED = "SquareRoot";
61    private const string SUBTRACTION_ALLOWED = "Subtraction";
62    private const string TANGENS_ALLOWED = "Tangens";
63    private const string XOR_ALLOWED = "Xor";
64
65
66    public override string Description {
67      get { return @"Injects a configurable function library for regression and classification problems."; }
68    }
69
70    public FunctionLibraryInjector()
71      : base() {
72      AddVariableInfo(new VariableInfo(NUMBEROFINPUTVARIABLES, "The number of available input variables", typeof(IntData), VariableKind.In));
73      AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In));
74      AddVariableInfo(new VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), VariableKind.In));
75      AddVariableInfo(new VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), VariableKind.In));
76      AddVariableInfo(new VariableInfo(FUNCTIONLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New));
77
78      AddVariable(DIFFERENTIALS_ALLOWED, false);
79      AddVariable(VARIABLES_ALLOWED, true);
80      AddVariable(CONSTANTS_ALLOWED, true);
81      AddVariable(ADDITION_ALLOWED, true);
82      AddVariable(AVERAGE_ALLOWED, false);
83      AddVariable(AND_ALLOWED, true);
84      AddVariable(COSINUS_ALLOWED, true);
85      AddVariable(DIVISION_ALLOWED, true);
86      AddVariable(EQUAL_ALLOWED, true);
87      AddVariable(EXPONENTIAL_ALLOWED, true);
88      AddVariable(GREATERTHAN_ALLOWED, true);
89      AddVariable(IFTHENELSE_ALLOWED, true);
90      AddVariable(LESSTHAN_ALLOWED, true);
91      AddVariable(LOGARTIHM_ALLOWED, true);
92      AddVariable(MULTIPLICATION_ALLOWED, true);
93      AddVariable(NOT_ALLOWED, true);
94      AddVariable(POWER_ALLOWED, true);
95      AddVariable(OR_ALLOWED, true);
96      AddVariable(SIGNUM_ALLOWED, true);
97      AddVariable(SINUS_ALLOWED, true);
98      AddVariable(SQRT_ALLOWED, true);
99      AddVariable(SUBTRACTION_ALLOWED, true);
100      AddVariable(TANGENS_ALLOWED, true);
101      AddVariable(XOR_ALLOWED, false);
102    }
103
104    private void AddVariable(string name, bool allowed) {
105      AddVariableInfo(new VariableInfo(name, name + " allowed", typeof(BoolData), Core.VariableKind.New));
106      GetVariableInfo(name).Local = true;
107      AddVariable(new Core.Variable(name, new BoolData(allowed)));
108    }
109
110    public override IOperation Apply(IScope scope) {
111      StructId.Variable variable;
112      GPOperatorLibrary operatorLibrary;
113      int nInputVariables = GetVariableValue<IntData>(NUMBEROFINPUTVARIABLES, scope, true).Data;
114      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
115
116      // try to get minTimeOffset (use 0 as default if not available)
117      IItem minTimeOffsetItem = GetVariableValue(MINTIMEOFFSET, scope, true, false);
118      int minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;
119      // try to get maxTimeOffset (use 0 as default if not available)
120      IItem maxTimeOffsetItem = GetVariableValue(MAXTIMEOFFSET, scope, true, false);
121      int maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data;
122
123      variable = new StructId.Variable();
124      StructId.Constant constant = new StructId.Constant();
125      StructId.Differential differential = new Differential();
126      StructId.Addition addition = new StructId.Addition();
127      StructId.And and = new StructId.And();
128      StructId.Average average = new StructId.Average();
129      StructId.Cosinus cosinus = new StructId.Cosinus();
130      StructId.Division division = new StructId.Division();
131      StructId.Equal equal = new StructId.Equal();
132      StructId.Exponential exponential = new StructId.Exponential();
133      StructId.GreaterThan greaterThan = new StructId.GreaterThan();
134      StructId.IfThenElse ifThenElse = new StructId.IfThenElse();
135      StructId.LessThan lessThan = new StructId.LessThan();
136      StructId.Logarithm logarithm = new StructId.Logarithm();
137      StructId.Multiplication multiplication = new StructId.Multiplication();
138      StructId.Not not = new StructId.Not();
139      StructId.Or or = new StructId.Or();
140      StructId.Power power = new StructId.Power();
141      StructId.Signum signum = new StructId.Signum();
142      StructId.Sinus sinus = new StructId.Sinus();
143      StructId.Sqrt sqrt = new StructId.Sqrt();
144      StructId.Subtraction subtraction = new StructId.Subtraction();
145      StructId.Tangens tangens = new StructId.Tangens();
146      StructId.Xor xor = new StructId.Xor();
147
148
149      List<IOperator> booleanFunctions = new List<IOperator>();
150      ConditionalAddOperator(AND_ALLOWED, and, booleanFunctions);
151      ConditionalAddOperator(EQUAL_ALLOWED, equal, booleanFunctions);
152      ConditionalAddOperator(GREATERTHAN_ALLOWED, greaterThan, booleanFunctions);
153      ConditionalAddOperator(LESSTHAN_ALLOWED, lessThan, booleanFunctions);
154      ConditionalAddOperator(NOT_ALLOWED, not, booleanFunctions);
155      ConditionalAddOperator(OR_ALLOWED, or, booleanFunctions);
156      ConditionalAddOperator(XOR_ALLOWED, xor, booleanFunctions);
157
158      List<IOperator> doubleFunctions = new List<IOperator>();
159      ConditionalAddOperator(DIFFERENTIALS_ALLOWED, differential, doubleFunctions);
160      ConditionalAddOperator(VARIABLES_ALLOWED, variable, doubleFunctions);
161      ConditionalAddOperator(CONSTANTS_ALLOWED, constant, doubleFunctions);
162      ConditionalAddOperator(ADDITION_ALLOWED, addition, doubleFunctions);
163      ConditionalAddOperator(AVERAGE_ALLOWED, average, doubleFunctions);
164      ConditionalAddOperator(COSINUS_ALLOWED, cosinus, doubleFunctions);
165      ConditionalAddOperator(DIVISION_ALLOWED, division, doubleFunctions);
166      ConditionalAddOperator(EXPONENTIAL_ALLOWED, exponential, doubleFunctions);
167      ConditionalAddOperator(IFTHENELSE_ALLOWED, ifThenElse, doubleFunctions);
168      ConditionalAddOperator(LOGARTIHM_ALLOWED, logarithm, doubleFunctions);
169      ConditionalAddOperator(MULTIPLICATION_ALLOWED, multiplication, doubleFunctions);
170      ConditionalAddOperator(POWER_ALLOWED, power, doubleFunctions);
171      ConditionalAddOperator(SIGNUM_ALLOWED, signum, doubleFunctions);
172      ConditionalAddOperator(SINUS_ALLOWED, sinus, doubleFunctions);
173      ConditionalAddOperator(SQRT_ALLOWED, sqrt, doubleFunctions);
174      ConditionalAddOperator(SUBTRACTION_ALLOWED, subtraction, doubleFunctions);
175      ConditionalAddOperator(TANGENS_ALLOWED, tangens, doubleFunctions);
176
177      SetAllowedSubOperators(and, booleanFunctions);
178      SetAllowedSubOperators(equal, doubleFunctions);
179      SetAllowedSubOperators(greaterThan, doubleFunctions);
180      SetAllowedSubOperators(lessThan, doubleFunctions);
181      SetAllowedSubOperators(not, booleanFunctions);
182      SetAllowedSubOperators(or, booleanFunctions);
183      SetAllowedSubOperators(xor, booleanFunctions);
184      SetAllowedSubOperators(addition, doubleFunctions);
185      SetAllowedSubOperators(average, doubleFunctions);
186      SetAllowedSubOperators(cosinus, doubleFunctions);
187      SetAllowedSubOperators(division, doubleFunctions);
188      SetAllowedSubOperators(exponential, doubleFunctions);
189      SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
190      SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
191      SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
192      SetAllowedSubOperators(logarithm, doubleFunctions);
193      SetAllowedSubOperators(multiplication, doubleFunctions);
194      SetAllowedSubOperators(power, doubleFunctions);
195      SetAllowedSubOperators(signum, doubleFunctions);
196      SetAllowedSubOperators(sinus, doubleFunctions);
197      SetAllowedSubOperators(sqrt, doubleFunctions);
198      SetAllowedSubOperators(subtraction, doubleFunctions);
199      SetAllowedSubOperators(tangens, doubleFunctions);
200
201      operatorLibrary = new GPOperatorLibrary();
202      ConditionalAddOperator(DIFFERENTIALS_ALLOWED, operatorLibrary, differential);
203      ConditionalAddOperator(VARIABLES_ALLOWED, operatorLibrary, variable);
204      ConditionalAddOperator(CONSTANTS_ALLOWED, operatorLibrary, constant);
205      ConditionalAddOperator(ADDITION_ALLOWED, operatorLibrary, addition);
206      ConditionalAddOperator(AVERAGE_ALLOWED, operatorLibrary, average);
207      ConditionalAddOperator(AND_ALLOWED, operatorLibrary, and);
208      ConditionalAddOperator(COSINUS_ALLOWED, operatorLibrary, cosinus);
209      ConditionalAddOperator(DIVISION_ALLOWED, operatorLibrary, division);
210      ConditionalAddOperator(EQUAL_ALLOWED, operatorLibrary, equal);
211      ConditionalAddOperator(EXPONENTIAL_ALLOWED, operatorLibrary, exponential);
212      ConditionalAddOperator(GREATERTHAN_ALLOWED, operatorLibrary, greaterThan);
213      ConditionalAddOperator(IFTHENELSE_ALLOWED, operatorLibrary, ifThenElse);
214      ConditionalAddOperator(LESSTHAN_ALLOWED, operatorLibrary, lessThan);
215      ConditionalAddOperator(LOGARTIHM_ALLOWED, operatorLibrary, logarithm);
216      ConditionalAddOperator(MULTIPLICATION_ALLOWED, operatorLibrary, multiplication);
217      ConditionalAddOperator(NOT_ALLOWED, operatorLibrary, not);
218      ConditionalAddOperator(POWER_ALLOWED, operatorLibrary, power);
219      ConditionalAddOperator(OR_ALLOWED, operatorLibrary, or);
220      ConditionalAddOperator(SIGNUM_ALLOWED, operatorLibrary, signum);
221      ConditionalAddOperator(SINUS_ALLOWED, operatorLibrary, sinus);
222      ConditionalAddOperator(SQRT_ALLOWED, operatorLibrary, sqrt);
223      ConditionalAddOperator(SUBTRACTION_ALLOWED, operatorLibrary, subtraction);
224      ConditionalAddOperator(TANGENS_ALLOWED, operatorLibrary, tangens);
225      ConditionalAddOperator(XOR_ALLOWED, operatorLibrary, xor);
226
227      variable.SetConstraints(1, nInputVariables, minTimeOffset, maxTimeOffset);
228      differential.SetConstraints(1, nInputVariables, minTimeOffset, maxTimeOffset);
229
230      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), operatorLibrary));
231
232      return null;
233    }
234
235    private void ConditionalAddOperator(string condName, IOperator op, List<IOperator> list) {
236      if (GetVariableValue<BoolData>(condName, null, false).Data) list.Add(op);
237    }
238
239    private void ConditionalAddOperator(string condName, GPOperatorLibrary operatorLibrary, IOperator op) {
240      if (GetVariableValue<BoolData>(condName, null, false).Data) operatorLibrary.GPOperatorGroup.AddOperator(op);
241    }
242
243    private void SetAllowedSubOperators(IFunction f, List<IOperator> gs) {
244      foreach (IConstraint c in f.Constraints) {
245        if (c is SubOperatorTypeConstraint) {
246          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
247          typeConstraint.Clear();
248          foreach (IOperator g in gs) {
249            typeConstraint.AddOperator(g);
250          }
251        } else if (c is AllSubOperatorsTypeConstraint) {
252          AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
253          typeConstraint.Clear();
254          foreach (IOperator g in gs) {
255            typeConstraint.AddOperator(g);
256          }
257        }
258      }
259    }
260
261    private void SetAllowedSubOperators(IFunction f, int p, List<IOperator> gs) {
262      foreach (IConstraint c in f.Constraints) {
263        if (c is SubOperatorTypeConstraint) {
264          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
265          if (typeConstraint.SubOperatorIndex.Data == p) {
266            typeConstraint.Clear();
267            foreach (IOperator g in gs) {
268              typeConstraint.AddOperator(g);
269            }
270          }
271        }
272      }
273    }
274  }
275}
Note: See TracBrowser for help on using the repository browser.