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