1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
29 | [StorableClass]
|
---|
30 | [Item("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
|
---|
31 | public class ArithmeticExpressionGrammar : DefaultSymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
|
---|
32 |
|
---|
33 | [StorableConstructor]
|
---|
34 | protected ArithmeticExpressionGrammar(bool deserializing) : base(deserializing) { }
|
---|
35 | protected ArithmeticExpressionGrammar(ArithmeticExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
36 | public ArithmeticExpressionGrammar()
|
---|
37 | : base() {
|
---|
38 | Initialize();
|
---|
39 | }
|
---|
40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
41 | return new ArithmeticExpressionGrammar(this, cloner);
|
---|
42 | }
|
---|
43 |
|
---|
44 | private void Initialize() {
|
---|
45 | var add = new Addition();
|
---|
46 | var sub = new Subtraction();
|
---|
47 | var mul = new Multiplication();
|
---|
48 | var div = new Division();
|
---|
49 | var constant = new Constant();
|
---|
50 | constant.MinValue = -20;
|
---|
51 | constant.MaxValue = 20;
|
---|
52 | var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
|
---|
53 |
|
---|
54 | var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };
|
---|
55 | var functionSymbols = new List<Symbol>() { add, sub, mul, div };
|
---|
56 |
|
---|
57 | foreach (var symb in allSymbols)
|
---|
58 | AddSymbol(symb);
|
---|
59 |
|
---|
60 | foreach (var funSymb in functionSymbols) {
|
---|
61 | SetMinSubtreeCount(funSymb, 1);
|
---|
62 | SetMaxSubtreeCount(funSymb, 3);
|
---|
63 | }
|
---|
64 | SetMinSubtreeCount(constant, 0);
|
---|
65 | SetMaxSubtreeCount(constant, 0);
|
---|
66 | SetMinSubtreeCount(variableSymbol, 0);
|
---|
67 | SetMaxSubtreeCount(variableSymbol, 0);
|
---|
68 |
|
---|
69 | // allow each symbol as child of the start symbol
|
---|
70 | foreach (var symb in allSymbols) {
|
---|
71 | SetAllowedChild(StartSymbol, symb, 0);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
75 | foreach (var parent in allSymbols) {
|
---|
76 | for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
|
---|
77 | foreach (var child in allSymbols) {
|
---|
78 | SetAllowedChild(parent, child, i);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|