[3253] | 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 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System;
|
---|
| 25 | using System.Linq;
|
---|
[3376] | 26 | using HeuristicLab.Common;
|
---|
[3253] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3257] | 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
|
---|
[3269] | 30 | using HeuristicLab.Data;
|
---|
[3373] | 31 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[3253] | 33 | [StorableClass]
|
---|
[3294] | 34 | [Item("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
|
---|
| 35 | public class ArithmeticExpressionGrammar : DefaultSymbolicExpressionGrammar {
|
---|
| 36 | [Storable]
|
---|
[3269] | 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);
|
---|
[3294] | 42 | variableSymbol.VariableNames = variableNames;
|
---|
[3269] | 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[3253] | 46 | [Storable]
|
---|
[3373] | 47 | private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
|
---|
[3253] | 48 |
|
---|
[3294] | 49 | public ArithmeticExpressionGrammar()
|
---|
[3338] | 50 | : base() {
|
---|
[3294] | 51 | Initialize();
|
---|
[3253] | 52 | }
|
---|
| 53 |
|
---|
[3294] | 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();
|
---|
[3373] | 60 | variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
|
---|
[3253] | 61 |
|
---|
[3294] | 62 | var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };
|
---|
| 63 | var functionSymbols = new List<Symbol>() { add, sub, mul, div };
|
---|
[3373] | 64 |
|
---|
[3338] | 65 | foreach (var symb in allSymbols)
|
---|
| 66 | AddSymbol(symb);
|
---|
[3253] | 67 |
|
---|
[3338] | 68 | foreach (var funSymb in functionSymbols) {
|
---|
| 69 | SetMinSubtreeCount(funSymb, 1);
|
---|
| 70 | SetMaxSubtreeCount(funSymb, 3);
|
---|
| 71 | }
|
---|
| 72 | SetMinSubtreeCount(constant, 0);
|
---|
| 73 | SetMaxSubtreeCount(constant, 0);
|
---|
| 74 | SetMinSubtreeCount(variableSymbol, 0);
|
---|
| 75 | SetMaxSubtreeCount(variableSymbol, 0);
|
---|
[3253] | 76 |
|
---|
[3338] | 77 | // allow each symbol as child of the start symbol
|
---|
| 78 | foreach (var symb in allSymbols) {
|
---|
| 79 | SetAllowedChild(StartSymbol, symb, 0);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
| 83 | foreach (var parent in allSymbols) {
|
---|
| 84 | for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
|
---|
| 85 | foreach (var child in allSymbols) {
|
---|
| 86 | SetAllowedChild(parent, child, i);
|
---|
[3294] | 87 | }
|
---|
[3253] | 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|