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