Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/FullFunctionalExpressionGrammar.cs @ 5991

Last change on this file since 5991 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 root = new Root();
58      root.InitialFrequency = 0.0;
59      var exp = new Exponential();
60      var @if = new IfThenElse();
61      var gt = new GreaterThan();
62      var lt = new LessThan();
63      var and = new And();
64      var or = new Or();
65      var not = new Not();
66
67      var timeLag = new TimeLag();
68      timeLag.InitialFrequency = 0.0;
69      var integral = new Integral();
70      integral.InitialFrequency = 0.0;
71      var derivative = new Derivative();
72      derivative.InitialFrequency = 0.0;
73
74      var variableCondition = new VariableCondition();
75      variableCondition.InitialFrequency = 0.0;
76
77      var constant = new Constant();
78      constant.MinValue = -20;
79      constant.MaxValue = 20;
80      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
81      var laggedVariable = new LaggedVariable();
82      laggedVariable.InitialFrequency = 0.0;
83
84      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, pow, root, exp, @if, gt, lt, and, or, not, timeLag, integral, derivative, constant, variableSymbol, laggedVariable, variableCondition };
85      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not, timeLag, integral, derivative };
86
87      var binaryFunctionSymbols = new List<Symbol>() { pow, root, gt, lt, variableCondition };
88      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
89      var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
90
91      foreach (var symb in allSymbols)
92        AddSymbol(symb);
93
94      foreach (var funSymb in functionSymbols) {
95        SetSubtreeCount(funSymb, 1, 3);
96      }
97      foreach (var funSymb in unaryFunctionSymbols) {
98        SetSubtreeCount(funSymb, 1, 1);
99      }
100      foreach (var funSymb in binaryFunctionSymbols) {
101        SetSubtreeCount(funSymb, 2, 2);
102      }
103      foreach (var terminalSymbol in terminalSymbols) {
104        SetSubtreeCount(terminalSymbol, 0, 0);
105      }
106
107      SetSubtreeCount(@if, 3, 3);
108
109
110      // allow each symbol as child of the start symbol
111      foreach (var symb in allSymbols) {
112        AddAllowedChildSymbol(StartSymbol, symb);
113        AddAllowedChildSymbol(DefunSymbol, symb);
114      }
115
116      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
117      foreach (var parent in allSymbols.Except(terminalSymbols)) {
118        foreach (var child in allSymbols)
119          AddAllowedChildSymbol(parent, child);
120      }
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.