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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.DataAnalysis;
|
---|
29 | using HeuristicLab.Constraints;
|
---|
30 | using StructId = HeuristicLab.GP.StructureIdentification;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
33 | public class FunctionLibraryInjector : OperatorBase {
|
---|
34 | private const string FUNCTIONLIBRARY = "FunctionLibrary";
|
---|
35 | private const string TARGETVARIABLE = "TargetVariable";
|
---|
36 | private const string ALLOWEDFEATURES = "AllowedFeatures";
|
---|
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(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In));
|
---|
73 | AddVariableInfo(new VariableInfo(ALLOWEDFEATURES, "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In));
|
---|
74 | AddVariableInfo(new Core.VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), Core.VariableKind.In));
|
---|
75 | AddVariableInfo(new Core.VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), Core.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 |
|
---|
114 | ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>(ALLOWEDFEATURES, scope, true);
|
---|
115 | int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
|
---|
116 |
|
---|
117 | // try to get minTimeOffset (use 0 as default if not available)
|
---|
118 | IItem minTimeOffsetItem = GetVariableValue(MINTIMEOFFSET, scope, true, false);
|
---|
119 | int minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;
|
---|
120 | // try to get maxTimeOffset (use 0 as default if not available)
|
---|
121 | IItem maxTimeOffsetItem = GetVariableValue(MAXTIMEOFFSET, scope, true, false);
|
---|
122 | int maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data;
|
---|
123 |
|
---|
124 | variable = new StructId.Variable();
|
---|
125 | StructId.Constant constant = new StructId.Constant();
|
---|
126 | StructId.Differential differential = new Differential();
|
---|
127 | StructId.Addition addition = new StructId.Addition();
|
---|
128 | StructId.And and = new StructId.And();
|
---|
129 | StructId.Average average = new StructId.Average();
|
---|
130 | StructId.Cosinus cosinus = new StructId.Cosinus();
|
---|
131 | StructId.Division division = new StructId.Division();
|
---|
132 | StructId.Equal equal = new StructId.Equal();
|
---|
133 | StructId.Exponential exponential = new StructId.Exponential();
|
---|
134 | StructId.GreaterThan greaterThan = new StructId.GreaterThan();
|
---|
135 | StructId.IfThenElse ifThenElse = new StructId.IfThenElse();
|
---|
136 | StructId.LessThan lessThan = new StructId.LessThan();
|
---|
137 | StructId.Logarithm logarithm = new StructId.Logarithm();
|
---|
138 | StructId.Multiplication multiplication = new StructId.Multiplication();
|
---|
139 | StructId.Not not = new StructId.Not();
|
---|
140 | StructId.Or or = new StructId.Or();
|
---|
141 | StructId.Power power = new StructId.Power();
|
---|
142 | StructId.Signum signum = new StructId.Signum();
|
---|
143 | StructId.Sinus sinus = new StructId.Sinus();
|
---|
144 | StructId.Sqrt sqrt = new StructId.Sqrt();
|
---|
145 | StructId.Subtraction subtraction = new StructId.Subtraction();
|
---|
146 | StructId.Tangens tangens = new StructId.Tangens();
|
---|
147 | StructId.Xor xor = new StructId.Xor();
|
---|
148 |
|
---|
149 |
|
---|
150 | List<IOperator> booleanFunctions = new List<IOperator>();
|
---|
151 | ConditionalAddOperator(AND_ALLOWED, and, booleanFunctions);
|
---|
152 | ConditionalAddOperator(EQUAL_ALLOWED, equal, booleanFunctions);
|
---|
153 | ConditionalAddOperator(GREATERTHAN_ALLOWED, greaterThan, booleanFunctions);
|
---|
154 | ConditionalAddOperator(LESSTHAN_ALLOWED, lessThan, booleanFunctions);
|
---|
155 | ConditionalAddOperator(NOT_ALLOWED, not, booleanFunctions);
|
---|
156 | ConditionalAddOperator(OR_ALLOWED, or, booleanFunctions);
|
---|
157 | ConditionalAddOperator(XOR_ALLOWED, xor, booleanFunctions);
|
---|
158 |
|
---|
159 | List<IOperator> doubleFunctions = new List<IOperator>();
|
---|
160 | ConditionalAddOperator(DIFFERENTIALS_ALLOWED, differential, doubleFunctions);
|
---|
161 | ConditionalAddOperator(VARIABLES_ALLOWED, variable, doubleFunctions);
|
---|
162 | ConditionalAddOperator(CONSTANTS_ALLOWED, constant, doubleFunctions);
|
---|
163 | ConditionalAddOperator(ADDITION_ALLOWED, addition, doubleFunctions);
|
---|
164 | ConditionalAddOperator(AVERAGE_ALLOWED, average, doubleFunctions);
|
---|
165 | ConditionalAddOperator(COSINUS_ALLOWED, cosinus, doubleFunctions);
|
---|
166 | ConditionalAddOperator(DIVISION_ALLOWED, division, doubleFunctions);
|
---|
167 | ConditionalAddOperator(EXPONENTIAL_ALLOWED, exponential, doubleFunctions);
|
---|
168 | ConditionalAddOperator(IFTHENELSE_ALLOWED, ifThenElse, doubleFunctions);
|
---|
169 | ConditionalAddOperator(LOGARTIHM_ALLOWED, logarithm, doubleFunctions);
|
---|
170 | ConditionalAddOperator(MULTIPLICATION_ALLOWED, multiplication, doubleFunctions);
|
---|
171 | ConditionalAddOperator(POWER_ALLOWED, power, doubleFunctions);
|
---|
172 | ConditionalAddOperator(SIGNUM_ALLOWED, signum, doubleFunctions);
|
---|
173 | ConditionalAddOperator(SINUS_ALLOWED, sinus, doubleFunctions);
|
---|
174 | ConditionalAddOperator(SQRT_ALLOWED, sqrt, doubleFunctions);
|
---|
175 | ConditionalAddOperator(SUBTRACTION_ALLOWED, subtraction, doubleFunctions);
|
---|
176 | ConditionalAddOperator(TANGENS_ALLOWED, tangens, doubleFunctions);
|
---|
177 |
|
---|
178 | SetAllowedSubOperators(and, booleanFunctions);
|
---|
179 | SetAllowedSubOperators(equal, doubleFunctions);
|
---|
180 | SetAllowedSubOperators(greaterThan, doubleFunctions);
|
---|
181 | SetAllowedSubOperators(lessThan, doubleFunctions);
|
---|
182 | SetAllowedSubOperators(not, booleanFunctions);
|
---|
183 | SetAllowedSubOperators(or, booleanFunctions);
|
---|
184 | SetAllowedSubOperators(xor, booleanFunctions);
|
---|
185 | SetAllowedSubOperators(addition, doubleFunctions);
|
---|
186 | SetAllowedSubOperators(average, doubleFunctions);
|
---|
187 | SetAllowedSubOperators(cosinus, doubleFunctions);
|
---|
188 | SetAllowedSubOperators(division, doubleFunctions);
|
---|
189 | SetAllowedSubOperators(exponential, doubleFunctions);
|
---|
190 | SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
|
---|
191 | SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
|
---|
192 | SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
|
---|
193 | SetAllowedSubOperators(logarithm, doubleFunctions);
|
---|
194 | SetAllowedSubOperators(multiplication, doubleFunctions);
|
---|
195 | SetAllowedSubOperators(power, doubleFunctions);
|
---|
196 | SetAllowedSubOperators(signum, doubleFunctions);
|
---|
197 | SetAllowedSubOperators(sinus, doubleFunctions);
|
---|
198 | SetAllowedSubOperators(sqrt, doubleFunctions);
|
---|
199 | SetAllowedSubOperators(subtraction, doubleFunctions);
|
---|
200 | SetAllowedSubOperators(tangens, doubleFunctions);
|
---|
201 |
|
---|
202 | operatorLibrary = new GPOperatorLibrary();
|
---|
203 | ConditionalAddOperator(DIFFERENTIALS_ALLOWED, operatorLibrary, differential);
|
---|
204 | ConditionalAddOperator(VARIABLES_ALLOWED, operatorLibrary, variable);
|
---|
205 | ConditionalAddOperator(CONSTANTS_ALLOWED, operatorLibrary, constant);
|
---|
206 | ConditionalAddOperator(ADDITION_ALLOWED, operatorLibrary, addition);
|
---|
207 | ConditionalAddOperator(AVERAGE_ALLOWED, operatorLibrary, average);
|
---|
208 | ConditionalAddOperator(AND_ALLOWED, operatorLibrary, and);
|
---|
209 | ConditionalAddOperator(COSINUS_ALLOWED, operatorLibrary, cosinus);
|
---|
210 | ConditionalAddOperator(DIVISION_ALLOWED, operatorLibrary, division);
|
---|
211 | ConditionalAddOperator(EQUAL_ALLOWED, operatorLibrary, equal);
|
---|
212 | ConditionalAddOperator(EXPONENTIAL_ALLOWED, operatorLibrary, exponential);
|
---|
213 | ConditionalAddOperator(GREATERTHAN_ALLOWED, operatorLibrary, greaterThan);
|
---|
214 | ConditionalAddOperator(IFTHENELSE_ALLOWED, operatorLibrary, ifThenElse);
|
---|
215 | ConditionalAddOperator(LESSTHAN_ALLOWED, operatorLibrary, lessThan);
|
---|
216 | ConditionalAddOperator(LOGARTIHM_ALLOWED, operatorLibrary, logarithm);
|
---|
217 | ConditionalAddOperator(MULTIPLICATION_ALLOWED, operatorLibrary, multiplication);
|
---|
218 | ConditionalAddOperator(NOT_ALLOWED, operatorLibrary, not);
|
---|
219 | ConditionalAddOperator(POWER_ALLOWED, operatorLibrary, power);
|
---|
220 | ConditionalAddOperator(OR_ALLOWED, operatorLibrary, or);
|
---|
221 | ConditionalAddOperator(SIGNUM_ALLOWED, operatorLibrary, signum);
|
---|
222 | ConditionalAddOperator(SINUS_ALLOWED, operatorLibrary, sinus);
|
---|
223 | ConditionalAddOperator(SQRT_ALLOWED, operatorLibrary, sqrt);
|
---|
224 | ConditionalAddOperator(SUBTRACTION_ALLOWED, operatorLibrary, subtraction);
|
---|
225 | ConditionalAddOperator(TANGENS_ALLOWED, operatorLibrary, tangens);
|
---|
226 | ConditionalAddOperator(XOR_ALLOWED, operatorLibrary, xor);
|
---|
227 |
|
---|
228 | int[] allowedIndexes = new int[allowedFeatures.Count];
|
---|
229 | for (int i = 0; i < allowedIndexes.Length; i++) {
|
---|
230 | allowedIndexes[i] = allowedFeatures[i].Data;
|
---|
231 | }
|
---|
232 |
|
---|
233 | variable.SetConstraints(allowedIndexes, minTimeOffset, maxTimeOffset);
|
---|
234 | differential.SetConstraints(allowedIndexes, minTimeOffset, maxTimeOffset);
|
---|
235 |
|
---|
236 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), operatorLibrary));
|
---|
237 |
|
---|
238 | return null;
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void ConditionalAddOperator(string condName, IOperator op, List<IOperator> list) {
|
---|
242 | if (GetVariableValue<BoolData>(condName, null, false).Data) list.Add(op);
|
---|
243 | }
|
---|
244 |
|
---|
245 | private void ConditionalAddOperator(string condName, GPOperatorLibrary operatorLibrary, IOperator op) {
|
---|
246 | if (GetVariableValue<BoolData>(condName, null, false).Data) operatorLibrary.GPOperatorGroup.AddOperator(op);
|
---|
247 | }
|
---|
248 |
|
---|
249 | private void SetAllowedSubOperators(IFunction f, List<IOperator> gs) {
|
---|
250 | foreach (IConstraint c in f.Constraints) {
|
---|
251 | if (c is SubOperatorTypeConstraint) {
|
---|
252 | SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
|
---|
253 | typeConstraint.Clear();
|
---|
254 | foreach (IOperator g in gs) {
|
---|
255 | typeConstraint.AddOperator(g);
|
---|
256 | }
|
---|
257 | } else if (c is AllSubOperatorsTypeConstraint) {
|
---|
258 | AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
|
---|
259 | typeConstraint.Clear();
|
---|
260 | foreach (IOperator g in gs) {
|
---|
261 | typeConstraint.AddOperator(g);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | private void SetAllowedSubOperators(IFunction f, int p, List<IOperator> gs) {
|
---|
268 | foreach (IConstraint c in f.Constraints) {
|
---|
269 | if (c is SubOperatorTypeConstraint) {
|
---|
270 | SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
|
---|
271 | if (typeConstraint.SubOperatorIndex.Data == p) {
|
---|
272 | typeConstraint.Clear();
|
---|
273 | foreach (IOperator g in gs) {
|
---|
274 | typeConstraint.AddOperator(g);
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|