Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentExpressionGrammar.cs @ 6337

Last change on this file since 6337 was 6337, checked in by mkommend, 13 years ago

#1479: Added updated version of grammar editor.

File size: 8.5 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
28  [StorableClass]
29  [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.")]
30  public class TypeCoherentExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
31
32    [StorableConstructor]
33    protected TypeCoherentExpressionGrammar(bool deserializing) : base(deserializing) { }
34    protected TypeCoherentExpressionGrammar(TypeCoherentExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
35    public TypeCoherentExpressionGrammar()
36      : base(ItemAttribute.GetName(typeof(TypeCoherentExpressionGrammar)), ItemAttribute.GetDescription(typeof(TypeCoherentExpressionGrammar))) {
37      Initialize();
38    }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new TypeCoherentExpressionGrammar(this, cloner);
41    }
42
43    private void Initialize() {
44      var add = new Addition();
45      var sub = new Subtraction();
46      var mul = new Multiplication();
47      var div = new Division();
48      var mean = new Average();
49      var sin = new Sine();
50      var cos = new Cosine();
51      var tan = new Tangent();
52      var log = new Logarithm();
53      var pow = new Power();
54      pow.InitialFrequency = 0.0;
55      var root = new Root();
56      root.InitialFrequency = 0.0;
57      var exp = new Exponential();
58      var @if = new IfThenElse();
59      var gt = new GreaterThan();
60      var lt = new LessThan();
61      var and = new And();
62      var or = new Or();
63      var not = new Not();
64
65      var timeLag = new TimeLag();
66      timeLag.InitialFrequency = 0.0;
67      var integral = new Integral();
68      integral.InitialFrequency = 0.0;
69      var derivative = new Derivative();
70      derivative.InitialFrequency = 0.0;
71      var variableCondition = new VariableCondition();
72      variableCondition.InitialFrequency = 0.0;
73
74      var constant = new Constant();
75      constant.MinValue = -20;
76      constant.MaxValue = 20;
77      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
78      var laggedVariable = new LaggedVariable();
79
80      laggedVariable.InitialFrequency = 0.0;
81      mean.InitialFrequency = 0.0;
82
83      /*
84       * Start = RealValueExpression
85       *
86       * RealValueExpression =
87       *   "Variable"  |
88       *   "Constant" |
89       *   BinaryOperator RealValueExpression RealValueExpression |
90       *   UnaryOperator RealValueExpression |
91       *   "IF" BooleanExpression RealValueExpression RealValueExpression |
92       *   "VariableCondition" RealValueExpression RealValueExpression
93       *
94       * BinaryOperator =
95       *   "+" | "-" | "*" | "/" | "Power" | "Root"
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 arithmeticSymbols = new GroupSymbol("Arithmetic Functions", new List<ISymbol>() { add, sub, mul, div });
109      var trigonometricSymbols = new GroupSymbol("Trigonometric Functions", new List<ISymbol>() { sin, cos, tan });
110      var powerSymbols = new GroupSymbol("Power Functions", new List<ISymbol> { pow, root });
111      var exponentialAndLogarithmicSymbols = new GroupSymbol("Exponential and Logarithmic Functions", new List<ISymbol> { exp, log });
112      var terminalSymbols = new GroupSymbol("Terminals", new List<ISymbol> { constant, variableSymbol });
113      var realValuedSymbols = new GroupSymbol("Real valued Symbols",
114        new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, powerSymbols, exponentialAndLogarithmicSymbols, terminalSymbols });
115
116      AddSymbol(realValuedSymbols);
117
118      SetSubtreeCount(arithmeticSymbols, 2, 2);
119      SetSubtreeCount(trigonometricSymbols, 1, 1);
120      SetSubtreeCount(powerSymbols, 2, 2);
121      SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
122      SetSubtreeCount(terminalSymbols, 0, 0);
123
124      AddAllowedChildSymbol(StartSymbol, realValuedSymbols);
125      AddAllowedChildSymbol(DefunSymbol, realValuedSymbols);
126      AddAllowedChildSymbol(realValuedSymbols, realValuedSymbols);
127
128      //var unaryBooleanFunctionSymbols = new List<Symbol>() { not };
129      //var binaryBooleanFunctionSymbols = new List<Symbol>() { or, and };
130      //var relationalFunctionSymbols = new List<Symbol>() { gt, lt };
131      //var booleanSymbols = unaryBooleanFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols);
132
133      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 };
134
135      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, timeLag, integral, derivative };
136      var binaryFunctionSymbols = new List<Symbol>() { add, sub, mul, div, mean, pow, root, variableCondition };
137
138
139
140      //var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
141      //var realValuedSymbols = unaryFunctionSymbols.Concat(binaryFunctionSymbols).Concat(terminalSymbols).Concat(new List<Symbol>() { @if });
142
143
144      //foreach (var symb in allSymbols)
145      //  AddSymbol(symb);
146
147      //foreach (var unaryFun in unaryFunctionSymbols.Concat(unaryBooleanFunctionSymbols)) {
148      //  SetSubtreeCount(unaryFun, 1, 1);
149      //}
150      //foreach (var binaryFun in binaryFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols)) {
151      //  SetSubtreeCount(binaryFun, 2, 2);
152      //}
153
154      //foreach (var terminalSymbol in terminalSymbols) {
155      //  SetSubtreeCount(terminalSymbol, 0, 0);
156      //}
157
158      //SetSubtreeCount(@if, 3, 3);
159
160
161      // allow only real-valued expressions as child of the start symbol
162      //foreach (var symb in realValuedSymbols) {
163      //  AddAllowedChildSymbol(StartSymbol, symb);
164      //  AddAllowedChildSymbol(DefunSymbol, symb);
165      //}
166
167      //foreach (var symb in unaryFunctionSymbols) {
168      //  foreach (var childSymb in realValuedSymbols) {
169      //    AddAllowedChildSymbol(symb, childSymb);
170      //  }
171      //}
172
173      //foreach (var symb in binaryFunctionSymbols) {
174      //  foreach (var childSymb in realValuedSymbols) {
175      //    AddAllowedChildSymbol(symb, childSymb);
176      //  }
177      //}
178
179      //foreach (var childSymb in booleanSymbols) {
180      //  AddAllowedChildSymbol(@if, childSymb, 0);
181      //}
182      //foreach (var childSymb in realValuedSymbols) {
183      //  AddAllowedChildSymbol(@if, childSymb, 1);
184      //  AddAllowedChildSymbol(@if, childSymb, 2);
185      //}
186
187      //foreach (var symb in relationalFunctionSymbols) {
188      //  foreach (var childSymb in realValuedSymbols) {
189      //    AddAllowedChildSymbol(symb, childSymb);
190      //  }
191      //}
192      //foreach (var symb in binaryBooleanFunctionSymbols) {
193      //  foreach (var childSymb in booleanSymbols) {
194      //    AddAllowedChildSymbol(symb, childSymb);
195      //  }
196      //}
197      //foreach (var symb in unaryBooleanFunctionSymbols) {
198      //  foreach (var childSymb in booleanSymbols) {
199      //    AddAllowedChildSymbol(symb, childSymb);
200      //  }
201      //}
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.