Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/ArithmeticExpressionGrammar.cs @ 6755

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

#1418: Reintegrated branch into trunk.

File size: 3.2 KB
RevLine 
[3253]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3253]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;
[4722]23using HeuristicLab.Common;
[3253]24using HeuristicLab.Core;
[4068]25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[3253]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[5574]27
[3373]28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[3253]29  [StorableClass]
[3294]30  [Item("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
[5686]31  public class ArithmeticExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
[4722]32
33    [StorableConstructor]
34    protected ArithmeticExpressionGrammar(bool deserializing) : base(deserializing) { }
35    protected ArithmeticExpressionGrammar(ArithmeticExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
[3294]36    public ArithmeticExpressionGrammar()
[5688]37      : base(ItemAttribute.GetName(typeof(ArithmeticExpressionGrammar)), ItemAttribute.GetDescription(typeof(ArithmeticExpressionGrammar))) {
[3294]38      Initialize();
[3253]39    }
[4722]40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new ArithmeticExpressionGrammar(this, cloner);
42    }
[3253]43
[3294]44    private void Initialize() {
45      var add = new Addition();
46      var sub = new Subtraction();
47      var mul = new Multiplication();
48      var div = new Division();
49      var constant = new Constant();
[3442]50      constant.MinValue = -20;
51      constant.MaxValue = 20;
[5574]52      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
[3253]53
[3294]54      var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };
55      var functionSymbols = new List<Symbol>() { add, sub, mul, div };
[3373]56
[3338]57      foreach (var symb in allSymbols)
58        AddSymbol(symb);
[3253]59
[3338]60      foreach (var funSymb in functionSymbols) {
[5686]61        SetSubtreeCount(funSymb, 1, 3);
[3338]62      }
[5686]63      SetSubtreeCount(constant, 0, 0);
64      SetSubtreeCount(variableSymbol, 0, 0);
[3253]65
[3338]66      // allow each symbol as child of the start symbol
67      foreach (var symb in allSymbols) {
[5686]68        AddAllowedChildSymbol(StartSymbol, symb);
[5726]69        AddAllowedChildSymbol(DefunSymbol, symb);
[3338]70      }
71
72      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
[5686]73      foreach (var parent in functionSymbols) {
74        foreach (var child in allSymbols)
75          AddAllowedChildSymbol(parent, child);
[3253]76      }
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.