Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/FullFunctionalExpressionGrammar.cs @ 18093

Last change on this file since 18093 was 18093, checked in by chaider, 2 years ago

#3041

  • Renaming Constant Symbol to Num, behaves like before
  • Adding new Symbol RealConstant (Constant), this symbol behaves now like a real constant, won't be changed by parameter optimization or manipulators
  • Refactored classes part1
File size: 7.5 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;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableType("44B0829C-1CB5-4BE9-9514-BBA54FAB2912")]
31  [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
32  public class FullFunctionalExpressionGrammar : DataAnalysisGrammar, ISymbolicDataAnalysisGrammar {
33    [StorableConstructor]
34    protected FullFunctionalExpressionGrammar(StorableConstructorFlag _) : base(_) { }
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 aq = new AnalyticQuotient();
51      var mean = new Average();
52      var sin = new Sine();
53      var cos = new Cosine();
54      var tan = new Tangent();
55      var log = new Logarithm();
56      var abs = new Absolute();
57      var tanh = new HyperbolicTangent();
58      var pow = new Power();
59      pow.InitialFrequency = 0.0;
60      var square = new Square();
61      square.InitialFrequency = 0.0;
62      var cube = new Cube();
63      cube.InitialFrequency = 0.0;
64      var root = new Root();
65      root.InitialFrequency = 0.0;
66      var sqrt = new SquareRoot();
67      sqrt.InitialFrequency = 0.0;
68      var cubeRoot = new CubeRoot();
69      cubeRoot.InitialFrequency = 0.0;
70      var airyA = new AiryA();
71      airyA.InitialFrequency = 0.0;
72      var airyB = new AiryB();
73      airyB.InitialFrequency = 0.0;
74      var bessel = new Bessel();
75      bessel.InitialFrequency = 0.0;
76      var cosineIntegral = new CosineIntegral();
77      cosineIntegral.InitialFrequency = 0.0;
78      var dawson = new Dawson();
79      dawson.InitialFrequency = 0.0;
80      var erf = new Erf();
81      erf.InitialFrequency = 0.0;
82      var expIntegralEi = new ExponentialIntegralEi();
83      expIntegralEi.InitialFrequency = 0.0;
84      var fresnelCosineIntegral = new FresnelCosineIntegral();
85      fresnelCosineIntegral.InitialFrequency = 0.0;
86      var fresnelSineIntegral = new FresnelSineIntegral();
87      fresnelSineIntegral.InitialFrequency = 0.0;
88      var gamma = new Gamma();
89      gamma.InitialFrequency = 0.0;
90      var hypCosineIntegral = new HyperbolicCosineIntegral();
91      hypCosineIntegral.InitialFrequency = 0.0;
92      var hypSineIntegral = new HyperbolicSineIntegral();
93      hypSineIntegral.InitialFrequency = 0.0;
94      var norm = new Norm();
95      norm.InitialFrequency = 0.0;
96      var psi = new Psi();
97      psi.InitialFrequency = 0.0;
98      var sineIntegral = new SineIntegral();
99      sineIntegral.InitialFrequency = 0.0;
100
101      var exp = new Exponential();
102      var @if = new IfThenElse();
103      var gt = new GreaterThan();
104      var lt = new LessThan();
105      var and = new And();
106      var or = new Or();
107      var not = new Not();
108      var xor = new Xor();
109
110      var timeLag = new TimeLag();
111      timeLag.InitialFrequency = 0.0;
112      var integral = new Integral();
113      integral.InitialFrequency = 0.0;
114      var derivative = new Derivative();
115      derivative.InitialFrequency = 0.0;
116
117      var variableCondition = new VariableCondition();
118      variableCondition.InitialFrequency = 0.0;
119
120      var constant = new Num();
121      constant.MinValue = -20;
122      constant.MaxValue = 20;
123      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
124      var binFactorVariable = new BinaryFactorVariable();
125      var factorVariable = new FactorVariable();
126      var laggedVariable = new LaggedVariable();
127      laggedVariable.InitialFrequency = 0.0;
128      var autoregressiveVariable = new AutoregressiveTargetVariable();
129      autoregressiveVariable.InitialFrequency = 0.0;
130      autoregressiveVariable.Enabled = false;
131
132      var number = new RealConstant();
133      number.MinValue = -20;
134      number.MaxValue = 20;
135
136      var allSymbols = new List<Symbol>() { add, sub, mul, div, aq, mean, abs, sin, cos, tan, log, square, cube, pow, sqrt, cubeRoot, root, exp, tanh,
137        airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, fresnelCosineIntegral, fresnelSineIntegral, gamma, hypCosineIntegral, hypSineIntegral, norm, psi, sineIntegral,
138        @if, gt, lt, and, or, not,xor, timeLag, integral, derivative, constant, number, variableSymbol, binFactorVariable, factorVariable, laggedVariable,autoregressiveVariable, variableCondition };
139      var unaryFunctionSymbols = new List<Symbol>() { abs, square, sqrt, cube, cubeRoot, sin, cos, tan, log, exp, tanh, not, timeLag, integral, derivative,
140        airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, fresnelCosineIntegral, fresnelSineIntegral, gamma, hypCosineIntegral, hypSineIntegral, norm, psi, sineIntegral
141      };
142
143      var binaryFunctionSymbols = new List<Symbol>() { pow, root, gt, lt, aq, variableCondition };
144      var ternarySymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or, xor };
145      var terminalSymbols = new List<Symbol>() { variableSymbol, binFactorVariable, factorVariable, constant, number, laggedVariable, autoregressiveVariable };
146
147      foreach (var symb in allSymbols)
148        AddSymbol(symb);
149
150      foreach (var funSymb in ternarySymbols) {
151        SetSubtreeCount(funSymb, 1, 3);
152      }
153      foreach (var funSymb in unaryFunctionSymbols) {
154        SetSubtreeCount(funSymb, 1, 1);
155      }
156      foreach (var funSymb in binaryFunctionSymbols) {
157        SetSubtreeCount(funSymb, 2, 2);
158      }
159      foreach (var terminalSymbol in terminalSymbols) {
160        SetSubtreeCount(terminalSymbol, 0, 0);
161      }
162
163      SetSubtreeCount(@if, 3, 3);
164
165
166      // allow each symbol as child of the start symbol
167      foreach (var symb in allSymbols) {
168        AddAllowedChildSymbol(StartSymbol, symb);
169        AddAllowedChildSymbol(DefunSymbol, symb);
170      }
171
172      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
173      foreach (var parent in allSymbols.Except(terminalSymbols)) {
174        foreach (var child in allSymbols)
175          AddAllowedChildSymbol(parent, child);
176      }
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.