1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | namespace 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 exp = new Exponential();
|
---|
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 |
|
---|
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();
|
---|
95 | var variableCondition = new VariableCondition();
|
---|
96 |
|
---|
97 | var timeLag = new TimeLag();
|
---|
98 | var integral = new Integral();
|
---|
99 | var derivative = new Derivative();
|
---|
100 |
|
---|
101 | var constant = new Constant();
|
---|
102 | constant.MinValue = -20;
|
---|
103 | constant.MaxValue = 20;
|
---|
104 | var variableSymbol = new Variable();
|
---|
105 | var laggedVariable = new LaggedVariable();
|
---|
106 | var autoregressiveVariable = new AutoregressiveTargetVariable();
|
---|
107 | #endregion
|
---|
108 |
|
---|
109 | #region group symbol declaration
|
---|
110 | var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul, div, mean });
|
---|
111 | var trigonometricSymbols = new GroupSymbol(TrigonometricFunctionsName, new List<ISymbol>() { sin, cos, tan });
|
---|
112 | var exponentialAndLogarithmicSymbols = new GroupSymbol(ExponentialFunctionsName, new List<ISymbol> { exp, log });
|
---|
113 | var specialFunctions = new GroupSymbol(SpecialFunctionsName, new List<ISymbol> { airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi,
|
---|
114 | fresnelCosineIntegral,fresnelSineIntegral,gamma,hypCosineIntegral,hypSineIntegral,norm, psi, sineIntegral});
|
---|
115 | var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol });
|
---|
116 | var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, specialFunctions, terminalSymbols });
|
---|
117 |
|
---|
118 | var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, pow, sqrt, root });
|
---|
119 |
|
---|
120 | var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition });
|
---|
121 | var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt });
|
---|
122 | var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not });
|
---|
123 | var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols });
|
---|
124 |
|
---|
125 | var timeSeriesSymbols = new GroupSymbol(TimeSeriesSymbolsName, new List<ISymbol> { timeLag, integral, derivative, laggedVariable, autoregressiveVariable });
|
---|
126 | #endregion
|
---|
127 |
|
---|
128 | AddSymbol(realValuedSymbols);
|
---|
129 | AddSymbol(powerSymbols);
|
---|
130 | AddSymbol(conditionalSymbols);
|
---|
131 | AddSymbol(timeSeriesSymbols);
|
---|
132 |
|
---|
133 | #region subtree count configuration
|
---|
134 | SetSubtreeCount(arithmeticSymbols, 2, 2);
|
---|
135 | SetSubtreeCount(trigonometricSymbols, 1, 1);
|
---|
136 | SetSubtreeCount(pow, 2, 2);
|
---|
137 | SetSubtreeCount(root, 2, 2);
|
---|
138 | SetSubtreeCount(square, 1, 1);
|
---|
139 | SetSubtreeCount(sqrt, 1, 1);
|
---|
140 | SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
|
---|
141 | SetSubtreeCount(specialFunctions, 1, 1);
|
---|
142 | SetSubtreeCount(terminalSymbols, 0, 0);
|
---|
143 |
|
---|
144 | SetSubtreeCount(@if, 3, 3);
|
---|
145 | SetSubtreeCount(variableCondition, 2, 2);
|
---|
146 | SetSubtreeCount(comparisonSymbols, 2, 2);
|
---|
147 | SetSubtreeCount(and, 2, 2);
|
---|
148 | SetSubtreeCount(or, 2, 2);
|
---|
149 | SetSubtreeCount(not, 1, 1);
|
---|
150 |
|
---|
151 | SetSubtreeCount(timeLag, 1, 1);
|
---|
152 | SetSubtreeCount(integral, 1, 1);
|
---|
153 | SetSubtreeCount(derivative, 1, 1);
|
---|
154 | SetSubtreeCount(laggedVariable, 0, 0);
|
---|
155 | SetSubtreeCount(autoregressiveVariable, 0, 0);
|
---|
156 | #endregion
|
---|
157 |
|
---|
158 | #region allowed child symbols configuration
|
---|
159 | AddAllowedChildSymbol(StartSymbol, realValuedSymbols);
|
---|
160 | AddAllowedChildSymbol(StartSymbol, powerSymbols);
|
---|
161 | AddAllowedChildSymbol(StartSymbol, conditionSymbols);
|
---|
162 | AddAllowedChildSymbol(StartSymbol, timeSeriesSymbols);
|
---|
163 | AddAllowedChildSymbol(StartSymbol, specialFunctions);
|
---|
164 |
|
---|
165 | AddAllowedChildSymbol(DefunSymbol, realValuedSymbols);
|
---|
166 | AddAllowedChildSymbol(DefunSymbol, powerSymbols);
|
---|
167 | AddAllowedChildSymbol(DefunSymbol, conditionSymbols);
|
---|
168 | AddAllowedChildSymbol(DefunSymbol, timeSeriesSymbols);
|
---|
169 | AddAllowedChildSymbol(DefunSymbol, specialFunctions);
|
---|
170 |
|
---|
171 | AddAllowedChildSymbol(realValuedSymbols, realValuedSymbols);
|
---|
172 | AddAllowedChildSymbol(realValuedSymbols, powerSymbols);
|
---|
173 | AddAllowedChildSymbol(realValuedSymbols, conditionSymbols);
|
---|
174 | AddAllowedChildSymbol(realValuedSymbols, timeSeriesSymbols);
|
---|
175 | AddAllowedChildSymbol(realValuedSymbols, specialFunctions);
|
---|
176 |
|
---|
177 | AddAllowedChildSymbol(powerSymbols, variableSymbol, 0);
|
---|
178 | AddAllowedChildSymbol(powerSymbols, laggedVariable, 0);
|
---|
179 | AddAllowedChildSymbol(powerSymbols, autoregressiveVariable, 0);
|
---|
180 | AddAllowedChildSymbol(powerSymbols, constant, 1);
|
---|
181 |
|
---|
182 | AddAllowedChildSymbol(square, realValuedSymbols, 0);
|
---|
183 | AddAllowedChildSymbol(square, conditionSymbols, 0);
|
---|
184 | AddAllowedChildSymbol(square, timeSeriesSymbols, 0);
|
---|
185 |
|
---|
186 | AddAllowedChildSymbol(sqrt, realValuedSymbols, 0);
|
---|
187 | AddAllowedChildSymbol(sqrt, conditionSymbols, 0);
|
---|
188 | AddAllowedChildSymbol(sqrt, timeSeriesSymbols, 0);
|
---|
189 |
|
---|
190 | AddAllowedChildSymbol(@if, comparisonSymbols, 0);
|
---|
191 | AddAllowedChildSymbol(@if, booleanOperationSymbols, 0);
|
---|
192 | AddAllowedChildSymbol(@if, conditionSymbols, 1);
|
---|
193 | AddAllowedChildSymbol(@if, realValuedSymbols, 1);
|
---|
194 | AddAllowedChildSymbol(@if, powerSymbols, 1);
|
---|
195 | AddAllowedChildSymbol(@if, timeSeriesSymbols, 1);
|
---|
196 | AddAllowedChildSymbol(@if, conditionSymbols, 2);
|
---|
197 | AddAllowedChildSymbol(@if, realValuedSymbols, 2);
|
---|
198 | AddAllowedChildSymbol(@if, powerSymbols, 2);
|
---|
199 | AddAllowedChildSymbol(@if, timeSeriesSymbols, 2);
|
---|
200 |
|
---|
201 | AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
|
---|
202 | AddAllowedChildSymbol(comparisonSymbols, realValuedSymbols);
|
---|
203 | AddAllowedChildSymbol(comparisonSymbols, powerSymbols);
|
---|
204 | AddAllowedChildSymbol(comparisonSymbols, conditionSymbols);
|
---|
205 | AddAllowedChildSymbol(comparisonSymbols, timeSeriesSymbols);
|
---|
206 |
|
---|
207 | AddAllowedChildSymbol(variableCondition, realValuedSymbols);
|
---|
208 | AddAllowedChildSymbol(variableCondition, powerSymbols);
|
---|
209 | AddAllowedChildSymbol(variableCondition, conditionSymbols);
|
---|
210 | AddAllowedChildSymbol(variableCondition, timeSeriesSymbols);
|
---|
211 |
|
---|
212 |
|
---|
213 | AddAllowedChildSymbol(timeLag, realValuedSymbols);
|
---|
214 | AddAllowedChildSymbol(timeLag, powerSymbols);
|
---|
215 | AddAllowedChildSymbol(timeLag, conditionSymbols);
|
---|
216 |
|
---|
217 | AddAllowedChildSymbol(integral, realValuedSymbols);
|
---|
218 | AddAllowedChildSymbol(integral, powerSymbols);
|
---|
219 | AddAllowedChildSymbol(integral, conditionSymbols);
|
---|
220 |
|
---|
221 | AddAllowedChildSymbol(derivative, realValuedSymbols);
|
---|
222 | AddAllowedChildSymbol(derivative, powerSymbols);
|
---|
223 | AddAllowedChildSymbol(derivative, conditionSymbols);
|
---|
224 | #endregion
|
---|
225 | }
|
---|
226 |
|
---|
227 | public void ConfigureAsDefaultRegressionGrammar() {
|
---|
228 | Symbols.First(s => s is Average).Enabled = false;
|
---|
229 | Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
|
---|
230 | Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
|
---|
231 | Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
|
---|
232 | Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
|
---|
233 | Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
|
---|
234 | }
|
---|
235 |
|
---|
236 | public void ConfigureAsDefaultClassificationGrammar() {
|
---|
237 | Symbols.First(s => s is Average).Enabled = false;
|
---|
238 | Symbols.First(s => s is VariableCondition).Enabled = false;
|
---|
239 | Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
|
---|
240 | Symbols.First(s => s.Name == ExponentialFunctionsName).Enabled = false;
|
---|
241 | Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
|
---|
242 | Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
|
---|
243 | Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = false;
|
---|
244 | }
|
---|
245 |
|
---|
246 | public void ConfigureAsDefaultTimeSeriesPrognosisGrammar() {
|
---|
247 | Symbols.First(s => s is Average).Enabled = false;
|
---|
248 | Symbols.First(s => s.Name == TrigonometricFunctionsName).Enabled = false;
|
---|
249 | Symbols.First(s => s.Name == PowerFunctionsName).Enabled = false;
|
---|
250 | Symbols.First(s => s.Name == ConditionalSymbolsName).Enabled = false;
|
---|
251 | Symbols.First(s => s.Name == SpecialFunctionsName).Enabled = false;
|
---|
252 |
|
---|
253 | Symbols.Single(s => s.Name == "Variable").Enabled = false;
|
---|
254 | Symbols.First(s => s.Name == TimeSeriesSymbolsName).Enabled = true;
|
---|
255 | Symbols.First(s => s is Derivative).Enabled = false;
|
---|
256 | Symbols.First(s => s is Integral).Enabled = false;
|
---|
257 | Symbols.First(s => s is TimeLag).Enabled = false;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|