Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3993 was 3993, checked in by mkommend, 14 years ago

changed symbols and grammars to be more efficient in respect to cloning, construction and deserialization (ticket #1073)

File size: 4.2 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 HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
23using System.Collections.Generic;
24using System;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
30using HeuristicLab.Data;
31using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [StorableClass]
34  [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
35  public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar {
36    [Storable]
37    private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
38
39    public FullFunctionalExpressionGrammar()
40      : base() {
41      Initialize();
42    }
43    [StorableConstructor]
44    protected FullFunctionalExpressionGrammar(bool deserializing) : base(deserializing) { }
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      var constant = new Constant();
64      constant.MinValue = -20;
65      constant.MaxValue = 20;
66      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
67
68      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };
69      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
70      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
71      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
72
73      foreach (var symb in allSymbols)
74        AddSymbol(symb);
75
76      foreach (var funSymb in functionSymbols) {
77        SetMinSubtreeCount(funSymb, 1);
78        SetMaxSubtreeCount(funSymb, 3);
79      }
80      foreach (var funSymb in unaryFunctionSymbols) {
81        SetMinSubtreeCount(funSymb, 1);
82        SetMaxSubtreeCount(funSymb, 1);
83      }
84      foreach (var funSymb in binaryFunctionSymbols) {
85        SetMinSubtreeCount(funSymb, 2);
86        SetMaxSubtreeCount(funSymb, 2);
87      }
88
89      SetMinSubtreeCount(@if, 3);
90      SetMaxSubtreeCount(@if, 3);
91      SetMinSubtreeCount(constant, 0);
92      SetMaxSubtreeCount(constant, 0);
93      SetMinSubtreeCount(variableSymbol, 0);
94      SetMaxSubtreeCount(variableSymbol, 0);
95
96      // allow each symbol as child of the start symbol
97      foreach (var symb in allSymbols) {
98        SetAllowedChild(StartSymbol, symb, 0);
99      }
100
101      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
102      foreach (var parent in allSymbols) {
103        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
104          foreach (var child in allSymbols) {
105            SetAllowedChild(parent, child, i);
106          }
107      }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.