Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentExpressionGrammar.cs @ 16360

Last change on this file since 16360 was 16360, checked in by gkronber, 5 years ago

#2915 renamed class AnalyticalQuotient -> AnalyticQuotient

File size: 13.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
29  [StorableClass]
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
45    [StorableConstructor]
46    protected TypeCoherentExpressionGrammar(bool deserializing) : base(deserializing) { }
47    protected TypeCoherentExpressionGrammar(TypeCoherentExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
48    public TypeCoherentExpressionGrammar()
49      : base(ItemAttribute.GetName(typeof(TypeCoherentExpressionGrammar)), ItemAttribute.GetDescription(typeof(TypeCoherentExpressionGrammar))) {
50      Initialize();
51    }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new TypeCoherentExpressionGrammar(this, cloner);
54    }
55
56    private void Initialize() {
57      #region symbol declaration
58      var add = new Addition();
59      var sub = new Subtraction();
60      var mul = new Multiplication();
61      var div = new Division();
62      var mean = new Average();
63      var sin = new Sine();
64      var cos = new Cosine();
65      var tan = new Tangent();
66      var log = new Logarithm();
67      var pow = new Power();
68      var square = new Square();
69      var root = new Root();
70      var sqrt = new SquareRoot();
71      var cube = new Cube();
72      var cubeRoot = new CubeRoot();
73      var exp = new Exponential();
74      var abs = new Absolute();
75
76      var airyA = new AiryA();
77      var airyB = new AiryB();
78      var bessel = new Bessel();
79      var cosineIntegral = new CosineIntegral();
80      var dawson = new Dawson();
81      var erf = new Erf();
82      var expIntegralEi = new ExponentialIntegralEi();
83      var fresnelCosineIntegral = new FresnelCosineIntegral();
84      var fresnelSineIntegral = new FresnelSineIntegral();
85      var gamma = new Gamma();
86      var hypCosineIntegral = new HyperbolicCosineIntegral();
87      var hypSineIntegral = new HyperbolicSineIntegral();
88      var norm = new Norm();
89      var psi = new Psi();
90      var sineIntegral = new SineIntegral();
91      var analyticalQuotient = new AnalyticQuotient();
92
93      var @if = new IfThenElse();
94      var gt = new GreaterThan();
95      var lt = new LessThan();
96      var and = new And();
97      var or = new Or();
98      var not = new Not();
99      var xor = new Xor();
100      var variableCondition = new VariableCondition();
101
102      var timeLag = new TimeLag();
103      var integral = new Integral();
104      var derivative = new Derivative();
105
106      var constant = new Constant();
107      constant.MinValue = -20;
108      constant.MaxValue = 20;
109      var variableSymbol = new Variable();
110      var binFactorVariable = new BinaryFactorVariable();
111      var factorVariable = new FactorVariable();
112      var laggedVariable = new LaggedVariable();
113      var autoregressiveVariable = new AutoregressiveTargetVariable();
114      #endregion
115
116      #region group symbol declaration
117      var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul, div, mean });
118      var trigonometricSymbols = new GroupSymbol(TrigonometricFunctionsName, new List<ISymbol>() { sin, cos, tan });
119      var exponentialAndLogarithmicSymbols = new GroupSymbol(ExponentialFunctionsName, new List<ISymbol> { exp, log });
120      var specialFunctions = new GroupSymbol(SpecialFunctionsName, new List<ISymbol> { abs, airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi,
121        fresnelCosineIntegral,fresnelSineIntegral,gamma,hypCosineIntegral,hypSineIntegral,norm, psi, sineIntegral, analyticalQuotient});
122      var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol, binFactorVariable, factorVariable });
123      var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, specialFunctions, terminalSymbols });
124
125      var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, pow, sqrt, root, cube, cubeRoot });
126
127      var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition });
128      var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt });
129      var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not, xor });
130      var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols });
131
132      var timeSeriesSymbols = new GroupSymbol(TimeSeriesSymbolsName, new List<ISymbol> { timeLag, integral, derivative, laggedVariable, autoregressiveVariable });
133      #endregion
134
135      AddSymbol(realValuedSymbols);
136      AddSymbol(powerSymbols);
137      AddSymbol(conditionalSymbols);
138      AddSymbol(timeSeriesSymbols);
139
140      #region subtree count configuration
141      SetSubtreeCount(arithmeticSymbols, 2, 2);
142      SetSubtreeCount(trigonometricSymbols, 1, 1);
143      SetSubtreeCount(pow, 2, 2);
144      SetSubtreeCount(root, 2, 2);
145      SetSubtreeCount(square, 1, 1);
146      SetSubtreeCount(cube, 1, 1);
147      SetSubtreeCount(sqrt, 1, 1);
148      SetSubtreeCount(cubeRoot, 1, 1);
149      SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
150      foreach(var sy in specialFunctions.Symbols.Except(new[] { analyticalQuotient})) {
151        SetSubtreeCount(sy, 1, 1);
152      }
153      SetSubtreeCount(analyticalQuotient, 2, 2);
154
155      SetSubtreeCount(terminalSymbols, 0, 0);
156
157      SetSubtreeCount(@if, 3, 3);
158      SetSubtreeCount(variableCondition, 2, 2);
159      SetSubtreeCount(comparisonSymbols, 2, 2);
160      SetSubtreeCount(and, 2, 2);
161      SetSubtreeCount(or, 2, 2);
162      SetSubtreeCount(not, 1, 1);
163      SetSubtreeCount(xor, 2, 2);
164
165      SetSubtreeCount(timeLag, 1, 1);
166      SetSubtreeCount(integral, 1, 1);
167      SetSubtreeCount(derivative, 1, 1);
168      SetSubtreeCount(laggedVariable, 0, 0);
169      SetSubtreeCount(autoregressiveVariable, 0, 0);
170      #endregion
171
172      #region allowed child symbols configuration
173      AddAllowedChildSymbol(StartSymbol, realValuedSymbols);
174      AddAllowedChildSymbol(StartSymbol, powerSymbols);
175      AddAllowedChildSymbol(StartSymbol, conditionSymbols);
176      AddAllowedChildSymbol(StartSymbol, timeSeriesSymbols);
177      AddAllowedChildSymbol(StartSymbol, specialFunctions);
178
179      AddAllowedChildSymbol(DefunSymbol, realValuedSymbols);
180      AddAllowedChildSymbol(DefunSymbol, powerSymbols);
181      AddAllowedChildSymbol(DefunSymbol, conditionSymbols);
182      AddAllowedChildSymbol(DefunSymbol, timeSeriesSymbols);
183      AddAllowedChildSymbol(DefunSymbol, specialFunctions);
184
185      AddAllowedChildSymbol(realValuedSymbols, realValuedSymbols);
186      AddAllowedChildSymbol(realValuedSymbols, powerSymbols);
187      AddAllowedChildSymbol(realValuedSymbols, conditionSymbols);
188      AddAllowedChildSymbol(realValuedSymbols, timeSeriesSymbols);
189      AddAllowedChildSymbol(realValuedSymbols, specialFunctions);
190
191      AddAllowedChildSymbol(powerSymbols, variableSymbol, 0);
192      AddAllowedChildSymbol(powerSymbols, laggedVariable, 0);
193      AddAllowedChildSymbol(powerSymbols, autoregressiveVariable, 0);
194      AddAllowedChildSymbol(powerSymbols, constant, 1);
195
196      AddAllowedChildSymbol(square, realValuedSymbols, 0);
197      AddAllowedChildSymbol(square, conditionSymbols, 0);
198      AddAllowedChildSymbol(square, timeSeriesSymbols, 0);
199
200      AddAllowedChildSymbol(sqrt, realValuedSymbols, 0);
201      AddAllowedChildSymbol(sqrt, conditionSymbols, 0);
202      AddAllowedChildSymbol(sqrt, timeSeriesSymbols, 0);
203
204      AddAllowedChildSymbol(@if, comparisonSymbols, 0);
205      AddAllowedChildSymbol(@if, booleanOperationSymbols, 0);
206      AddAllowedChildSymbol(@if, conditionSymbols, 1);
207      AddAllowedChildSymbol(@if, realValuedSymbols, 1);
208      AddAllowedChildSymbol(@if, powerSymbols, 1);
209      AddAllowedChildSymbol(@if, timeSeriesSymbols, 1);
210      AddAllowedChildSymbol(@if, conditionSymbols, 2);
211      AddAllowedChildSymbol(@if, realValuedSymbols, 2);
212      AddAllowedChildSymbol(@if, powerSymbols, 2);
213      AddAllowedChildSymbol(@if, timeSeriesSymbols, 2);
214
215      AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
216      AddAllowedChildSymbol(comparisonSymbols, realValuedSymbols);
217      AddAllowedChildSymbol(comparisonSymbols, powerSymbols);
218      AddAllowedChildSymbol(comparisonSymbols, conditionSymbols);
219      AddAllowedChildSymbol(comparisonSymbols, timeSeriesSymbols);
220
221      AddAllowedChildSymbol(variableCondition, realValuedSymbols);
222      AddAllowedChildSymbol(variableCondition, powerSymbols);
223      AddAllowedChildSymbol(variableCondition, conditionSymbols);
224      AddAllowedChildSymbol(variableCondition, timeSeriesSymbols);
225
226
227      AddAllowedChildSymbol(timeLag, realValuedSymbols);
228      AddAllowedChildSymbol(timeLag, powerSymbols);
229      AddAllowedChildSymbol(timeLag, conditionSymbols);
230
231      AddAllowedChildSymbol(integral, realValuedSymbols);
232      AddAllowedChildSymbol(integral, powerSymbols);
233      AddAllowedChildSymbol(integral, conditionSymbols);
234
235      AddAllowedChildSymbol(derivative, realValuedSymbols);
236      AddAllowedChildSymbol(derivative, powerSymbols);
237      AddAllowedChildSymbol(derivative, conditionSymbols);
238      #endregion
239    }
240
241    public void ConfigureAsDefaultRegressionGrammar() {
242      Symbols.First(s => s is Average).Enabled = false;
243      Symbols.First(s => s is Absolute).Enabled = false;
244      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
245      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
246      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
247      Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
248      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
249    }
250
251    public void ConfigureAsDefaultClassificationGrammar() {
252      Symbols.First(s => s is Average).Enabled = false;
253      Symbols.First(s => s is VariableCondition).Enabled = false;
254      Symbols.First(s => s is Xor).Enabled = false;
255      Symbols.First(s => s is Absolute).Enabled = false;
256      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
257      Symbols.First(s => s.Name == ExponentialFunctionsName).Enabled = false;
258      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
259      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
260      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
261    }
262
263    public void ConfigureAsDefaultTimeSeriesPrognosisGrammar() {
264      Symbols.First(s => s is Average).Enabled = false;
265      Symbols.First(s => s is Absolute).Enabled = false;
266      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
267      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
268      Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
269      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
270
271      Symbols.Single(s => s.Name == "Variable").Enabled = false;
272      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = true;
273      Symbols.First(s => s is Derivative).Enabled = false;
274      Symbols.First(s => s is Integral).Enabled = false;
275      Symbols.First(s => s is TimeLag).Enabled = false;
276    }
277  }
278}
Note: See TracBrowser for help on using the repository browser.