Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/TypeCoherentExpressionGrammar.cs @ 5445

Last change on this file since 5445 was 5445, checked in by swagner, 13 years ago

Updated year of copyrights (#1406)

File size: 7.2 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.Encodings.SymbolicExpressionTreeEncoding.Symbols;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
31  [StorableClass]
32  [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.")]
33  public class TypeCoherentExpressionGrammar : DefaultSymbolicExpressionGrammar {
34
35    [StorableConstructor]
36    protected TypeCoherentExpressionGrammar(bool deserializing) : base(deserializing) { }
37    protected TypeCoherentExpressionGrammar(TypeCoherentExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
38    public TypeCoherentExpressionGrammar()
39      : base() {
40      Initialize();
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new TypeCoherentExpressionGrammar(this, cloner);
44    }
45
46    private void Initialize() {
47      var add = new Addition();
48      var sub = new Subtraction();
49      var mul = new Multiplication();
50      var div = new Division();
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 pow = new Power();
57      pow.InitialFrequency = 0.0;
58      var root = new Root();
59      root.InitialFrequency = 0.0;
60      var exp = new Exponential();
61      var @if = new IfThenElse();
62      var gt = new GreaterThan();
63      var lt = new LessThan();
64      var and = new And();
65      var or = new Or();
66      var not = new Not();
67
68      var timeLag = new TimeLag();
69      timeLag.InitialFrequency = 0.0;
70      var integral = new Integral();
71      integral.InitialFrequency = 0.0;
72      var derivative = new Derivative();
73      derivative.InitialFrequency = 0.0;
74
75      var constant = new Constant();
76      constant.MinValue = -20;
77      constant.MaxValue = 20;
78      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
79      var laggedVariable = new LaggedVariable();
80
81      laggedVariable.InitialFrequency = 0.0;
82      mean.InitialFrequency = 0.0;
83
84      /*
85       * Start = RealValueExpression
86       *
87       * RealValueExpression =
88       *   "Variable"  |
89       *   "Constant" |
90       *   BinaryOperator RealValueExpression RealValueExpression |
91       *   UnaryOperator RealValueExpression |
92       *   "IF" BooleanExpression RealValueExpression RealValueExpression |
93       *
94       * BinaryOperator =
95       *   "+" | "-" | "*" | "/" | "Power"
96       *
97       * UnaryOperator =
98       *   "Sin" | "Cos" | "Tan" | "Log" | "Exp"
99       *
100       * BooleanExpression =
101       *   "AND" BooleanExpression BooleanExpression |
102       *   "OR" BooleanExpression BooleanExpression |
103       *   "NOT" BooleanExpression |
104       *   ">" RealValueExpression RealValueExpression |
105       *   "<" RealValueExpression RealValueExpression
106       */
107
108      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 };
109
110      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, timeLag, integral, derivative };
111      var binaryFunctionSymbols = new List<Symbol>() { add, sub, mul, div, mean, pow, root };
112
113      var unaryBooleanFunctionSymbols = new List<Symbol>() { not };
114      var binaryBooleanFunctionSymbols = new List<Symbol>() { or, and };
115      var relationalFunctionSymbols = new List<Symbol>() { gt, lt };
116      var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
117      var realValuedSymbols = unaryFunctionSymbols.Concat(binaryFunctionSymbols).Concat(terminalSymbols).Concat(new List<Symbol>() { @if });
118      var booleanSymbols = unaryBooleanFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols);
119
120      foreach (var symb in allSymbols)
121        AddSymbol(symb);
122
123      foreach (var unaryFun in unaryFunctionSymbols.Concat(unaryBooleanFunctionSymbols)) {
124        SetMinSubtreeCount(unaryFun, 1);
125        SetMaxSubtreeCount(unaryFun, 1);
126      }
127      foreach (var binaryFun in binaryFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols)) {
128        SetMinSubtreeCount(binaryFun, 2);
129        SetMaxSubtreeCount(binaryFun, 2);
130      }
131
132      foreach (var terminalSymbol in terminalSymbols) {
133        SetMinSubtreeCount(terminalSymbol, 0);
134        SetMaxSubtreeCount(terminalSymbol, 0);
135      }
136
137      SetMinSubtreeCount(@if, 3);
138      SetMaxSubtreeCount(@if, 3);
139
140
141      // allow only real-valued expressions as child of the start symbol
142      foreach (var symb in realValuedSymbols) {
143        SetAllowedChild(StartSymbol, symb, 0);
144      }
145
146      foreach (var symb in unaryFunctionSymbols) {
147        foreach (var childSymb in realValuedSymbols) {
148          SetAllowedChild(symb, childSymb, 0);
149        }
150      }
151
152      foreach (var symb in binaryFunctionSymbols) {
153        foreach (var childSymb in realValuedSymbols) {
154          SetAllowedChild(symb, childSymb, 0);
155          SetAllowedChild(symb, childSymb, 1);
156        }
157      }
158
159      foreach (var childSymb in booleanSymbols) {
160        SetAllowedChild(@if, childSymb, 0);
161      }
162      foreach (var childSymb in realValuedSymbols) {
163        SetAllowedChild(@if, childSymb, 1);
164        SetAllowedChild(@if, childSymb, 2);
165      }
166
167      foreach (var symb in relationalFunctionSymbols) {
168        foreach (var childSymb in realValuedSymbols) {
169          SetAllowedChild(symb, childSymb, 0);
170          SetAllowedChild(symb, childSymb, 1);
171        }
172      }
173      foreach (var symb in binaryBooleanFunctionSymbols) {
174        foreach (var childSymb in booleanSymbols) {
175          SetAllowedChild(symb, childSymb, 0);
176          SetAllowedChild(symb, childSymb, 1);
177        }
178      }
179      foreach (var symb in unaryBooleanFunctionSymbols) {
180        foreach (var childSymb in booleanSymbols) {
181          SetAllowedChild(symb, childSymb, 0);
182        }
183      }
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.