Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2915 renamed class AnalyticalQuotient -> AnalyticQuotient

File size: 13.3 KB
RevLine 
[5333]1#region License Information
2/* HeuristicLab
[15583]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5333]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
[5393]22using System.Collections.Generic;
[5333]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.")]
[5686]31  public class TypeCoherentExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
[6803]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";
[7696]42    private const string SpecialFunctionsName = "Special Functions";
[6803]43    private const string TimeSeriesSymbolsName = "Time Series Symbols";
[5333]44
45    [StorableConstructor]
46    protected TypeCoherentExpressionGrammar(bool deserializing) : base(deserializing) { }
47    protected TypeCoherentExpressionGrammar(TypeCoherentExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
48    public TypeCoherentExpressionGrammar()
[5688]49      : base(ItemAttribute.GetName(typeof(TypeCoherentExpressionGrammar)), ItemAttribute.GetDescription(typeof(TypeCoherentExpressionGrammar))) {
[5333]50      Initialize();
51    }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new TypeCoherentExpressionGrammar(this, cloner);
54    }
55
56    private void Initialize() {
[6803]57      #region symbol declaration
[5333]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();
[7695]68      var square = new Square();
[5393]69      var root = new Root();
[7695]70      var sqrt = new SquareRoot();
[16356]71      var cube = new Cube();
72      var cubeRoot = new CubeRoot();
[5333]73      var exp = new Exponential();
[16356]74      var abs = new Absolute();
[7696]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();
[16360]91      var analyticalQuotient = new AnalyticQuotient();
[7696]92
[5333]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();
[10774]99      var xor = new Xor();
[6803]100      var variableCondition = new VariableCondition();
[5393]101
102      var timeLag = new TimeLag();
103      var integral = new Integral();
104      var derivative = new Derivative();
105
[5333]106      var constant = new Constant();
107      constant.MinValue = -20;
108      constant.MaxValue = 20;
[6803]109      var variableSymbol = new Variable();
[14826]110      var binFactorVariable = new BinaryFactorVariable();
111      var factorVariable = new FactorVariable();
[5333]112      var laggedVariable = new LaggedVariable();
[8798]113      var autoregressiveVariable = new AutoregressiveTargetVariable();
[6803]114      #endregion
[5333]115
[6803]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 });
[16356]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});
[14826]122      var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol, binFactorVariable, factorVariable });
[7696]123      var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, specialFunctions, terminalSymbols });
[5333]124
[16356]125      var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, pow, sqrt, root, cube, cubeRoot });
[5333]126
[6803]127      var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition });
128      var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt });
[10774]129      var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not, xor });
[6803]130      var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols });
[5333]131
[8798]132      var timeSeriesSymbols = new GroupSymbol(TimeSeriesSymbolsName, new List<ISymbol> { timeLag, integral, derivative, laggedVariable, autoregressiveVariable });
[6803]133      #endregion
[5333]134
[6803]135      AddSymbol(realValuedSymbols);
136      AddSymbol(powerSymbols);
137      AddSymbol(conditionalSymbols);
138      AddSymbol(timeSeriesSymbols);
[5333]139
[6803]140      #region subtree count configuration
141      SetSubtreeCount(arithmeticSymbols, 2, 2);
142      SetSubtreeCount(trigonometricSymbols, 1, 1);
[7695]143      SetSubtreeCount(pow, 2, 2);
144      SetSubtreeCount(root, 2, 2);
145      SetSubtreeCount(square, 1, 1);
[16356]146      SetSubtreeCount(cube, 1, 1);
[7695]147      SetSubtreeCount(sqrt, 1, 1);
[16356]148      SetSubtreeCount(cubeRoot, 1, 1);
[6803]149      SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
[16356]150      foreach(var sy in specialFunctions.Symbols.Except(new[] { analyticalQuotient})) {
151        SetSubtreeCount(sy, 1, 1);
152      }
153      SetSubtreeCount(analyticalQuotient, 2, 2);
154
[6803]155      SetSubtreeCount(terminalSymbols, 0, 0);
[5333]156
[6803]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);
[10774]163      SetSubtreeCount(xor, 2, 2);
[5333]164
[6803]165      SetSubtreeCount(timeLag, 1, 1);
166      SetSubtreeCount(integral, 1, 1);
167      SetSubtreeCount(derivative, 1, 1);
168      SetSubtreeCount(laggedVariable, 0, 0);
[8798]169      SetSubtreeCount(autoregressiveVariable, 0, 0);
[6803]170      #endregion
[5333]171
[6819]172      #region allowed child symbols configuration
[6803]173      AddAllowedChildSymbol(StartSymbol, realValuedSymbols);
[9459]174      AddAllowedChildSymbol(StartSymbol, powerSymbols);
175      AddAllowedChildSymbol(StartSymbol, conditionSymbols);
176      AddAllowedChildSymbol(StartSymbol, timeSeriesSymbols);
177      AddAllowedChildSymbol(StartSymbol, specialFunctions);
178
[6803]179      AddAllowedChildSymbol(DefunSymbol, realValuedSymbols);
[9459]180      AddAllowedChildSymbol(DefunSymbol, powerSymbols);
181      AddAllowedChildSymbol(DefunSymbol, conditionSymbols);
182      AddAllowedChildSymbol(DefunSymbol, timeSeriesSymbols);
183      AddAllowedChildSymbol(DefunSymbol, specialFunctions);
[5333]184
[6803]185      AddAllowedChildSymbol(realValuedSymbols, realValuedSymbols);
186      AddAllowedChildSymbol(realValuedSymbols, powerSymbols);
187      AddAllowedChildSymbol(realValuedSymbols, conditionSymbols);
188      AddAllowedChildSymbol(realValuedSymbols, timeSeriesSymbols);
[7696]189      AddAllowedChildSymbol(realValuedSymbols, specialFunctions);
[5333]190
[6803]191      AddAllowedChildSymbol(powerSymbols, variableSymbol, 0);
[7071]192      AddAllowedChildSymbol(powerSymbols, laggedVariable, 0);
[9459]193      AddAllowedChildSymbol(powerSymbols, autoregressiveVariable, 0);
[6803]194      AddAllowedChildSymbol(powerSymbols, constant, 1);
[9459]195
[7695]196      AddAllowedChildSymbol(square, realValuedSymbols, 0);
[9459]197      AddAllowedChildSymbol(square, conditionSymbols, 0);
198      AddAllowedChildSymbol(square, timeSeriesSymbols, 0);
199
[7695]200      AddAllowedChildSymbol(sqrt, realValuedSymbols, 0);
[9459]201      AddAllowedChildSymbol(sqrt, conditionSymbols, 0);
202      AddAllowedChildSymbol(sqrt, timeSeriesSymbols, 0);
[5333]203
[6803]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);
[5333]214
[6803]215      AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
216      AddAllowedChildSymbol(comparisonSymbols, realValuedSymbols);
217      AddAllowedChildSymbol(comparisonSymbols, powerSymbols);
218      AddAllowedChildSymbol(comparisonSymbols, conditionSymbols);
219      AddAllowedChildSymbol(comparisonSymbols, timeSeriesSymbols);
[5333]220
[6803]221      AddAllowedChildSymbol(variableCondition, realValuedSymbols);
222      AddAllowedChildSymbol(variableCondition, powerSymbols);
223      AddAllowedChildSymbol(variableCondition, conditionSymbols);
224      AddAllowedChildSymbol(variableCondition, timeSeriesSymbols);
[5333]225
[6803]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
[5333]239    }
[6803]240
241    public void ConfigureAsDefaultRegressionGrammar() {
[7696]242      Symbols.First(s => s is Average).Enabled = false;
[16356]243      Symbols.First(s => s is Absolute).Enabled = false;
[7696]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;
[6803]249    }
250
251    public void ConfigureAsDefaultClassificationGrammar() {
[7696]252      Symbols.First(s => s is Average).Enabled = false;
253      Symbols.First(s => s is VariableCondition).Enabled = false;
[10789]254      Symbols.First(s => s is Xor).Enabled = false;
[16356]255      Symbols.First(s => s is Absolute).Enabled = false;
[7696]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;
[6803]261    }
[8798]262
263    public void ConfigureAsDefaultTimeSeriesPrognosisGrammar() {
264      Symbols.First(s => s is Average).Enabled = false;
[16356]265      Symbols.First(s => s is Absolute).Enabled = false;
[8798]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
[9992]271      Symbols.Single(s => s.Name == "Variable").Enabled = false;
[8798]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    }
[5333]277  }
278}
Note: See TracBrowser for help on using the repository browser.