Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation.GP/3.3/Grammar/FullFunctionalExpressionGrammar.cs @ 4089

Last change on this file since 4089 was 4089, checked in by abeham, 12 years ago

#1041

  • Added project with operators to convert expression trees to protocol buffer messages
File size: 3.9 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.Collections.Generic;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.ExternalEvaluation.GP {
29  [StorableClass]
30  [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
31  public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar {
32    [Storable]
33    private HeuristicLab.Problems.ExternalEvaluation.GP.Variable variableSymbol;
34
35    public FullFunctionalExpressionGrammar()
36      : base() {
37      Initialize();
38    }
39
40    private void Initialize() {
41      var add = new Addition();
42      var sub = new Subtraction();
43      var mul = new Multiplication();
44      var div = new Division();
45      var mean = new Average();
46      var sin = new Sine();
47      var cos = new Cosine();
48      var tan = new Tangent();
49      var log = new Logarithm();
50      var exp = new Exponential();
51      var @if = new IfThenElse();
52      var gt = new GreaterThan();
53      var lt = new LessThan();
54      var and = new And();
55      var or = new Or();
56      var not = new Not();
57      var constant = new Constant();
58      constant.MinValue = -20;
59      constant.MaxValue = 20;
60      variableSymbol = new Variable();
61
62      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };
63      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
64      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
65      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or};
66
67      foreach (var symb in allSymbols)
68        AddSymbol(symb);
69
70      foreach (var funSymb in functionSymbols) {
71        SetMinSubtreeCount(funSymb, 1);
72        SetMaxSubtreeCount(funSymb, 3);
73      }
74      foreach (var funSymb in unaryFunctionSymbols) {
75        SetMinSubtreeCount(funSymb, 1);
76        SetMaxSubtreeCount(funSymb, 1);
77      }
78      foreach (var funSymb in binaryFunctionSymbols) {
79        SetMinSubtreeCount(funSymb, 2);
80        SetMaxSubtreeCount(funSymb, 2);
81      }
82
83      SetMinSubtreeCount(@if, 3);
84      SetMaxSubtreeCount(@if, 3);
85      SetMinSubtreeCount(constant, 0);
86      SetMaxSubtreeCount(constant, 0);
87      SetMinSubtreeCount(variableSymbol, 0);
88      SetMaxSubtreeCount(variableSymbol, 0);
89
90      // allow each symbol as child of the start symbol
91      foreach (var symb in allSymbols) {
92        SetAllowedChild(StartSymbol, symb, 0);
93      }
94
95      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
96      foreach (var parent in allSymbols) {
97        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
98          foreach (var child in allSymbols) {
99            SetAllowedChild(parent, child, i);
100          }
101      }
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.