Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6433 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 3.3 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;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
[3253]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[3373]28using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[3253]30  [StorableClass]
[3294]31  [Item("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
32  public class ArithmeticExpressionGrammar : DefaultSymbolicExpressionGrammar {
[4722]33
34    [StorableConstructor]
35    protected ArithmeticExpressionGrammar(bool deserializing) : base(deserializing) { }
36    protected ArithmeticExpressionGrammar(ArithmeticExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
[3294]37    public ArithmeticExpressionGrammar()
[3338]38      : base() {
[3294]39      Initialize();
[3253]40    }
[4722]41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new ArithmeticExpressionGrammar(this, cloner);
43    }
[3253]44
[3294]45    private void Initialize() {
46      var add = new Addition();
47      var sub = new Subtraction();
48      var mul = new Multiplication();
49      var div = new Division();
50      var constant = new Constant();
[3442]51      constant.MinValue = -20;
52      constant.MaxValue = 20;
[4249]53      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
[3253]54
[3294]55      var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };
56      var functionSymbols = new List<Symbol>() { add, sub, mul, div };
[3373]57
[3338]58      foreach (var symb in allSymbols)
59        AddSymbol(symb);
[3253]60
[3338]61      foreach (var funSymb in functionSymbols) {
62        SetMinSubtreeCount(funSymb, 1);
63        SetMaxSubtreeCount(funSymb, 3);
64      }
65      SetMinSubtreeCount(constant, 0);
66      SetMaxSubtreeCount(constant, 0);
67      SetMinSubtreeCount(variableSymbol, 0);
68      SetMaxSubtreeCount(variableSymbol, 0);
[3253]69
[3338]70      // allow each symbol as child of the start symbol
71      foreach (var symb in allSymbols) {
72        SetAllowedChild(StartSymbol, symb, 0);
73      }
74
75      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
76      foreach (var parent in allSymbols) {
77        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
78          foreach (var child in allSymbols) {
79            SetAllowedChild(parent, child, i);
[3294]80          }
[3253]81      }
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.