1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.ExternalEvaluation.GP {
|
---|
30 | [StorableClass]
|
---|
31 | [Item("ExternalEvaluationExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
|
---|
32 | public class ExternalEvaluationExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
|
---|
33 | [Storable]
|
---|
34 | private HeuristicLab.Problems.DataAnalysis.Symbolic.Variable variableSymbol;
|
---|
35 | [StorableConstructor]
|
---|
36 | protected ExternalEvaluationExpressionGrammar(bool deserializing) : base(deserializing) { }
|
---|
37 | protected ExternalEvaluationExpressionGrammar(ExternalEvaluationExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
39 | return new ExternalEvaluationExpressionGrammar(this, cloner);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public ExternalEvaluationExpressionGrammar()
|
---|
43 | : base("ExternalEvaluationExpressionGrammar", "Represents a grammar for functional expressions using all available functions.") {
|
---|
44 | Initialize();
|
---|
45 | }
|
---|
46 |
|
---|
47 | private void Initialize() {
|
---|
48 | var add = new Addition();
|
---|
49 | var sub = new Subtraction();
|
---|
50 | var mul = new Multiplication();
|
---|
51 | var div = new Division();
|
---|
52 | var mean = new Average();
|
---|
53 | var sin = new Sine();
|
---|
54 | var cos = new Cosine();
|
---|
55 | var tan = new Tangent();
|
---|
56 | var log = new Logarithm();
|
---|
57 | var exp = new Exponential();
|
---|
58 | var @if = new IfThenElse();
|
---|
59 | var gt = new GreaterThan();
|
---|
60 | var lt = new LessThan();
|
---|
61 | var and = new And();
|
---|
62 | var or = new Or();
|
---|
63 | var not = new Not();
|
---|
64 | var constant = new Constant();
|
---|
65 | constant.MinValue = -20;
|
---|
66 | constant.MaxValue = 20;
|
---|
67 | variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
|
---|
68 |
|
---|
69 | var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };
|
---|
70 | var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
|
---|
71 | var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
|
---|
72 | var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
|
---|
73 |
|
---|
74 | foreach (var symb in allSymbols)
|
---|
75 | AddSymbol(symb);
|
---|
76 |
|
---|
77 | foreach (var funSymb in functionSymbols) {
|
---|
78 | SetSubtreeCount(funSymb, 1, 3);
|
---|
79 | }
|
---|
80 | foreach (var funSymb in unaryFunctionSymbols) {
|
---|
81 | SetSubtreeCount(funSymb, 1, 1);
|
---|
82 | }
|
---|
83 | foreach (var funSymb in binaryFunctionSymbols) {
|
---|
84 | SetSubtreeCount(funSymb, 2, 2);
|
---|
85 | }
|
---|
86 |
|
---|
87 | SetSubtreeCount(@if, 3, 3);
|
---|
88 | SetSubtreeCount(constant, 0, 0);
|
---|
89 | SetSubtreeCount(variableSymbol, 0, 0);
|
---|
90 |
|
---|
91 | // allow each symbol as child of the start symbol
|
---|
92 | foreach (var symb in allSymbols) {
|
---|
93 | AddAllowedChildSymbol(StartSymbol, symb, 0);
|
---|
94 | }
|
---|
95 |
|
---|
96 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
97 | foreach (var parent in allSymbols) {
|
---|
98 | for (int i = 0; i < GetMaximumSubtreeCount(parent); i++)
|
---|
99 | foreach (var child in allSymbols) {
|
---|
100 | AddAllowedChildSymbol(parent, child, i);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|