Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.ExternalEvaluation.GP/3.3/Grammar/TypeCoherentExpressionGrammar.cs @ 15321

Last change on this file since 15321 was 5881, checked in by epitzer, 13 years ago

Adopt TypeCoherentExpressionGrammar from HeuristicLab.Problems.DataAnalysis.Symbolic for external evaluation (#1459)

File size: 6.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.Encodings.SymbolicExpressionTreeEncoding.Symbols;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.ExternalEvaluation.GP {
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 exp = new Exponential();
57      var @if = new IfThenElse();
58      var gt = new GreaterThan();
59      var lt = new LessThan();
60      var and = new And();
61      var or = new Or();
62      var not = new Not();
63
64      var constant = new Constant();
65      constant.MinValue = -20;
66      constant.MaxValue = 20;
67      var variableSymbol = new Variable();
68
69      mean.InitialFrequency = 0.0;
70
71      /*
72       * Start = RealValueExpression
73       *
74       * RealValueExpression =
75       *   "Variable"  |
76       *   "Constant" |
77       *   BinaryOperator RealValueExpression RealValueExpression |
78       *   UnaryOperator RealValueExpression |
79       *   "IF" BooleanExpression RealValueExpression RealValueExpression |
80       *   "VariableCondition" RealValueExpression RealValueExpression
81       *
82       * BinaryOperator =
83       *   "+" | "-" | "*" | "/"
84       *
85       * UnaryOperator =
86       *   "Sin" | "Cos" | "Tan" | "Log" | "Exp"
87       *
88       * BooleanExpression =
89       *   "AND" BooleanExpression BooleanExpression |
90       *   "OR" BooleanExpression BooleanExpression |
91       *   "NOT" BooleanExpression |
92       *   ">" RealValueExpression RealValueExpression |
93       *   "<" RealValueExpression RealValueExpression
94       */
95
96      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };
97
98      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp };
99      var binaryFunctionSymbols = new List<Symbol>() { add, sub, mul, div, mean };
100
101      var unaryBooleanFunctionSymbols = new List<Symbol>() { not };
102      var binaryBooleanFunctionSymbols = new List<Symbol>() { or, and };
103      var relationalFunctionSymbols = new List<Symbol>() { gt, lt };
104      var terminalSymbols = new List<Symbol>() { variableSymbol, constant };
105      var realValuedSymbols = unaryFunctionSymbols.Concat(binaryFunctionSymbols).Concat(terminalSymbols).Concat(new List<Symbol>() { @if });
106      var booleanSymbols = unaryBooleanFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols);
107
108      foreach (var symb in allSymbols)
109        AddSymbol(symb);
110
111      foreach (var unaryFun in unaryFunctionSymbols.Concat(unaryBooleanFunctionSymbols)) {
112        SetMinSubtreeCount(unaryFun, 1);
113        SetMaxSubtreeCount(unaryFun, 1);
114      }
115      foreach (var binaryFun in binaryFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols)) {
116        SetMinSubtreeCount(binaryFun, 2);
117        SetMaxSubtreeCount(binaryFun, 2);
118      }
119
120      foreach (var terminalSymbol in terminalSymbols) {
121        SetMinSubtreeCount(terminalSymbol, 0);
122        SetMaxSubtreeCount(terminalSymbol, 0);
123      }
124
125      SetMinSubtreeCount(@if, 3);
126      SetMaxSubtreeCount(@if, 3);
127
128
129      // allow only real-valued expressions as child of the start symbol
130      foreach (var symb in realValuedSymbols) {
131        SetAllowedChild(StartSymbol, symb, 0);
132      }
133
134      foreach (var symb in unaryFunctionSymbols) {
135        foreach (var childSymb in realValuedSymbols) {
136          SetAllowedChild(symb, childSymb, 0);
137        }
138      }
139
140      foreach (var symb in binaryFunctionSymbols) {
141        foreach (var childSymb in realValuedSymbols) {
142          SetAllowedChild(symb, childSymb, 0);
143          SetAllowedChild(symb, childSymb, 1);
144        }
145      }
146
147      foreach (var childSymb in booleanSymbols) {
148        SetAllowedChild(@if, childSymb, 0);
149      }
150      foreach (var childSymb in realValuedSymbols) {
151        SetAllowedChild(@if, childSymb, 1);
152        SetAllowedChild(@if, childSymb, 2);
153      }
154
155      foreach (var symb in relationalFunctionSymbols) {
156        foreach (var childSymb in realValuedSymbols) {
157          SetAllowedChild(symb, childSymb, 0);
158          SetAllowedChild(symb, childSymb, 1);
159        }
160      }
161      foreach (var symb in binaryBooleanFunctionSymbols) {
162        foreach (var childSymb in booleanSymbols) {
163          SetAllowedChild(symb, childSymb, 0);
164          SetAllowedChild(symb, childSymb, 1);
165        }
166      }
167      foreach (var symb in unaryBooleanFunctionSymbols) {
168        foreach (var childSymb in booleanSymbols) {
169          SetAllowedChild(symb, childSymb, 0);
170        }
171      }
172      var originalStart = StartSymbol;
173      if (!(originalStart is ProgramRootSymbol)) {
174        var root = new ProgramRootSymbol();
175        AddSymbol(root);
176        SetMinSubtreeCount(root, 1);
177        SetMaxSubtreeCount(root, 1);
178        SetAllowedChild(root, originalStart, 0);
179
180        StartSymbol = root;
181      }
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.