Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2395 was 2223, checked in by mkommend, 15 years ago

reintegrated branch new heuristic.modeling database backend (ticket #712)

File size: 11.5 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.GP.Interfaces;
26using HeuristicLab.GP;
27
28namespace HeuristicLab.GP.StructureIdentification {
29  public class FunctionLibraryInjector : FunctionLibraryInjectorBase {
30    private const string MINTIMEOFFSET = "MinTimeOffset";
31    private const string MAXTIMEOFFSET = "MaxTimeOffset";
32
33    private const string DIFFERENTIALS_ALLOWED = "Differentials";
34    private const string VARIABLES_ALLOWED = "Variables";
35    private const string CONSTANTS_ALLOWED = "Constants";
36    private const string ADDITION_ALLOWED = "Addition";
37    private const string AVERAGE_ALLOWED = "Average";
38    private const string AND_ALLOWED = "And";
39    private const string COSINUS_ALLOWED = "Cosinus";
40    private const string DIVISION_ALLOWED = "Division";
41    private const string EQUAL_ALLOWED = "Equal";
42    private const string EXPONENTIAL_ALLOWED = "Exponential";
43    private const string GREATERTHAN_ALLOWED = "GreaterThan";
44    private const string IFTHENELSE_ALLOWED = "IfThenElse";
45    private const string LESSTHAN_ALLOWED = "LessThan";
46    private const string LOGARTIHM_ALLOWED = "Logarithm";
47    private const string MULTIPLICATION_ALLOWED = "Multiplication";
48    private const string NOT_ALLOWED = "Not";
49    private const string POWER_ALLOWED = "Power";
50    private const string OR_ALLOWED = "Or";
51    private const string SIGNUM_ALLOWED = "Signum";
52    private const string SINUS_ALLOWED = "Sinus";
53    private const string SQRT_ALLOWED = "SquareRoot";
54    private const string SUBTRACTION_ALLOWED = "Subtraction";
55    private const string TANGENS_ALLOWED = "Tangens";
56    private const string XOR_ALLOWED = "Xor";
57
58    private int minTimeOffset;
59    private int maxTimeOffset;
60
61    public override string Description {
62      get { return @"Injects a configurable function library for regression and classification problems."; }
63    }
64
65    public FunctionLibraryInjector()
66      : base() {
67      AddVariableInfo(new VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), VariableKind.In));
68      AddVariableInfo(new VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), VariableKind.In));
69
70      AddVariable(DIFFERENTIALS_ALLOWED, false);
71      AddVariable(VARIABLES_ALLOWED, true);
72      AddVariable(CONSTANTS_ALLOWED, true);
73      AddVariable(ADDITION_ALLOWED, true);
74      AddVariable(AVERAGE_ALLOWED, false);
75      AddVariable(AND_ALLOWED, true);
76      AddVariable(COSINUS_ALLOWED, true);
77      AddVariable(DIVISION_ALLOWED, true);
78      AddVariable(EQUAL_ALLOWED, true);
79      AddVariable(EXPONENTIAL_ALLOWED, true);
80      AddVariable(GREATERTHAN_ALLOWED, true);
81      AddVariable(IFTHENELSE_ALLOWED, true);
82      AddVariable(LESSTHAN_ALLOWED, true);
83      AddVariable(LOGARTIHM_ALLOWED, true);
84      AddVariable(MULTIPLICATION_ALLOWED, true);
85      AddVariable(NOT_ALLOWED, true);
86      AddVariable(POWER_ALLOWED, true);
87      AddVariable(OR_ALLOWED, true);
88      AddVariable(SIGNUM_ALLOWED, true);
89      AddVariable(SINUS_ALLOWED, true);
90      AddVariable(SQRT_ALLOWED, true);
91      AddVariable(SUBTRACTION_ALLOWED, true);
92      AddVariable(TANGENS_ALLOWED, true);
93      AddVariable(XOR_ALLOWED, false);
94    }
95
96    private void AddVariable(string name, bool allowed) {
97      AddVariableInfo(new VariableInfo(name, name + " allowed", typeof(BoolData), Core.VariableKind.New));
98      GetVariableInfo(name).Local = true;
99      AddVariable(new Core.Variable(name, new BoolData(allowed)));
100    }
101
102    public override IOperation Apply(IScope scope) {
103      // try to get minTimeOffset (use 0 as default if not available)
104      IItem minTimeOffsetItem = GetVariableValue(MINTIMEOFFSET, scope, true, false);
105      minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;
106      // try to get maxTimeOffset (use 0 as default if not available)
107      IItem maxTimeOffsetItem = GetVariableValue(MAXTIMEOFFSET, scope, true, false);
108      maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data;
109
110      return base.Apply(scope);
111    }
112
113    protected override FunctionLibrary CreateFunctionLibrary() {
114      FunctionLibrary functionLibrary = new FunctionLibrary();
115
116      Variable variable = new Variable();
117      Constant constant = new Constant();
118      Differential differential = new Differential();
119      Addition addition = new Addition();
120      And and = new And();
121      Average average = new Average();
122      Cosinus cosinus = new Cosinus();
123      Division division = new Division();
124      Equal equal = new Equal();
125      Exponential exponential = new Exponential();
126      GreaterThan greaterThan = new GreaterThan();
127      IfThenElse ifThenElse = new IfThenElse();
128      LessThan lessThan = new LessThan();
129      Logarithm logarithm = new Logarithm();
130      Multiplication multiplication = new Multiplication();
131      Not not = new Not();
132      Or or = new Or();
133      Power power = new Power();
134      Signum signum = new Signum();
135      Sinus sinus = new Sinus();
136      Sqrt sqrt = new Sqrt();
137      Subtraction subtraction = new Subtraction();
138      Tangens tangens = new Tangens();
139      Xor xor = new Xor();
140
141
142      List<IFunction> booleanFunctions = new List<IFunction>();
143      ConditionalAddFunction(AND_ALLOWED, and, booleanFunctions);
144      ConditionalAddFunction(EQUAL_ALLOWED, equal, booleanFunctions);
145      ConditionalAddFunction(GREATERTHAN_ALLOWED, greaterThan, booleanFunctions);
146      ConditionalAddFunction(LESSTHAN_ALLOWED, lessThan, booleanFunctions);
147      ConditionalAddFunction(NOT_ALLOWED, not, booleanFunctions);
148      ConditionalAddFunction(OR_ALLOWED, or, booleanFunctions);
149      ConditionalAddFunction(XOR_ALLOWED, xor, booleanFunctions);
150
151      List<IFunction> doubleFunctions = new List<IFunction>();
152      ConditionalAddFunction(DIFFERENTIALS_ALLOWED, differential, doubleFunctions);
153      ConditionalAddFunction(VARIABLES_ALLOWED, variable, doubleFunctions);
154      ConditionalAddFunction(CONSTANTS_ALLOWED, constant, doubleFunctions);
155      ConditionalAddFunction(ADDITION_ALLOWED, addition, doubleFunctions);
156      ConditionalAddFunction(AVERAGE_ALLOWED, average, doubleFunctions);
157      ConditionalAddFunction(COSINUS_ALLOWED, cosinus, doubleFunctions);
158      ConditionalAddFunction(DIVISION_ALLOWED, division, doubleFunctions);
159      ConditionalAddFunction(EXPONENTIAL_ALLOWED, exponential, doubleFunctions);
160      ConditionalAddFunction(IFTHENELSE_ALLOWED, ifThenElse, doubleFunctions);
161      ConditionalAddFunction(LOGARTIHM_ALLOWED, logarithm, doubleFunctions);
162      ConditionalAddFunction(MULTIPLICATION_ALLOWED, multiplication, doubleFunctions);
163      ConditionalAddFunction(POWER_ALLOWED, power, doubleFunctions);
164      ConditionalAddFunction(SIGNUM_ALLOWED, signum, doubleFunctions);
165      ConditionalAddFunction(SINUS_ALLOWED, sinus, doubleFunctions);
166      ConditionalAddFunction(SQRT_ALLOWED, sqrt, doubleFunctions);
167      ConditionalAddFunction(SUBTRACTION_ALLOWED, subtraction, doubleFunctions);
168      ConditionalAddFunction(TANGENS_ALLOWED, tangens, doubleFunctions);
169
170      SetAllowedSubOperators(and, booleanFunctions);
171      SetAllowedSubOperators(equal, doubleFunctions);
172      SetAllowedSubOperators(greaterThan, doubleFunctions);
173      SetAllowedSubOperators(lessThan, doubleFunctions);
174      SetAllowedSubOperators(not, booleanFunctions);
175      SetAllowedSubOperators(or, booleanFunctions);
176      SetAllowedSubOperators(xor, booleanFunctions);
177      SetAllowedSubOperators(addition, doubleFunctions);
178      SetAllowedSubOperators(average, doubleFunctions);
179      SetAllowedSubOperators(cosinus, doubleFunctions);
180      SetAllowedSubOperators(division, doubleFunctions);
181      SetAllowedSubOperators(exponential, doubleFunctions);
182      SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
183      SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
184      SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
185      SetAllowedSubOperators(logarithm, doubleFunctions);
186      SetAllowedSubOperators(multiplication, doubleFunctions);
187      SetAllowedSubOperators(power, doubleFunctions);
188      SetAllowedSubOperators(signum, doubleFunctions);
189      SetAllowedSubOperators(sinus, doubleFunctions);
190      SetAllowedSubOperators(sqrt, doubleFunctions);
191      SetAllowedSubOperators(subtraction, doubleFunctions);
192      SetAllowedSubOperators(tangens, doubleFunctions);
193
194      ConditionalAddOperator(DIFFERENTIALS_ALLOWED, functionLibrary, differential);
195      ConditionalAddOperator(VARIABLES_ALLOWED, functionLibrary, variable);
196      ConditionalAddOperator(CONSTANTS_ALLOWED, functionLibrary, constant);
197      ConditionalAddOperator(ADDITION_ALLOWED, functionLibrary, addition);
198      ConditionalAddOperator(AVERAGE_ALLOWED, functionLibrary, average);
199      ConditionalAddOperator(AND_ALLOWED, functionLibrary, and);
200      ConditionalAddOperator(COSINUS_ALLOWED, functionLibrary, cosinus);
201      ConditionalAddOperator(DIVISION_ALLOWED, functionLibrary, division);
202      ConditionalAddOperator(EQUAL_ALLOWED, functionLibrary, equal);
203      ConditionalAddOperator(EXPONENTIAL_ALLOWED, functionLibrary, exponential);
204      ConditionalAddOperator(GREATERTHAN_ALLOWED, functionLibrary, greaterThan);
205      ConditionalAddOperator(IFTHENELSE_ALLOWED, functionLibrary, ifThenElse);
206      ConditionalAddOperator(LESSTHAN_ALLOWED, functionLibrary, lessThan);
207      ConditionalAddOperator(LOGARTIHM_ALLOWED, functionLibrary, logarithm);
208      ConditionalAddOperator(MULTIPLICATION_ALLOWED, functionLibrary, multiplication);
209      ConditionalAddOperator(NOT_ALLOWED, functionLibrary, not);
210      ConditionalAddOperator(POWER_ALLOWED, functionLibrary, power);
211      ConditionalAddOperator(OR_ALLOWED, functionLibrary, or);
212      ConditionalAddOperator(SIGNUM_ALLOWED, functionLibrary, signum);
213      ConditionalAddOperator(SINUS_ALLOWED, functionLibrary, sinus);
214      ConditionalAddOperator(SQRT_ALLOWED, functionLibrary, sqrt);
215      ConditionalAddOperator(SUBTRACTION_ALLOWED, functionLibrary, subtraction);
216      ConditionalAddOperator(TANGENS_ALLOWED, functionLibrary, tangens);
217      ConditionalAddOperator(XOR_ALLOWED, functionLibrary, xor);
218
219      variable.SetConstraints(minTimeOffset, maxTimeOffset);
220      differential.SetConstraints(minTimeOffset, maxTimeOffset);
221
222      return functionLibrary;
223    }
224
225    private void ConditionalAddFunction(string condName, IFunction fun, List<IFunction> list) {
226      if (GetVariableValue<BoolData>(condName, null, false).Data) list.Add(fun);
227    }
228
229    private void ConditionalAddOperator(string condName, FunctionLibrary functionLib, IFunction op) {
230      if (GetVariableValue<BoolData>(condName, null, false).Data) functionLib.AddFunction(op);
231    }
232  }
233}
Note: See TracBrowser for help on using the repository browser.