Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/FullFunctionalExpressionGrammar.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: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableClass]
31  [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
32  public class FullFunctionalExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
33    [StorableConstructor]
34    protected FullFunctionalExpressionGrammar(bool deserializing) : base(deserializing) { }
35    protected FullFunctionalExpressionGrammar(FullFunctionalExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
36    public FullFunctionalExpressionGrammar()
37      : base(ItemAttribute.GetName(typeof(FullFunctionalExpressionGrammar)), ItemAttribute.GetDescription(typeof(FullFunctionalExpressionGrammar))) {
38      Initialize();
39    }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new FullFunctionalExpressionGrammar(this, cloner);
43    }
44
45    private void Initialize() {
46      var add = new Addition();
47      var sub = new Subtraction();
48      var mul = new Multiplication();
49      var div = new Division();
50      var mean = new Average();
51      var sin = new Sine();
52      var cos = new Cosine();
53      var tan = new Tangent();
54      var log = new Logarithm();
55      var pow = new Power();
56      pow.InitialFrequency = 0.0;
57      var square = new Square();
58      square.InitialFrequency = 0.0;
59      var root = new Root();
60      root.InitialFrequency = 0.0;
61      var sqrt = new SquareRoot();
62      sqrt.InitialFrequency = 0.0;
63      var airyA = new AiryA();
64      airyA.InitialFrequency = 0.0;
65      var airyB = new AiryB();
66      airyB.InitialFrequency = 0.0;
67      var bessel = new Bessel();
68      bessel.InitialFrequency = 0.0;
69      var cosineIntegral = new CosineIntegral();
70      cosineIntegral.InitialFrequency = 0.0;
71      var dawson = new Dawson();
72      dawson.InitialFrequency = 0.0;
73      var erf = new Erf();
74      erf.InitialFrequency = 0.0;
75      var expIntegralEi = new ExponentialIntegralEi();
76      expIntegralEi.InitialFrequency = 0.0;
77      var fresnelCosineIntegral = new FresnelCosineIntegral();
78      fresnelCosineIntegral.InitialFrequency = 0.0;
79      var fresnelSineIntegral = new FresnelSineIntegral();
80      fresnelSineIntegral.InitialFrequency = 0.0;
81      var gamma = new Gamma();
82      gamma.InitialFrequency = 0.0;
83      var hypCosineIntegral = new HyperbolicCosineIntegral();
84      hypCosineIntegral.InitialFrequency = 0.0;
85      var hypSineIntegral = new HyperbolicSineIntegral();
86      hypSineIntegral.InitialFrequency = 0.0;
87      var norm = new Norm();
88      norm.InitialFrequency = 0.0;
89      var psi = new Psi();
90      psi.InitialFrequency = 0.0;
91      var sineIntegral = new SineIntegral();
92      sineIntegral.InitialFrequency = 0.0;
93
94      var exp = new Exponential();
95      var @if = new IfThenElse();
96      var gt = new GreaterThan();
97      var lt = new LessThan();
98      var and = new And();
99      var or = new Or();
100      var not = new Not();
101      var xor = new Xor();
102
103      var timeLag = new TimeLag();
104      timeLag.InitialFrequency = 0.0;
105      var integral = new Integral();
106      integral.InitialFrequency = 0.0;
107      var derivative = new Derivative();
108      derivative.InitialFrequency = 0.0;
109
110      var variableCondition = new VariableCondition();
111      variableCondition.InitialFrequency = 0.0;
112
113      var constant = new Constant();
114      constant.MinValue = -20;
115      constant.MaxValue = 20;
116      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
117      var binFactorVariable = new BinaryFactorVariable();
118      var factorVariable = new FactorVariable();
119      var laggedVariable = new LaggedVariable();
120      laggedVariable.InitialFrequency = 0.0;
121      var autoregressiveVariable = new AutoregressiveTargetVariable();
122      autoregressiveVariable.InitialFrequency = 0.0;
123      autoregressiveVariable.Enabled = false;
124
125      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, square, pow, sqrt, root, exp,
126        airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, fresnelCosineIntegral, fresnelSineIntegral, gamma, hypCosineIntegral, hypSineIntegral, norm, psi, sineIntegral,
127        @if, gt, lt, and, or, not,xor, timeLag, integral, derivative, constant, variableSymbol, binFactorVariable, factorVariable, laggedVariable,autoregressiveVariable, variableCondition };
128      var unaryFunctionSymbols = new List<Symbol>() { square, sqrt, sin, cos, tan, log, exp, not, timeLag, integral, derivative,
129        airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, fresnelCosineIntegral, fresnelSineIntegral, gamma, hypCosineIntegral, hypSineIntegral, norm, psi, sineIntegral
130      };
131
132      var binaryFunctionSymbols = new List<Symbol>() { pow, root, gt, lt, variableCondition };
133      var ternarySymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or, xor };
134      var terminalSymbols = new List<Symbol>() { variableSymbol, binFactorVariable, factorVariable, constant, laggedVariable, autoregressiveVariable };
135
136      foreach (var symb in allSymbols)
137        AddSymbol(symb);
138
139      foreach (var funSymb in ternarySymbols) {
140        SetSubtreeCount(funSymb, 1, 3);
141      }
142      foreach (var funSymb in unaryFunctionSymbols) {
143        SetSubtreeCount(funSymb, 1, 1);
144      }
145      foreach (var funSymb in binaryFunctionSymbols) {
146        SetSubtreeCount(funSymb, 2, 2);
147      }
148      foreach (var terminalSymbol in terminalSymbols) {
149        SetSubtreeCount(terminalSymbol, 0, 0);
150      }
151
152      SetSubtreeCount(@if, 3, 3);
153
154
155      // allow each symbol as child of the start symbol
156      foreach (var symb in allSymbols) {
157        AddAllowedChildSymbol(StartSymbol, symb);
158        AddAllowedChildSymbol(DefunSymbol, symb);
159      }
160
161      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
162      foreach (var parent in allSymbols.Except(terminalSymbols)) {
163        foreach (var child in allSymbols)
164          AddAllowedChildSymbol(parent, child);
165      }
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.