Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18132 was 18132, checked in by gkronber, 2 years ago

#3140: merged r18091:18131 from branch to trunk

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