Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentExpressionGrammar.cs @ 17498

Last change on this file since 17498 was 17498, checked in by mkommend, 4 years ago

#3052: Merged r17413 into stable.

File size: 13.5 KB
RevLine 
[5333]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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;
[17498]24using HEAL.Attic;
[5333]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[17097]29  [StorableType("36A22322-0627-4E25-A468-F2A788AF6D46")]
[5333]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.")]
[17498]31  public class TypeCoherentExpressionGrammar : DataAnalysisGrammar, 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]
[17097]46    protected TypeCoherentExpressionGrammar(StorableConstructorFlag _) : base(_) { }
[5333]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();
[17072]71      var cube = new Cube();
72      var cubeRoot = new CubeRoot();
[5333]73      var exp = new Exponential();
[17072]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();
[17101]87      var tanh = new HyperbolicTangent();
[7696]88      var hypSineIntegral = new HyperbolicSineIntegral();
89      var norm = new Norm();
90      var psi = new Psi();
91      var sineIntegral = new SineIntegral();
[17072]92      var analyticalQuotient = new AnalyticQuotient();
[7696]93
[5333]94      var @if = new IfThenElse();
95      var gt = new GreaterThan();
96      var lt = new LessThan();
97      var and = new And();
98      var or = new Or();
99      var not = new Not();
[10910]100      var xor = new Xor();
[6803]101      var variableCondition = new VariableCondition();
[5393]102
103      var timeLag = new TimeLag();
104      var integral = new Integral();
105      var derivative = new Derivative();
106
[5333]107      var constant = new Constant();
108      constant.MinValue = -20;
109      constant.MaxValue = 20;
[6803]110      var variableSymbol = new Variable();
[15131]111      var binFactorVariable = new BinaryFactorVariable();
112      var factorVariable = new FactorVariable();
[5333]113      var laggedVariable = new LaggedVariable();
[8798]114      var autoregressiveVariable = new AutoregressiveTargetVariable();
[6803]115      #endregion
[5333]116
[6803]117      #region group symbol declaration
118      var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul, div, mean });
[17498]119      var trigonometricSymbols = new GroupSymbol(TrigonometricFunctionsName, new List<ISymbol>() { sin, cos, tan, tanh });
120      var exponentialAndLogarithmicSymbols = new GroupSymbol(ExponentialFunctionsName, new List<ISymbol> { exp, log });
[17072]121      var specialFunctions = new GroupSymbol(SpecialFunctionsName, new List<ISymbol> { abs, airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi,
122        fresnelCosineIntegral,fresnelSineIntegral,gamma,hypCosineIntegral,hypSineIntegral,norm, psi, sineIntegral, analyticalQuotient});
[15131]123      var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol, binFactorVariable, factorVariable });
[7696]124      var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, specialFunctions, terminalSymbols });
[5333]125
[17072]126      var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, pow, sqrt, root, cube, cubeRoot });
[5333]127
[6803]128      var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition });
129      var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt });
[10910]130      var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not, xor });
[6803]131      var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols });
[5333]132
[8798]133      var timeSeriesSymbols = new GroupSymbol(TimeSeriesSymbolsName, new List<ISymbol> { timeLag, integral, derivative, laggedVariable, autoregressiveVariable });
[6803]134      #endregion
[5333]135
[6803]136      AddSymbol(realValuedSymbols);
137      AddSymbol(powerSymbols);
138      AddSymbol(conditionalSymbols);
139      AddSymbol(timeSeriesSymbols);
[5333]140
[6803]141      #region subtree count configuration
142      SetSubtreeCount(arithmeticSymbols, 2, 2);
143      SetSubtreeCount(trigonometricSymbols, 1, 1);
[7695]144      SetSubtreeCount(pow, 2, 2);
145      SetSubtreeCount(root, 2, 2);
146      SetSubtreeCount(square, 1, 1);
[17072]147      SetSubtreeCount(cube, 1, 1);
[7695]148      SetSubtreeCount(sqrt, 1, 1);
[17072]149      SetSubtreeCount(cubeRoot, 1, 1);
[6803]150      SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
[17498]151      foreach (var sy in specialFunctions.Symbols.Except(new[] { analyticalQuotient })) {
[17072]152        SetSubtreeCount(sy, 1, 1);
153      }
154      SetSubtreeCount(analyticalQuotient, 2, 2);
155
[6803]156      SetSubtreeCount(terminalSymbols, 0, 0);
[5333]157
[6803]158      SetSubtreeCount(@if, 3, 3);
159      SetSubtreeCount(variableCondition, 2, 2);
160      SetSubtreeCount(comparisonSymbols, 2, 2);
161      SetSubtreeCount(and, 2, 2);
162      SetSubtreeCount(or, 2, 2);
163      SetSubtreeCount(not, 1, 1);
[10910]164      SetSubtreeCount(xor, 2, 2);
[5333]165
[6803]166      SetSubtreeCount(timeLag, 1, 1);
167      SetSubtreeCount(integral, 1, 1);
168      SetSubtreeCount(derivative, 1, 1);
169      SetSubtreeCount(laggedVariable, 0, 0);
[8798]170      SetSubtreeCount(autoregressiveVariable, 0, 0);
[6803]171      #endregion
[5333]172
[6819]173      #region allowed child symbols configuration
[6803]174      AddAllowedChildSymbol(StartSymbol, realValuedSymbols);
[9459]175      AddAllowedChildSymbol(StartSymbol, powerSymbols);
176      AddAllowedChildSymbol(StartSymbol, conditionSymbols);
177      AddAllowedChildSymbol(StartSymbol, timeSeriesSymbols);
178      AddAllowedChildSymbol(StartSymbol, specialFunctions);
179
[6803]180      AddAllowedChildSymbol(DefunSymbol, realValuedSymbols);
[9459]181      AddAllowedChildSymbol(DefunSymbol, powerSymbols);
182      AddAllowedChildSymbol(DefunSymbol, conditionSymbols);
183      AddAllowedChildSymbol(DefunSymbol, timeSeriesSymbols);
184      AddAllowedChildSymbol(DefunSymbol, specialFunctions);
[5333]185
[6803]186      AddAllowedChildSymbol(realValuedSymbols, realValuedSymbols);
187      AddAllowedChildSymbol(realValuedSymbols, powerSymbols);
188      AddAllowedChildSymbol(realValuedSymbols, conditionSymbols);
189      AddAllowedChildSymbol(realValuedSymbols, timeSeriesSymbols);
[7696]190      AddAllowedChildSymbol(realValuedSymbols, specialFunctions);
[5333]191
[6803]192      AddAllowedChildSymbol(powerSymbols, variableSymbol, 0);
[7071]193      AddAllowedChildSymbol(powerSymbols, laggedVariable, 0);
[9459]194      AddAllowedChildSymbol(powerSymbols, autoregressiveVariable, 0);
[6803]195      AddAllowedChildSymbol(powerSymbols, constant, 1);
[9459]196
[7695]197      AddAllowedChildSymbol(square, realValuedSymbols, 0);
[9459]198      AddAllowedChildSymbol(square, conditionSymbols, 0);
199      AddAllowedChildSymbol(square, timeSeriesSymbols, 0);
200
[7695]201      AddAllowedChildSymbol(sqrt, realValuedSymbols, 0);
[9459]202      AddAllowedChildSymbol(sqrt, conditionSymbols, 0);
203      AddAllowedChildSymbol(sqrt, timeSeriesSymbols, 0);
[5333]204
[6803]205      AddAllowedChildSymbol(@if, comparisonSymbols, 0);
206      AddAllowedChildSymbol(@if, booleanOperationSymbols, 0);
207      AddAllowedChildSymbol(@if, conditionSymbols, 1);
208      AddAllowedChildSymbol(@if, realValuedSymbols, 1);
209      AddAllowedChildSymbol(@if, powerSymbols, 1);
210      AddAllowedChildSymbol(@if, timeSeriesSymbols, 1);
211      AddAllowedChildSymbol(@if, conditionSymbols, 2);
212      AddAllowedChildSymbol(@if, realValuedSymbols, 2);
213      AddAllowedChildSymbol(@if, powerSymbols, 2);
214      AddAllowedChildSymbol(@if, timeSeriesSymbols, 2);
[5333]215
[6803]216      AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
217      AddAllowedChildSymbol(comparisonSymbols, realValuedSymbols);
218      AddAllowedChildSymbol(comparisonSymbols, powerSymbols);
219      AddAllowedChildSymbol(comparisonSymbols, conditionSymbols);
220      AddAllowedChildSymbol(comparisonSymbols, timeSeriesSymbols);
[5333]221
[6803]222      AddAllowedChildSymbol(variableCondition, realValuedSymbols);
223      AddAllowedChildSymbol(variableCondition, powerSymbols);
224      AddAllowedChildSymbol(variableCondition, conditionSymbols);
225      AddAllowedChildSymbol(variableCondition, timeSeriesSymbols);
[5333]226
[6803]227
228      AddAllowedChildSymbol(timeLag, realValuedSymbols);
229      AddAllowedChildSymbol(timeLag, powerSymbols);
230      AddAllowedChildSymbol(timeLag, conditionSymbols);
231
232      AddAllowedChildSymbol(integral, realValuedSymbols);
233      AddAllowedChildSymbol(integral, powerSymbols);
234      AddAllowedChildSymbol(integral, conditionSymbols);
235
236      AddAllowedChildSymbol(derivative, realValuedSymbols);
237      AddAllowedChildSymbol(derivative, powerSymbols);
238      AddAllowedChildSymbol(derivative, conditionSymbols);
239      #endregion
[5333]240    }
[6803]241
242    public void ConfigureAsDefaultRegressionGrammar() {
[7696]243      Symbols.First(s => s is Average).Enabled = false;
[17072]244      Symbols.First(s => s is Absolute).Enabled = false;
[17101]245      Symbols.First(s => s is HyperbolicTangent).Enabled = false;
[7696]246      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
247      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
248      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
249      Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
250      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
[6803]251    }
252
253    public void ConfigureAsDefaultClassificationGrammar() {
[7696]254      Symbols.First(s => s is Average).Enabled = false;
255      Symbols.First(s => s is VariableCondition).Enabled = false;
[10910]256      Symbols.First(s => s is Xor).Enabled = false;
[17072]257      Symbols.First(s => s is Absolute).Enabled = false;
[17101]258      Symbols.First(s => s is HyperbolicTangent).Enabled = false;
[7696]259      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
260      Symbols.First(s => s.Name == ExponentialFunctionsName).Enabled = false;
261      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
262      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
263      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
[6803]264    }
[8798]265
266    public void ConfigureAsDefaultTimeSeriesPrognosisGrammar() {
267      Symbols.First(s => s is Average).Enabled = false;
[17072]268      Symbols.First(s => s is Absolute).Enabled = false;
[17101]269      Symbols.First(s => s is HyperbolicTangent).Enabled = false;
[8798]270      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
271      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
272      Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
273      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
274
[10020]275      Symbols.Single(s => s.Name == "Variable").Enabled = false;
[8798]276      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = true;
277      Symbols.First(s => s is Derivative).Enabled = false;
278      Symbols.First(s => s is Integral).Enabled = false;
279      Symbols.First(s => s is TimeLag).Enabled = false;
280    }
[5333]281  }
282}
Note: See TracBrowser for help on using the repository browser.