Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5384 was 5333, checked in by gkronber, 13 years ago

Implemented type coherent grammar. #1387

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
23using System.Collections.Generic;
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      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      var constant = new Constant();
65      constant.MinValue = -20;
66      constant.MaxValue = 20;
67      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
68      var laggedVariable = new LaggedVariable();
69
70      laggedVariable.InitialFrequency = 0.0;
71      mean.InitialFrequency = 0.0;
72
73      /*
74       * Start = RealValueExpression
75       *
76       * RealValueExpression =
77       *   "Variable"  |
78       *   "Constant" |
79       *   BinaryOperator RealValueExpression RealValueExpression |
80       *   UnaryOperator RealValueExpression |
81       *   "IF" BooleanExpression RealValueExpression RealValueExpression |
82       *
83       * BinaryOperator =
84       *   "+" | "-" | "*" | "/" | "Power"
85       *
86       * UnaryOperator =
87       *   "Sin" | "Cos" | "Tan" | "Log" | "Exp"
88       *
89       * BooleanExpression =
90       *   "AND" BooleanExpression BooleanExpression |
91       *   "OR" BooleanExpression BooleanExpression |
92       *   "NOT" BooleanExpression |
93       *   ">" RealValueExpression RealValueExpression |
94       *   "<" RealValueExpression RealValueExpression
95       */
96
97      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, pow, exp, @if, gt, lt, and, or, not, constant, variableSymbol, laggedVariable };
98
99      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, };
100      var binaryFunctionSymbols = new List<Symbol>() { add, sub, mul, div, mean, pow };
101
102      var unaryBooleanFunctionSymbols = new List<Symbol>() { not };
103      var binaryBooleanFunctionSymbols = new List<Symbol>() { or, and };
104      var relationalFunctionSymbols = new List<Symbol>() { gt, lt };
105      var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
106      var realValuedSymbols = unaryFunctionSymbols.Concat(binaryFunctionSymbols).Concat(terminalSymbols).Concat(new List<Symbol>() { @if });
107      var booleanSymbols = unaryBooleanFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols);
108
109      foreach (var symb in allSymbols)
110        AddSymbol(symb);
111
112      foreach (var unaryFun in unaryFunctionSymbols.Concat(unaryBooleanFunctionSymbols)) {
113        SetMinSubtreeCount(unaryFun, 1);
114        SetMaxSubtreeCount(unaryFun, 1);
115      }
116      foreach (var binaryFun in binaryFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols)) {
117        SetMinSubtreeCount(binaryFun, 2);
118        SetMaxSubtreeCount(binaryFun, 2);
119      }
120
121      foreach (var terminalSymbol in terminalSymbols) {
122        SetMinSubtreeCount(terminalSymbol, 0);
123        SetMaxSubtreeCount(terminalSymbol, 0);
124      }
125
126      SetMinSubtreeCount(@if, 3);
127      SetMaxSubtreeCount(@if, 3);
128
129
130      // allow only real-valued expressions as child of the start symbol
131      foreach (var symb in realValuedSymbols) {
132        SetAllowedChild(StartSymbol, symb, 0);
133      }
134
135      foreach (var symb in unaryFunctionSymbols) {
136        foreach (var childSymb in realValuedSymbols) {
137          SetAllowedChild(symb, childSymb, 0);
138        }
139      }
140
141      foreach (var symb in binaryFunctionSymbols) {
142        foreach (var childSymb in realValuedSymbols) {
143          SetAllowedChild(symb, childSymb, 0);
144          SetAllowedChild(symb, childSymb, 1);
145        }
146      }
147
148      foreach (var childSymb in booleanSymbols) {
149        SetAllowedChild(@if, childSymb, 0);
150      }
151      foreach (var childSymb in realValuedSymbols) {
152        SetAllowedChild(@if, childSymb, 1);
153        SetAllowedChild(@if, childSymb, 2);
154      }
155
156      foreach (var symb in relationalFunctionSymbols) {
157        foreach (var childSymb in realValuedSymbols) {
158          SetAllowedChild(symb, childSymb, 0);
159          SetAllowedChild(symb, childSymb, 1);
160        }
161      }
162      foreach (var symb in binaryBooleanFunctionSymbols) {
163        foreach (var childSymb in booleanSymbols) {
164          SetAllowedChild(symb, childSymb, 0);
165          SetAllowedChild(symb, childSymb, 1);
166        }
167      }
168      foreach (var symb in unaryBooleanFunctionSymbols) {
169        foreach (var childSymb in booleanSymbols) {
170          SetAllowedChild(symb, childSymb, 0);
171        }
172      }
173    }
174  }
175}
Note: See TracBrowser for help on using the repository browser.