Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3338 was 3338, checked in by gkronber, 12 years ago

Fixed bugs related to dynamic symbol constraints with ADFs. #290 (Implement ADFs)

File size: 3.4 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
22
23using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
24using System.Collections.Generic;
25using System;
26using System.Linq;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
30using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols;
31using HeuristicLab.Data;
32namespace HeuristicLab.Problems.DataAnalysis.Regression.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 List<string> variableNames = new List<string>();
38    public IEnumerable<string> VariableNames {
39      get { return variableNames; }
40      set {
41        variableNames = new List<string>(value);
42        variableSymbol.VariableNames = variableNames;
43      }
44    }
45
46    [Storable]
47    private HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols.Variable variableSymbol;
48
49    public ArithmeticExpressionGrammar()
50      : base() {
51      Initialize();
52    }
53
54    private void Initialize() {
55      var add = new Addition();
56      var sub = new Subtraction();
57      var mul = new Multiplication();
58      var div = new Division();
59      var constant = new Constant();
60      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols.Variable();
61
62      var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };
63      var functionSymbols = new List<Symbol>() { add, sub, mul, div };
64      foreach (var symb in allSymbols)
65        AddSymbol(symb);
66
67      foreach (var funSymb in functionSymbols) {
68        SetMinSubtreeCount(funSymb, 1);
69        SetMaxSubtreeCount(funSymb, 3);
70      }
71      SetMinSubtreeCount(constant, 0);
72      SetMaxSubtreeCount(constant, 0);
73      SetMinSubtreeCount(variableSymbol, 0);
74      SetMaxSubtreeCount(variableSymbol, 0);
75
76      // allow each symbol as child of the start symbol
77      foreach (var symb in allSymbols) {
78        SetAllowedChild(StartSymbol, symb, 0);
79      }
80
81      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
82      foreach (var parent in allSymbols) {
83        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
84          foreach (var child in allSymbols) {
85            SetAllowedChild(parent, child, i);
86          }
87      }
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.