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("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
|
---|
31 | public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
|
---|
32 | [StorableConstructor]
|
---|
33 | protected FullFunctionalExpressionGrammar(bool deserializing) : base(deserializing) { }
|
---|
34 | protected FullFunctionalExpressionGrammar(FullFunctionalExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
35 | public FullFunctionalExpressionGrammar()
|
---|
36 | : base() {
|
---|
37 | Initialize();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
41 | return new FullFunctionalExpressionGrammar(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 mean = new Average();
|
---|
50 | var sin = new Sine();
|
---|
51 | var cos = new Cosine();
|
---|
52 | var tan = new Tangent();
|
---|
53 | var log = new Logarithm();
|
---|
54 | var pow = new Power();
|
---|
55 | pow.InitialFrequency = 0.0;
|
---|
56 | var root = new Root();
|
---|
57 | root.InitialFrequency = 0.0;
|
---|
58 | var exp = new Exponential();
|
---|
59 | var @if = new IfThenElse();
|
---|
60 | var gt = new GreaterThan();
|
---|
61 | var lt = new LessThan();
|
---|
62 | var and = new And();
|
---|
63 | var or = new Or();
|
---|
64 | var not = new Not();
|
---|
65 |
|
---|
66 | var timeLag = new TimeLag();
|
---|
67 | timeLag.InitialFrequency = 0.0;
|
---|
68 | var integral = new Integral();
|
---|
69 | integral.InitialFrequency = 0.0;
|
---|
70 | var derivative = new Derivative();
|
---|
71 | derivative.InitialFrequency = 0.0;
|
---|
72 |
|
---|
73 | var variableCondition = new VariableCondition();
|
---|
74 | variableCondition.InitialFrequency = 0.0;
|
---|
75 |
|
---|
76 | var constant = new Constant();
|
---|
77 | constant.MinValue = -20;
|
---|
78 | constant.MaxValue = 20;
|
---|
79 | var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
|
---|
80 | var laggedVariable = new LaggedVariable();
|
---|
81 | laggedVariable.InitialFrequency = 0.0;
|
---|
82 |
|
---|
83 | var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, pow, root, exp, @if, gt, lt, and, or, not, timeLag, integral, derivative, constant, variableSymbol, laggedVariable, variableCondition };
|
---|
84 | var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not, timeLag, integral, derivative };
|
---|
85 |
|
---|
86 | var binaryFunctionSymbols = new List<Symbol>() { pow, root, gt, lt, variableCondition };
|
---|
87 | var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
|
---|
88 | var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
|
---|
89 |
|
---|
90 | foreach (var symb in allSymbols)
|
---|
91 | AddSymbol(symb);
|
---|
92 |
|
---|
93 | foreach (var funSymb in functionSymbols) {
|
---|
94 | SetMinSubtreeCount(funSymb, 1);
|
---|
95 | SetMaxSubtreeCount(funSymb, 3);
|
---|
96 | }
|
---|
97 | foreach (var funSymb in unaryFunctionSymbols) {
|
---|
98 | SetMinSubtreeCount(funSymb, 1);
|
---|
99 | SetMaxSubtreeCount(funSymb, 1);
|
---|
100 | }
|
---|
101 | foreach (var funSymb in binaryFunctionSymbols) {
|
---|
102 | SetMinSubtreeCount(funSymb, 2);
|
---|
103 | SetMaxSubtreeCount(funSymb, 2);
|
---|
104 | }
|
---|
105 |
|
---|
106 | foreach (var terminalSymbol in terminalSymbols) {
|
---|
107 | SetMinSubtreeCount(terminalSymbol, 0);
|
---|
108 | SetMaxSubtreeCount(terminalSymbol, 0);
|
---|
109 | }
|
---|
110 |
|
---|
111 | SetMinSubtreeCount(@if, 3);
|
---|
112 | SetMaxSubtreeCount(@if, 3);
|
---|
113 |
|
---|
114 |
|
---|
115 | // allow each symbol as child of the start symbol
|
---|
116 | foreach (var symb in allSymbols) {
|
---|
117 | SetAllowedChild(StartSymbol, symb, 0);
|
---|
118 | }
|
---|
119 |
|
---|
120 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
121 | foreach (var parent in allSymbols) {
|
---|
122 | for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
|
---|
123 | foreach (var child in allSymbols) {
|
---|
124 | SetAllowedChild(parent, child, i);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|