Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14826 was 14826, checked in by gkronber, 7 years ago

#2650: merged the factors branch into trunk

File size: 12.7 KB
RevLine 
[5333]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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();
[5333]71      var exp = new Exponential();
[7696]72
73      var airyA = new AiryA();
74      var airyB = new AiryB();
75      var bessel = new Bessel();
76      var cosineIntegral = new CosineIntegral();
77      var dawson = new Dawson();
78      var erf = new Erf();
79      var expIntegralEi = new ExponentialIntegralEi();
80      var fresnelCosineIntegral = new FresnelCosineIntegral();
81      var fresnelSineIntegral = new FresnelSineIntegral();
82      var gamma = new Gamma();
83      var hypCosineIntegral = new HyperbolicCosineIntegral();
84      var hypSineIntegral = new HyperbolicSineIntegral();
85      var norm = new Norm();
86      var psi = new Psi();
87      var sineIntegral = new SineIntegral();
88
[5333]89      var @if = new IfThenElse();
90      var gt = new GreaterThan();
91      var lt = new LessThan();
92      var and = new And();
93      var or = new Or();
94      var not = new Not();
[10774]95      var xor = new Xor();
[6803]96      var variableCondition = new VariableCondition();
[5393]97
98      var timeLag = new TimeLag();
99      var integral = new Integral();
100      var derivative = new Derivative();
101
[5333]102      var constant = new Constant();
103      constant.MinValue = -20;
104      constant.MaxValue = 20;
[6803]105      var variableSymbol = new Variable();
[14826]106      var binFactorVariable = new BinaryFactorVariable();
107      var factorVariable = new FactorVariable();
[5333]108      var laggedVariable = new LaggedVariable();
[8798]109      var autoregressiveVariable = new AutoregressiveTargetVariable();
[6803]110      #endregion
[5333]111
[6803]112      #region group symbol declaration
113      var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul, div, mean });
114      var trigonometricSymbols = new GroupSymbol(TrigonometricFunctionsName, new List<ISymbol>() { sin, cos, tan });
115      var exponentialAndLogarithmicSymbols = new GroupSymbol(ExponentialFunctionsName, new List<ISymbol> { exp, log });
[14826]116      var specialFunctions = new GroupSymbol(SpecialFunctionsName, new List<ISymbol> { airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi,
[7696]117        fresnelCosineIntegral,fresnelSineIntegral,gamma,hypCosineIntegral,hypSineIntegral,norm, psi, sineIntegral});
[14826]118      var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol, binFactorVariable, factorVariable });
[7696]119      var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, specialFunctions, terminalSymbols });
[5333]120
[7695]121      var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, pow, sqrt, root });
[5333]122
[6803]123      var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition });
124      var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt });
[10774]125      var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not, xor });
[6803]126      var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols });
[5333]127
[8798]128      var timeSeriesSymbols = new GroupSymbol(TimeSeriesSymbolsName, new List<ISymbol> { timeLag, integral, derivative, laggedVariable, autoregressiveVariable });
[6803]129      #endregion
[5333]130
[6803]131      AddSymbol(realValuedSymbols);
132      AddSymbol(powerSymbols);
133      AddSymbol(conditionalSymbols);
134      AddSymbol(timeSeriesSymbols);
[5333]135
[6803]136      #region subtree count configuration
137      SetSubtreeCount(arithmeticSymbols, 2, 2);
138      SetSubtreeCount(trigonometricSymbols, 1, 1);
[7695]139      SetSubtreeCount(pow, 2, 2);
140      SetSubtreeCount(root, 2, 2);
141      SetSubtreeCount(square, 1, 1);
142      SetSubtreeCount(sqrt, 1, 1);
[6803]143      SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
[7696]144      SetSubtreeCount(specialFunctions, 1, 1);
[6803]145      SetSubtreeCount(terminalSymbols, 0, 0);
[5333]146
[6803]147      SetSubtreeCount(@if, 3, 3);
148      SetSubtreeCount(variableCondition, 2, 2);
149      SetSubtreeCount(comparisonSymbols, 2, 2);
150      SetSubtreeCount(and, 2, 2);
151      SetSubtreeCount(or, 2, 2);
152      SetSubtreeCount(not, 1, 1);
[10774]153      SetSubtreeCount(xor, 2, 2);
[5333]154
[6803]155      SetSubtreeCount(timeLag, 1, 1);
156      SetSubtreeCount(integral, 1, 1);
157      SetSubtreeCount(derivative, 1, 1);
158      SetSubtreeCount(laggedVariable, 0, 0);
[8798]159      SetSubtreeCount(autoregressiveVariable, 0, 0);
[6803]160      #endregion
[5333]161
[6819]162      #region allowed child symbols configuration
[6803]163      AddAllowedChildSymbol(StartSymbol, realValuedSymbols);
[9459]164      AddAllowedChildSymbol(StartSymbol, powerSymbols);
165      AddAllowedChildSymbol(StartSymbol, conditionSymbols);
166      AddAllowedChildSymbol(StartSymbol, timeSeriesSymbols);
167      AddAllowedChildSymbol(StartSymbol, specialFunctions);
168
[6803]169      AddAllowedChildSymbol(DefunSymbol, realValuedSymbols);
[9459]170      AddAllowedChildSymbol(DefunSymbol, powerSymbols);
171      AddAllowedChildSymbol(DefunSymbol, conditionSymbols);
172      AddAllowedChildSymbol(DefunSymbol, timeSeriesSymbols);
173      AddAllowedChildSymbol(DefunSymbol, specialFunctions);
[5333]174
[6803]175      AddAllowedChildSymbol(realValuedSymbols, realValuedSymbols);
176      AddAllowedChildSymbol(realValuedSymbols, powerSymbols);
177      AddAllowedChildSymbol(realValuedSymbols, conditionSymbols);
178      AddAllowedChildSymbol(realValuedSymbols, timeSeriesSymbols);
[7696]179      AddAllowedChildSymbol(realValuedSymbols, specialFunctions);
[5333]180
[6803]181      AddAllowedChildSymbol(powerSymbols, variableSymbol, 0);
[7071]182      AddAllowedChildSymbol(powerSymbols, laggedVariable, 0);
[9459]183      AddAllowedChildSymbol(powerSymbols, autoregressiveVariable, 0);
[6803]184      AddAllowedChildSymbol(powerSymbols, constant, 1);
[9459]185
[7695]186      AddAllowedChildSymbol(square, realValuedSymbols, 0);
[9459]187      AddAllowedChildSymbol(square, conditionSymbols, 0);
188      AddAllowedChildSymbol(square, timeSeriesSymbols, 0);
189
[7695]190      AddAllowedChildSymbol(sqrt, realValuedSymbols, 0);
[9459]191      AddAllowedChildSymbol(sqrt, conditionSymbols, 0);
192      AddAllowedChildSymbol(sqrt, timeSeriesSymbols, 0);
[5333]193
[6803]194      AddAllowedChildSymbol(@if, comparisonSymbols, 0);
195      AddAllowedChildSymbol(@if, booleanOperationSymbols, 0);
196      AddAllowedChildSymbol(@if, conditionSymbols, 1);
197      AddAllowedChildSymbol(@if, realValuedSymbols, 1);
198      AddAllowedChildSymbol(@if, powerSymbols, 1);
199      AddAllowedChildSymbol(@if, timeSeriesSymbols, 1);
200      AddAllowedChildSymbol(@if, conditionSymbols, 2);
201      AddAllowedChildSymbol(@if, realValuedSymbols, 2);
202      AddAllowedChildSymbol(@if, powerSymbols, 2);
203      AddAllowedChildSymbol(@if, timeSeriesSymbols, 2);
[5333]204
[6803]205      AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
206      AddAllowedChildSymbol(comparisonSymbols, realValuedSymbols);
207      AddAllowedChildSymbol(comparisonSymbols, powerSymbols);
208      AddAllowedChildSymbol(comparisonSymbols, conditionSymbols);
209      AddAllowedChildSymbol(comparisonSymbols, timeSeriesSymbols);
[5333]210
[6803]211      AddAllowedChildSymbol(variableCondition, realValuedSymbols);
212      AddAllowedChildSymbol(variableCondition, powerSymbols);
213      AddAllowedChildSymbol(variableCondition, conditionSymbols);
214      AddAllowedChildSymbol(variableCondition, timeSeriesSymbols);
[5333]215
[6803]216
217      AddAllowedChildSymbol(timeLag, realValuedSymbols);
218      AddAllowedChildSymbol(timeLag, powerSymbols);
219      AddAllowedChildSymbol(timeLag, conditionSymbols);
220
221      AddAllowedChildSymbol(integral, realValuedSymbols);
222      AddAllowedChildSymbol(integral, powerSymbols);
223      AddAllowedChildSymbol(integral, conditionSymbols);
224
225      AddAllowedChildSymbol(derivative, realValuedSymbols);
226      AddAllowedChildSymbol(derivative, powerSymbols);
227      AddAllowedChildSymbol(derivative, conditionSymbols);
228      #endregion
[5333]229    }
[6803]230
231    public void ConfigureAsDefaultRegressionGrammar() {
[7696]232      Symbols.First(s => s is Average).Enabled = false;
233      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
234      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
235      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
236      Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
237      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
[6803]238    }
239
240    public void ConfigureAsDefaultClassificationGrammar() {
[7696]241      Symbols.First(s => s is Average).Enabled = false;
242      Symbols.First(s => s is VariableCondition).Enabled = false;
[10789]243      Symbols.First(s => s is Xor).Enabled = false;
[7696]244      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
245      Symbols.First(s => s.Name == ExponentialFunctionsName).Enabled = false;
246      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
247      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
248      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
[6803]249    }
[8798]250
251    public void ConfigureAsDefaultTimeSeriesPrognosisGrammar() {
252      Symbols.First(s => s is Average).Enabled = false;
253      Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
254      Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
255      Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
256      Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
257
[9992]258      Symbols.Single(s => s.Name == "Variable").Enabled = false;
[8798]259      Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = true;
260      Symbols.First(s => s is Derivative).Enabled = false;
261      Symbols.First(s => s is Integral).Enabled = false;
262      Symbols.First(s => s is TimeLag).Enabled = false;
263    }
[5333]264  }
265}
Note: See TracBrowser for help on using the repository browser.