[3253] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[17413] | 23 | using HEAL.Attic;
|
---|
[4722] | 24 | using HeuristicLab.Common;
|
---|
[3253] | 25 | using HeuristicLab.Core;
|
---|
[4068] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[6803] | 27 | using HeuristicLab.PluginInfrastructure;
|
---|
[5574] | 28 |
|
---|
[3373] | 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[6803] | 30 | [NonDiscoverableType]
|
---|
[16565] | 31 | [StorableType("FCBA02B7-5D29-42F5-A64C-A60AD8EA475D")]
|
---|
[3294] | 32 | [Item("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
|
---|
[17413] | 33 | public class ArithmeticExpressionGrammar : DataAnalysisGrammar, ISymbolicDataAnalysisGrammar {
|
---|
[4722] | 34 |
|
---|
| 35 | [StorableConstructor]
|
---|
[16565] | 36 | protected ArithmeticExpressionGrammar(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 37 | protected ArithmeticExpressionGrammar(ArithmeticExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
[3294] | 38 | public ArithmeticExpressionGrammar()
|
---|
[5688] | 39 | : base(ItemAttribute.GetName(typeof(ArithmeticExpressionGrammar)), ItemAttribute.GetDescription(typeof(ArithmeticExpressionGrammar))) {
|
---|
[3294] | 40 | Initialize();
|
---|
[3253] | 41 | }
|
---|
[4722] | 42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 43 | return new ArithmeticExpressionGrammar(this, cloner);
|
---|
| 44 | }
|
---|
[3253] | 45 |
|
---|
[3294] | 46 | private void Initialize() {
|
---|
| 47 | var add = new Addition();
|
---|
| 48 | var sub = new Subtraction();
|
---|
| 49 | var mul = new Multiplication();
|
---|
| 50 | var div = new Division();
|
---|
[18132] | 51 | var number = new Number();
|
---|
| 52 | number.MinValue = -20;
|
---|
| 53 | number.MaxValue = 20;
|
---|
[3294] | 54 | var constant = new Constant();
|
---|
[18132] | 55 | constant.Enabled = false;
|
---|
[5574] | 56 | var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
|
---|
[14826] | 57 | var binFactorVariableSymbol = new BinaryFactorVariable();
|
---|
| 58 | var factorVariableSymbol = new FactorVariable();
|
---|
[3253] | 59 |
|
---|
[18132] | 60 | var allSymbols = new List<Symbol>() { add, sub, mul, div, number, constant, variableSymbol, binFactorVariableSymbol, factorVariableSymbol };
|
---|
[3294] | 61 | var functionSymbols = new List<Symbol>() { add, sub, mul, div };
|
---|
[3373] | 62 |
|
---|
[3338] | 63 | foreach (var symb in allSymbols)
|
---|
| 64 | AddSymbol(symb);
|
---|
[3253] | 65 |
|
---|
[3338] | 66 | foreach (var funSymb in functionSymbols) {
|
---|
[5686] | 67 | SetSubtreeCount(funSymb, 1, 3);
|
---|
[3338] | 68 | }
|
---|
[18132] | 69 | SetSubtreeCount(number, 0, 0);
|
---|
[5686] | 70 | SetSubtreeCount(constant, 0, 0);
|
---|
| 71 | SetSubtreeCount(variableSymbol, 0, 0);
|
---|
[14826] | 72 | SetSubtreeCount(binFactorVariableSymbol, 0, 0);
|
---|
| 73 | SetSubtreeCount(factorVariableSymbol, 0, 0);
|
---|
[3253] | 74 |
|
---|
[3338] | 75 | // allow each symbol as child of the start symbol
|
---|
| 76 | foreach (var symb in allSymbols) {
|
---|
[5686] | 77 | AddAllowedChildSymbol(StartSymbol, symb);
|
---|
[5726] | 78 | AddAllowedChildSymbol(DefunSymbol, symb);
|
---|
[3338] | 79 | }
|
---|
| 80 |
|
---|
| 81 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
[5686] | 82 | foreach (var parent in functionSymbols) {
|
---|
| 83 | foreach (var child in allSymbols)
|
---|
| 84 | AddAllowedChildSymbol(parent, child);
|
---|
[3253] | 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|