1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 System.Linq;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
31 | [StorableType("7EC7B4A7-0E27-4011-B983-B0E15A6944EC")]
|
---|
32 | [Item("TypeCoherentVectorExpressionGrammar", "Represents a grammar for functional expressions in which special syntactic constraints are enforced so that vector and scalar expressions are not mixed.")]
|
---|
33 | public class TypeCoherentVectorExpressionGrammar : DataAnalysisGrammar, ISymbolicDataAnalysisGrammar {
|
---|
34 | private const string ArithmeticFunctionsName = "Arithmetic Functions";
|
---|
35 | private const string TrigonometricFunctionsName = "Trigonometric Functions";
|
---|
36 | private const string ExponentialFunctionsName = "Exponential and Logarithmic Functions";
|
---|
37 | private const string PowerFunctionsName = "Power Functions";
|
---|
38 | private const string TerminalsName = "Terminals";
|
---|
39 | private const string VectorAggregationName = "Aggregations";
|
---|
40 | private const string VectorStatisticsName = "Vector Statistics";
|
---|
41 | private const string VectorDistancesName = "Vector Distances";
|
---|
42 | private const string ScalarSymbolsName = "Scalar Symbols";
|
---|
43 |
|
---|
44 | private const string VectorArithmeticFunctionsName = "Vector Arithmetic Functions";
|
---|
45 | private const string VectorTrigonometricFunctionsName = "Vector Trigonometric Functions";
|
---|
46 | private const string VectorExponentialFunctionsName = "Vector Exponential and Logarithmic Functions";
|
---|
47 | private const string VectorPowerFunctionsName = "Vector Power Functions";
|
---|
48 | private const string VectorTerminalsName = "Vector Terminals";
|
---|
49 | private const string VectorSymbolsName = "Vector Symbols";
|
---|
50 |
|
---|
51 | private const string VectorManipulationSymbolsName = "Vector Manipulation Symbols";
|
---|
52 |
|
---|
53 | private const string RealValuedSymbolsName = "Real Valued Symbols";
|
---|
54 |
|
---|
55 | [StorableConstructor]
|
---|
56 | protected TypeCoherentVectorExpressionGrammar(StorableConstructorFlag _) : base(_) { }
|
---|
57 | protected TypeCoherentVectorExpressionGrammar(TypeCoherentVectorExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
58 | public TypeCoherentVectorExpressionGrammar()
|
---|
59 | : base(ItemAttribute.GetName(typeof(TypeCoherentVectorExpressionGrammar)), ItemAttribute.GetDescription(typeof(TypeCoherentVectorExpressionGrammar))) {
|
---|
60 | Initialize();
|
---|
61 | }
|
---|
62 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
63 | return new TypeCoherentVectorExpressionGrammar(this, cloner);
|
---|
64 | }
|
---|
65 |
|
---|
66 | private void Initialize() {
|
---|
67 | #region scalar symbol declaration
|
---|
68 | var add = new Addition();
|
---|
69 | var sub = new Subtraction();
|
---|
70 | var mul = new Multiplication();
|
---|
71 | var div = new Division();
|
---|
72 |
|
---|
73 | var sin = new Sine();
|
---|
74 | var cos = new Cosine();
|
---|
75 | var tan = new Tangent();
|
---|
76 |
|
---|
77 | var exp = new Exponential();
|
---|
78 | var log = new Logarithm();
|
---|
79 |
|
---|
80 | var square = new Square();
|
---|
81 | var sqrt = new SquareRoot();
|
---|
82 | var cube = new Cube();
|
---|
83 | var cubeRoot = new CubeRoot();
|
---|
84 | var power = new Power();
|
---|
85 | var root = new Root();
|
---|
86 |
|
---|
87 | var constant = new Constant { MinValue = -20, MaxValue = 20 };
|
---|
88 | var variable = new Variable();
|
---|
89 | var binFactorVariable = new BinaryFactorVariable();
|
---|
90 | var factorVariable = new FactorVariable();
|
---|
91 |
|
---|
92 | var mean = new Mean();
|
---|
93 | var sd = new StandardDeviation();
|
---|
94 | var sum = new Sum();
|
---|
95 | var length = new Length() { Enabled = false };
|
---|
96 | var min = new Min() { Enabled = false };
|
---|
97 | var max = new Max() { Enabled = false };
|
---|
98 | var variance = new Variance() { Enabled = false };
|
---|
99 | var skewness = new Skewness() { Enabled = false };
|
---|
100 | var kurtosis = new Kurtosis() { Enabled = false };
|
---|
101 | var euclideanDistance = new EuclideanDistance() { Enabled = false };
|
---|
102 | var covariance = new Covariance() { Enabled = false };
|
---|
103 | #endregion
|
---|
104 |
|
---|
105 | #region vector symbol declaration
|
---|
106 | var vectoradd = new Addition() { Name = "Vector Addition" };
|
---|
107 | var vectorsub = new Subtraction() { Name = "Vector Subtraction" };
|
---|
108 | var vectormul = new Multiplication() { Name = "Vector Multiplication" };
|
---|
109 | var vectordiv = new Division() { Name = "Vector Division" };
|
---|
110 |
|
---|
111 | var vectorsin = new Sine() { Name = "Vector Sine" };
|
---|
112 | var vectorcos = new Cosine() { Name = "Vector Cosine" };
|
---|
113 | var vectortan = new Tangent() { Name = "Vector Tangent" };
|
---|
114 |
|
---|
115 | var vectorexp = new Exponential() { Name = "Vector Exponential" };
|
---|
116 | var vectorlog = new Logarithm() { Name = "Vector Logarithm" };
|
---|
117 |
|
---|
118 | var vectorsquare = new Square() { Name = "Vector Square" };
|
---|
119 | var vectorsqrt = new SquareRoot() { Name = "Vector SquareRoot" };
|
---|
120 | var vectorcube = new Cube() { Name = "Vector Cube" };
|
---|
121 | var vectorcubeRoot = new CubeRoot() { Name = "Vector CubeRoot" };
|
---|
122 | var vectorpower = new Power() { Name = "Vector Power" };
|
---|
123 | var vectorroot = new Root() { Name = "Vector Root" };
|
---|
124 |
|
---|
125 | var vectorvariable = new Variable() { Name = "Vector Variable" };
|
---|
126 | #endregion
|
---|
127 |
|
---|
128 | #region vector manipulation symbol declaration
|
---|
129 | var subvector = new SubVector() { EnableWindowing = true };
|
---|
130 | #endregion
|
---|
131 |
|
---|
132 | #region group symbol declaration
|
---|
133 | var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul, div });
|
---|
134 | var trigonometricSymbols = new GroupSymbol(TrigonometricFunctionsName, new List<ISymbol>() { sin, cos, tan });
|
---|
135 | var exponentialAndLogarithmicSymbols = new GroupSymbol(ExponentialFunctionsName, new List<ISymbol> { exp, log });
|
---|
136 | var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, sqrt, cube, cubeRoot, power, root });
|
---|
137 | var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variable, binFactorVariable, factorVariable });
|
---|
138 | var statisticsSymbols = new GroupSymbol(VectorStatisticsName, new List<ISymbol> { mean, sd, sum, length, min, max, variance, skewness, kurtosis });
|
---|
139 | var distancesSymbols = new GroupSymbol(VectorDistancesName, new List<ISymbol> { euclideanDistance, covariance });
|
---|
140 | var aggregationSymbols = new GroupSymbol(VectorAggregationName, new List<ISymbol> { statisticsSymbols, distancesSymbols });
|
---|
141 | var scalarSymbols = new GroupSymbol(ScalarSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, powerSymbols, terminalSymbols, aggregationSymbols });
|
---|
142 |
|
---|
143 | var vectorarithmeticSymbols = new GroupSymbol(VectorArithmeticFunctionsName, new List<ISymbol>() { vectoradd, vectorsub, vectormul, vectordiv });
|
---|
144 | var vectortrigonometricSymbols = new GroupSymbol(VectorTrigonometricFunctionsName, new List<ISymbol>() { vectorsin, vectorcos, vectortan });
|
---|
145 | var vectorexponentialAndLogarithmicSymbols = new GroupSymbol(VectorExponentialFunctionsName, new List<ISymbol> { vectorexp, vectorlog });
|
---|
146 | var vectorpowerSymbols = new GroupSymbol(VectorPowerFunctionsName, new List<ISymbol> { vectorsquare, vectorsqrt, vectorcube, vectorcubeRoot, vectorpower, vectorroot });
|
---|
147 | var vectorterminalSymbols = new GroupSymbol(VectorTerminalsName, new List<ISymbol> { vectorvariable });
|
---|
148 | var vectorSymbols = new GroupSymbol(VectorSymbolsName, new List<ISymbol>() { vectorarithmeticSymbols, vectortrigonometricSymbols, vectorexponentialAndLogarithmicSymbols, vectorpowerSymbols, vectorterminalSymbols });
|
---|
149 |
|
---|
150 | var vectorManipulationSymbols = new GroupSymbol(VectorManipulationSymbolsName, new List<ISymbol>() { subvector });
|
---|
151 |
|
---|
152 | //var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol> { scalarSymbols, vectorSymbols });
|
---|
153 | #endregion
|
---|
154 |
|
---|
155 | //AddSymbol(realValuedSymbols);
|
---|
156 | AddSymbol(scalarSymbols);
|
---|
157 | AddSymbol(vectorSymbols);
|
---|
158 | AddSymbol(vectorManipulationSymbols);
|
---|
159 |
|
---|
160 | #region subtree count configuration
|
---|
161 | SetSubtreeCount(arithmeticSymbols, 2, 2);
|
---|
162 | SetSubtreeCount(trigonometricSymbols, 1, 1);
|
---|
163 | SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
|
---|
164 | SetSubtreeCount(square, 1, 1);
|
---|
165 | SetSubtreeCount(sqrt, 1, 1);
|
---|
166 | SetSubtreeCount(cube, 1, 1);
|
---|
167 | SetSubtreeCount(cubeRoot, 1, 1);
|
---|
168 | SetSubtreeCount(power, 2, 2);
|
---|
169 | SetSubtreeCount(root, 2, 2);
|
---|
170 | SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
|
---|
171 | SetSubtreeCount(terminalSymbols, 0, 0);
|
---|
172 | SetSubtreeCount(statisticsSymbols, 1, 1);
|
---|
173 | SetSubtreeCount(distancesSymbols, 2, 2);
|
---|
174 |
|
---|
175 | SetSubtreeCount(vectorarithmeticSymbols, 2, 2);
|
---|
176 | SetSubtreeCount(vectortrigonometricSymbols, 1, 1);
|
---|
177 | SetSubtreeCount(vectorexponentialAndLogarithmicSymbols, 1, 1);
|
---|
178 | SetSubtreeCount(vectorsquare, 1, 1);
|
---|
179 | SetSubtreeCount(vectorsqrt, 1, 1);
|
---|
180 | SetSubtreeCount(vectorcube, 1, 1);
|
---|
181 | SetSubtreeCount(vectorcubeRoot, 1, 1);
|
---|
182 | SetSubtreeCount(vectorpower, 2, 2);
|
---|
183 | SetSubtreeCount(vectorroot, 2, 2);
|
---|
184 | SetSubtreeCount(vectorexponentialAndLogarithmicSymbols, 1, 1);
|
---|
185 | SetSubtreeCount(vectorterminalSymbols, 0, 0);
|
---|
186 |
|
---|
187 | SetSubtreeCount(subvector, 1, 1);
|
---|
188 | #endregion
|
---|
189 |
|
---|
190 | #region allowed child symbols configuration
|
---|
191 | AddAllowedChildSymbol(StartSymbol, scalarSymbols);
|
---|
192 |
|
---|
193 | AddAllowedChildSymbol(arithmeticSymbols, scalarSymbols);
|
---|
194 | AddAllowedChildSymbol(trigonometricSymbols, scalarSymbols);
|
---|
195 | AddAllowedChildSymbol(exponentialAndLogarithmicSymbols, scalarSymbols);
|
---|
196 | AddAllowedChildSymbol(powerSymbols, scalarSymbols, 0);
|
---|
197 | AddAllowedChildSymbol(power, constant, 1);
|
---|
198 | AddAllowedChildSymbol(root, constant, 1);
|
---|
199 | AddAllowedChildSymbol(aggregationSymbols, vectorSymbols);
|
---|
200 | AddAllowedChildSymbol(statisticsSymbols, subvector);
|
---|
201 |
|
---|
202 | AddAllowedChildSymbol(vectorarithmeticSymbols, vectorSymbols);
|
---|
203 | AddAllowedChildSymbol(vectorarithmeticSymbols, scalarSymbols);
|
---|
204 | AddAllowedChildSymbol(vectortrigonometricSymbols, vectorSymbols);
|
---|
205 | AddAllowedChildSymbol(vectorexponentialAndLogarithmicSymbols, vectorSymbols);
|
---|
206 | AddAllowedChildSymbol(vectorpowerSymbols, vectorSymbols, 0);
|
---|
207 | AddAllowedChildSymbol(vectorpower, constant, 1);
|
---|
208 | AddAllowedChildSymbol(vectorroot, constant, 1);
|
---|
209 |
|
---|
210 | AddAllowedChildSymbol(subvector, vectorSymbols);
|
---|
211 | #endregion
|
---|
212 |
|
---|
213 | #region default enabled/disabled
|
---|
214 | var disabledByDefault = new[] {
|
---|
215 | TrigonometricFunctionsName, ExponentialFunctionsName, PowerFunctionsName,
|
---|
216 | VectorTrigonometricFunctionsName, VectorExponentialFunctionsName, VectorPowerFunctionsName,
|
---|
217 | VectorManipulationSymbolsName
|
---|
218 | };
|
---|
219 | foreach (var grp in Symbols.Where(sym => disabledByDefault.Contains(sym.Name)))
|
---|
220 | grp.Enabled = false;
|
---|
221 | #endregion
|
---|
222 | }
|
---|
223 |
|
---|
224 | public override void ConfigureVariableSymbols(IDataAnalysisProblemData problemData) {
|
---|
225 | base.ConfigureVariableSymbols(problemData);
|
---|
226 |
|
---|
227 | var dataset = problemData.Dataset;
|
---|
228 | foreach (var varSymbol in Symbols.OfType<VariableBase>().Where(sym => sym.Name == "Variable")) {
|
---|
229 | if (!varSymbol.Fixed) {
|
---|
230 | varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => dataset.VariableHasType<double>(x));
|
---|
231 | varSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => dataset.VariableHasType<double>(x));
|
---|
232 | varSymbol.VariableDataType = typeof(double);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | foreach (var varSymbol in Symbols.OfType<VariableBase>().Where(sym => sym.Name == "Vector Variable")) {
|
---|
236 | if (!varSymbol.Fixed) {
|
---|
237 | varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => dataset.VariableHasType<DoubleVector>(x));
|
---|
238 | varSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => dataset.VariableHasType<DoubleVector>(x));
|
---|
239 | varSymbol.VariableDataType = typeof(DoubleVector);
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|