source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/FullFunctionalExpressionGrammar.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 12 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 4.1 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;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
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.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
34
35    public FullFunctionalExpressionGrammar()
36      : base() {
37      Initialize();
38    }
39    [StorableConstructor]
40    protected FullFunctionalExpressionGrammar(bool deserializing) : base(deserializing) { }
41
42    private void Initialize() {
43      var add = new Addition();
44      var sub = new Subtraction();
45      var mul = new Multiplication();
46      var div = new Division();
47      var mean = new Average();
48      var sin = new Sine();
49      var cos = new Cosine();
50      var tan = new Tangent();
51      var log = new Logarithm();
52      var exp = new Exponential();
53      var @if = new IfThenElse();
54      var gt = new GreaterThan();
55      var lt = new LessThan();
56      var and = new And();
57      var or = new Or();
58      var not = new Not();
59      var constant = new Constant();
60      constant.MinValue = -20;
61      constant.MaxValue = 20;
62      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
63
64      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };
65      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
66      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
67      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
68
69      foreach (var symb in allSymbols)
70        AddSymbol(symb);
71
72      foreach (var funSymb in functionSymbols) {
73        SetMinSubtreeCount(funSymb, 1);
74        SetMaxSubtreeCount(funSymb, 3);
75      }
76      foreach (var funSymb in unaryFunctionSymbols) {
77        SetMinSubtreeCount(funSymb, 1);
78        SetMaxSubtreeCount(funSymb, 1);
79      }
80      foreach (var funSymb in binaryFunctionSymbols) {
81        SetMinSubtreeCount(funSymb, 2);
82        SetMaxSubtreeCount(funSymb, 2);
83      }
84
85      SetMinSubtreeCount(@if, 3);
86      SetMaxSubtreeCount(@if, 3);
87      SetMinSubtreeCount(constant, 0);
88      SetMaxSubtreeCount(constant, 0);
89      SetMinSubtreeCount(variableSymbol, 0);
90      SetMaxSubtreeCount(variableSymbol, 0);
91
92      // allow each symbol as child of the start symbol
93      foreach (var symb in allSymbols) {
94        SetAllowedChild(StartSymbol, symb, 0);
95      }
96
97      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
98      foreach (var parent in allSymbols) {
99        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
100          foreach (var child in allSymbols) {
101            SetAllowedChild(parent, child, i);
102          }
103      }
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.