[4193] | 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 System.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
| 28 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 29 | [StorableClass]
|
---|
| 30 | [Item("BasicExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
|
---|
| 31 | public class BasicExpressionGrammar : DefaultSymbolicExpressionGrammar {
|
---|
| 32 | [Storable]
|
---|
| 33 | private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
|
---|
| 34 |
|
---|
| 35 | public BasicExpressionGrammar()
|
---|
| 36 | : base() {
|
---|
| 37 | Initialize();
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | [StorableConstructor]
|
---|
| 41 | protected BasicExpressionGrammar(bool deserializing) : base(deserializing) { }
|
---|
| 42 |
|
---|
| 43 | private void Initialize() {
|
---|
| 44 | var add = new Addition();
|
---|
| 45 | var sub = new Subtraction();
|
---|
| 46 | var mul = new Multiplication();
|
---|
| 47 | var div = new Division();
|
---|
| 48 | var constant = new Constant();
|
---|
| 49 | var log = new Logarithm();
|
---|
| 50 | var exp = new Exponential();
|
---|
| 51 |
|
---|
| 52 | constant.MinValue = -20;
|
---|
| 53 | constant.MaxValue = 20;
|
---|
| 54 | variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
|
---|
| 55 |
|
---|
| 56 | var allSymbols = new List<Symbol>() { add, sub, mul, div, log, exp, constant, variableSymbol };
|
---|
| 57 | var arithSymbols = new List<Symbol>() { add, sub, mul, div };
|
---|
| 58 | var funSymbols = new List<Symbol>() { log, exp };
|
---|
| 59 |
|
---|
| 60 | foreach (var symb in allSymbols)
|
---|
| 61 | AddSymbol(symb);
|
---|
| 62 |
|
---|
| 63 | foreach (var funSymb in arithSymbols) {
|
---|
| 64 | SetMinSubtreeCount(funSymb, 2);
|
---|
| 65 | SetMaxSubtreeCount(funSymb, 2);
|
---|
| 66 | }
|
---|
| 67 | foreach (var funSymb in funSymbols) {
|
---|
| 68 | SetMinSubtreeCount(funSymb, 1);
|
---|
| 69 | SetMaxSubtreeCount(funSymb, 1);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | SetMinSubtreeCount(constant, 0);
|
---|
| 73 | SetMaxSubtreeCount(constant, 0);
|
---|
| 74 | SetMinSubtreeCount(variableSymbol, 0);
|
---|
| 75 | SetMaxSubtreeCount(variableSymbol, 0);
|
---|
| 76 |
|
---|
| 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);
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|