Free cookie consent management tool by TermsFeed Policy Generator

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

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