Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/ArithmeticExpressionGrammar.cs @ 4398

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

Refactored grammars (ticket #1028).

File size: 3.0 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("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
31  public class ArithmeticExpressionGrammar : DefaultSymbolicExpressionGrammar {
32    public ArithmeticExpressionGrammar()
33      : base() {
34      Initialize();
35    }
36
37    [StorableConstructor]
38    protected ArithmeticExpressionGrammar(bool deserializing) : base(deserializing) { }
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 constant = new Constant();
46      constant.MinValue = -20;
47      constant.MaxValue = 20;
48      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
49
50      var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };
51      var functionSymbols = new List<Symbol>() { add, sub, mul, div };
52
53      foreach (var symb in allSymbols)
54        AddSymbol(symb);
55
56      foreach (var funSymb in functionSymbols) {
57        SetMinSubtreeCount(funSymb, 1);
58        SetMaxSubtreeCount(funSymb, 3);
59      }
60      SetMinSubtreeCount(constant, 0);
61      SetMaxSubtreeCount(constant, 0);
62      SetMinSubtreeCount(variableSymbol, 0);
63      SetMaxSubtreeCount(variableSymbol, 0);
64
65      // allow each symbol as child of the start symbol
66      foreach (var symb in allSymbols) {
67        SetAllowedChild(StartSymbol, symb, 0);
68      }
69
70      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
71      foreach (var parent in allSymbols) {
72        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
73          foreach (var child in allSymbols) {
74            SetAllowedChild(parent, child, i);
75          }
76      }
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.