Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentExpressionGrammar.cs @ 17401

Last change on this file since 17401 was 17401, checked in by pfleck, 4 years ago

#3040 Added parser for new benchmark data but did not commit the data yet (too large)

File size: 15.3 KB
Line 
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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HEAL.Attic;
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
29  [StorableType("36A22322-0627-4E25-A468-F2A788AF6D46")]
30  [Item("TypeCoherentExpressionGrammar", "Represents a grammar for functional expressions in which special syntactic constraints are enforced so that boolean and real-valued expressions are not mixed.")]
31  public class TypeCoherentExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
32    private const string ArithmeticFunctionsName = "Arithmetic Functions";
33    private const string TrigonometricFunctionsName = "Trigonometric Functions";
34    private const string ExponentialFunctionsName = "Exponential and Logarithmic Functions";
35    private const string RealValuedSymbolsName = "Real Valued Symbols";
36    private const string TerminalsName = "Terminals";
37    private const string PowerFunctionsName = "Power Functions";
38    private const string ConditionsName = "Conditions";
39    private const string ComparisonsName = "Comparisons";
40    private const string BooleanOperatorsName = "Boolean Operators";
41    private const string ConditionalSymbolsName = "ConditionalSymbols";
42    private const string SpecialFunctionsName = "Special Functions";
43    private const string TimeSeriesSymbolsName = "Time Series Symbols";
44    private const string BasicVectorOperationSymbolsName = "Basic Vector Operations";
45    private const string VectorAggregationSymbolsName = "Vector Aggregation";
46    private const string VectorSymbolsName = "Vector Symbols";
47
48    [StorableConstructor]
49    protected TypeCoherentExpressionGrammar(StorableConstructorFlag _) : base(_) { }
50    protected TypeCoherentExpressionGrammar(TypeCoherentExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
51    public TypeCoherentExpressionGrammar()
52      : base(ItemAttribute.GetName(typeof(TypeCoherentExpressionGrammar)), ItemAttribute.GetDescription(typeof(TypeCoherentExpressionGrammar))) {
53      Initialize();
54    }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new TypeCoherentExpressionGrammar(this, cloner);
57    }
58
59    private void Initialize() {
60      #region symbol declaration
61      var add = new Addition();
62      var sub = new Subtraction();
63      var mul = new Multiplication();
64      var div = new Division();
65      var mean = new Average();
66      var sin = new Sine();
67      var cos = new Cosine();
68      var tan = new Tangent();
69      var log = new Logarithm();
70      var pow = new Power();
71      var square = new Square();
72      var root = new Root();
73      var sqrt = new SquareRoot();
74      var cube = new Cube();
75      var cubeRoot = new CubeRoot();
76      var exp = new Exponential();
77      var abs = new Absolute();
78
79      var airyA = new AiryA();
80      var airyB = new AiryB();
81      var bessel = new Bessel();
82      var cosineIntegral = new CosineIntegral();
83      var dawson = new Dawson();
84      var erf = new Erf();
85      var expIntegralEi = new ExponentialIntegralEi();
86      var fresnelCosineIntegral = new FresnelCosineIntegral();
87      var fresnelSineIntegral = new FresnelSineIntegral();
88      var gamma = new Gamma();
89      var hypCosineIntegral = new HyperbolicCosineIntegral();
90      var tanh = new HyperbolicTangent();
91      var hypSineIntegral = new HyperbolicSineIntegral();
92      var norm = new Norm();
93      var psi = new Psi();
94      var sineIntegral = new SineIntegral();
95      var analyticalQuotient = new AnalyticQuotient();
96
97      var @if = new IfThenElse();
98      var gt = new GreaterThan();
99      var lt = new LessThan();
100      var and = new And();
101      var or = new Or();
102      var not = new Not();
103      var xor = new Xor();
104      var variableCondition = new VariableCondition();
105
106      var timeLag = new TimeLag();
107      var integral = new Integral();
108      var derivative = new Derivative();
109
110      var constant = new Constant();
111      constant.MinValue = -20;
112      constant.MaxValue = 20;
113      var variableSymbol = new Variable();
114      var binFactorVariable = new BinaryFactorVariable();
115      var factorVariable = new FactorVariable();
116      var laggedVariable = new LaggedVariable();
117      var autoregressiveVariable = new AutoregressiveTargetVariable();
118
119      var vectorVariable = new VectorVariable();
120      var vectorAdd = new VectorAddition();
121      var vectorSub = new VectorSubtraction();
122      var vectorMul = new VectorMultiplication();
123      var vectorDiv = new VectorDivision();
124      var vectorSum = new VectorSum();
125      var vectorAvg = new VectorMean();
126      #endregion
127
128      #region group symbol declaration
129      var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul, div, mean });
130      var trigonometricSymbols = new GroupSymbol(TrigonometricFunctionsName, new List<ISymbol>() { sin, cos, tan, tanh });
131      var exponentialAndLogarithmicSymbols = new GroupSymbol(ExponentialFunctionsName, new List<ISymbol> { exp, log });
132      var specialFunctions = new GroupSymbol(SpecialFunctionsName, new List<ISymbol> { abs, airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi,
133        fresnelCosineIntegral,fresnelSineIntegral,gamma,hypCosineIntegral,hypSineIntegral,norm, psi, sineIntegral, analyticalQuotient});
134      var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol, binFactorVariable, factorVariable });
135      var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, specialFunctions, terminalSymbols });
136
137      var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, pow, sqrt, root, cube, cubeRoot });
138
139      var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition });
140      var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt });
141      var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not, xor });
142      var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols });
143
144      var timeSeriesSymbols = new GroupSymbol(TimeSeriesSymbolsName, new List<ISymbol> { timeLag, integral, derivative, laggedVariable, autoregressiveVariable });
145
146      var basicVectorOperationSymbols = new GroupSymbol(BasicVectorOperationSymbolsName, new List<ISymbol>() { vectorAdd, vectorSub, vectorMul, vectorDiv });
147      var vectorAggregationSymbols = new GroupSymbol(VectorAggregationSymbolsName, new List<ISymbol>() { vectorSum, vectorAvg });
148      var vectorSymbols = new GroupSymbol(VectorSymbolsName, new List<ISymbol>() { vectorVariable, basicVectorOperationSymbols, vectorAggregationSymbols });
149      #endregion
150
151      AddSymbol(realValuedSymbols);
152      AddSymbol(powerSymbols);
153      AddSymbol(conditionalSymbols);
154      AddSymbol(timeSeriesSymbols);
155      AddSymbol(vectorSymbols);
156
157      #region subtree count configuration
158      SetSubtreeCount(arithmeticSymbols, 2, 2);
159      SetSubtreeCount(trigonometricSymbols, 1, 1);
160      SetSubtreeCount(pow, 2, 2);
161      SetSubtreeCount(root, 2, 2);
162      SetSubtreeCount(square, 1, 1);
163      SetSubtreeCount(cube, 1, 1);
164      SetSubtreeCount(sqrt, 1, 1);
165      SetSubtreeCount(cubeRoot, 1, 1);
166      SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
167      foreach (var sy in specialFunctions.Symbols.Except(new[] { analyticalQuotient })) {
168        SetSubtreeCount(sy, 1, 1);
169      }
170      SetSubtreeCount(analyticalQuotient, 2, 2);
171
172      SetSubtreeCount(terminalSymbols, 0, 0);
173
174      SetSubtreeCount(@if, 3, 3);
175      SetSubtreeCount(variableCondition, 2, 2);
176      SetSubtreeCount(comparisonSymbols, 2, 2);
177      SetSubtreeCount(and, 2, 2);
178      SetSubtreeCount(or, 2, 2);
179      SetSubtreeCount(not, 1, 1);
180      SetSubtreeCount(xor, 2, 2);
181
182      SetSubtreeCount(timeLag, 1, 1);
183      SetSubtreeCount(integral, 1, 1);
184      SetSubtreeCount(derivative, 1, 1);
185      SetSubtreeCount(laggedVariable, 0, 0);
186      SetSubtreeCount(autoregressiveVariable, 0, 0);
187
188
189      SetSubtreeCount(vectorVariable, 0, 0);
190      SetSubtreeCount(basicVectorOperationSymbols, 2, 2);
191      SetSubtreeCount(vectorAggregationSymbols, 1, 1);
192      #endregion
193
194      #region allowed child symbols configuration
195      AddAllowedChildSymbol(StartSymbol, realValuedSymbols);
196      AddAllowedChildSymbol(StartSymbol, powerSymbols);
197      AddAllowedChildSymbol(StartSymbol, conditionSymbols);
198      AddAllowedChildSymbol(StartSymbol, timeSeriesSymbols);
199      AddAllowedChildSymbol(StartSymbol, specialFunctions);
200
201      AddAllowedChildSymbol(DefunSymbol, realValuedSymbols);
202      AddAllowedChildSymbol(DefunSymbol, powerSymbols);
203      AddAllowedChildSymbol(DefunSymbol, conditionSymbols);
204      AddAllowedChildSymbol(DefunSymbol, timeSeriesSymbols);
205      AddAllowedChildSymbol(DefunSymbol, specialFunctions);
206
207      AddAllowedChildSymbol(realValuedSymbols, realValuedSymbols);
208      AddAllowedChildSymbol(realValuedSymbols, powerSymbols);
209      AddAllowedChildSymbol(realValuedSymbols, conditionSymbols);
210      AddAllowedChildSymbol(realValuedSymbols, timeSeriesSymbols);
211      AddAllowedChildSymbol(realValuedSymbols, specialFunctions);
212
213      AddAllowedChildSymbol(powerSymbols, variableSymbol, 0);
214      AddAllowedChildSymbol(powerSymbols, laggedVariable, 0);
215      AddAllowedChildSymbol(powerSymbols, autoregressiveVariable, 0);
216      AddAllowedChildSymbol(powerSymbols, constant, 1);
217
218      AddAllowedChildSymbol(square, realValuedSymbols, 0);
219      AddAllowedChildSymbol(square, conditionSymbols, 0);
220      AddAllowedChildSymbol(square, timeSeriesSymbols, 0);
221
222      AddAllowedChildSymbol(sqrt, realValuedSymbols, 0);
223      AddAllowedChildSymbol(sqrt, conditionSymbols, 0);
224      AddAllowedChildSymbol(sqrt, timeSeriesSymbols, 0);
225
226      AddAllowedChildSymbol(@if, comparisonSymbols, 0);
227      AddAllowedChildSymbol(@if, booleanOperationSymbols, 0);
228      AddAllowedChildSymbol(@if, conditionSymbols, 1);
229      AddAllowedChildSymbol(@if, realValuedSymbols, 1);
230      AddAllowedChildSymbol(@if, powerSymbols, 1);
231      AddAllowedChildSymbol(@if, timeSeriesSymbols, 1);
232      AddAllowedChildSymbol(@if, conditionSymbols, 2);
233      AddAllowedChildSymbol(@if, realValuedSymbols, 2);
234      AddAllowedChildSymbol(@if, powerSymbols, 2);
235      AddAllowedChildSymbol(@if, timeSeriesSymbols, 2);
236
237      AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
238      AddAllowedChildSymbol(comparisonSymbols, realValuedSymbols);
239      AddAllowedChildSymbol(comparisonSymbols, powerSymbols);
240      AddAllowedChildSymbol(comparisonSymbols, conditionSymbols);
241      AddAllowedChildSymbol(comparisonSymbols, timeSeriesSymbols);
242
243      AddAllowedChildSymbol(variableCondition, realValuedSymbols);
244      AddAllowedChildSymbol(variableCondition, powerSymbols);
245      AddAllowedChildSymbol(variableCondition, conditionSymbols);
246      AddAllowedChildSymbol(variableCondition, timeSeriesSymbols);
247
248
249      AddAllowedChildSymbol(timeLag, realValuedSymbols);
250      AddAllowedChildSymbol(timeLag, powerSymbols);
251      AddAllowedChildSymbol(timeLag, conditionSymbols);
252
253      AddAllowedChildSymbol(integral, realValuedSymbols);
254      AddAllowedChildSymbol(integral, powerSymbols);
255      AddAllowedChildSymbol(integral, conditionSymbols);
256
257      AddAllowedChildSymbol(derivative, realValuedSymbols);
258      AddAllowedChildSymbol(derivative, powerSymbols);
259      AddAllowedChildSymbol(derivative, conditionSymbols);
260
261      AddAllowedChildSymbol(realValuedSymbols, vectorAggregationSymbols);
262      AddAllowedChildSymbol(vectorAggregationSymbols, basicVectorOperationSymbols);
263      AddAllowedChildSymbol(vectorAggregationSymbols, vectorVariable);
264      AddAllowedChildSymbol(basicVectorOperationSymbols, basicVectorOperationSymbols);
265      AddAllowedChildSymbol(basicVectorOperationSymbols, vectorVariable);
266      #endregion
267    }
268
269    public void ConfigureAsDefaultRegressionGrammar() {
270      Symbols.First(s => s is Average).Enabled = false;
271      Symbols.First(s => s is Absolute).Enabled = false;
272      Symbols.First(s => s is HyperbolicTangent).Enabled = false;
273      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
274      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
275      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
276      Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
277      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
278      Symbols.First(s => s.Name == VectorSymbolsName).Enabled = false;
279    }
280
281    public void ConfigureAsDefaultClassificationGrammar() {
282      Symbols.First(s => s is Average).Enabled = false;
283      Symbols.First(s => s is VariableCondition).Enabled = false;
284      Symbols.First(s => s is Xor).Enabled = false;
285      Symbols.First(s => s is Absolute).Enabled = false;
286      Symbols.First(s => s is HyperbolicTangent).Enabled = false;
287      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
288      Symbols.First(s => s.Name == ExponentialFunctionsName).Enabled = false;
289      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
290      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
291      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
292      Symbols.First(s => s.Name == VectorSymbolsName).Enabled = false;
293    }
294
295    public void ConfigureAsDefaultTimeSeriesPrognosisGrammar() {
296      Symbols.First(s => s is Average).Enabled = false;
297      Symbols.First(s => s is Absolute).Enabled = false;
298      Symbols.First(s => s is HyperbolicTangent).Enabled = false;
299      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
300      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
301      Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
302      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
303      Symbols.First(s => s.Name == VectorSymbolsName).Enabled = false;
304
305      Symbols.Single(s => s.Name == "Variable").Enabled = false;
306      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = true;
307      Symbols.First(s => s is Derivative).Enabled = false;
308      Symbols.First(s => s is Integral).Enabled = false;
309      Symbols.First(s => s is TimeLag).Enabled = false;
310    }
311  }
312}
Note: See TracBrowser for help on using the repository browser.