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 System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
31 | [StorableClass]
|
---|
32 | [Item("TypeCoherentExpressionGrammar", "Represents a grammar for functional expressions in which special syntactic constraints are enforced so that boolean and real-valued expressions are not mixed.")]
|
---|
33 | public class TypeCoherentExpressionGrammar : DefaultSymbolicExpressionGrammar {
|
---|
34 |
|
---|
35 | [StorableConstructor]
|
---|
36 | protected TypeCoherentExpressionGrammar(bool deserializing) : base(deserializing) { }
|
---|
37 | protected TypeCoherentExpressionGrammar(TypeCoherentExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
38 | public TypeCoherentExpressionGrammar()
|
---|
39 | : base() {
|
---|
40 | Initialize();
|
---|
41 | }
|
---|
42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
43 | return new TypeCoherentExpressionGrammar(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 mean = new Average();
|
---|
52 | var sin = new Sine();
|
---|
53 | var cos = new Cosine();
|
---|
54 | var tan = new Tangent();
|
---|
55 | var log = new Logarithm();
|
---|
56 | var pow = new Power();
|
---|
57 | pow.InitialFrequency = 0.0;
|
---|
58 | var root = new Root();
|
---|
59 | root.InitialFrequency = 0.0;
|
---|
60 | var exp = new Exponential();
|
---|
61 | var @if = new IfThenElse();
|
---|
62 | var gt = new GreaterThan();
|
---|
63 | var lt = new LessThan();
|
---|
64 | var and = new And();
|
---|
65 | var or = new Or();
|
---|
66 | var not = new Not();
|
---|
67 |
|
---|
68 | var timeLag = new TimeLag();
|
---|
69 | timeLag.InitialFrequency = 0.0;
|
---|
70 | var integral = new Integral();
|
---|
71 | integral.InitialFrequency = 0.0;
|
---|
72 | var derivative = new Derivative();
|
---|
73 | derivative.InitialFrequency = 0.0;
|
---|
74 | var variableCondition = new VariableCondition();
|
---|
75 | variableCondition.InitialFrequency = 0.0;
|
---|
76 |
|
---|
77 | var constant = new Constant();
|
---|
78 | constant.MinValue = -20;
|
---|
79 | constant.MaxValue = 20;
|
---|
80 | var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
|
---|
81 | var laggedVariable = new LaggedVariable();
|
---|
82 |
|
---|
83 | laggedVariable.InitialFrequency = 0.0;
|
---|
84 | mean.InitialFrequency = 0.0;
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Start = RealValueExpression
|
---|
88 | *
|
---|
89 | * RealValueExpression =
|
---|
90 | * "Variable" |
|
---|
91 | * "Constant" |
|
---|
92 | * BinaryOperator RealValueExpression RealValueExpression |
|
---|
93 | * UnaryOperator RealValueExpression |
|
---|
94 | * "IF" BooleanExpression RealValueExpression RealValueExpression |
|
---|
95 | * "VariableCondition" RealValueExpression RealValueExpression
|
---|
96 | *
|
---|
97 | * BinaryOperator =
|
---|
98 | * "+" | "-" | "*" | "/" | "Power"
|
---|
99 | *
|
---|
100 | * UnaryOperator =
|
---|
101 | * "Sin" | "Cos" | "Tan" | "Log" | "Exp"
|
---|
102 | *
|
---|
103 | * BooleanExpression =
|
---|
104 | * "AND" BooleanExpression BooleanExpression |
|
---|
105 | * "OR" BooleanExpression BooleanExpression |
|
---|
106 | * "NOT" BooleanExpression |
|
---|
107 | * ">" RealValueExpression RealValueExpression |
|
---|
108 | * "<" RealValueExpression RealValueExpression
|
---|
109 | */
|
---|
110 |
|
---|
111 | 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 };
|
---|
112 |
|
---|
113 | var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, timeLag, integral, derivative };
|
---|
114 | var binaryFunctionSymbols = new List<Symbol>() { add, sub, mul, div, mean, pow, root, variableCondition };
|
---|
115 |
|
---|
116 | var unaryBooleanFunctionSymbols = new List<Symbol>() { not };
|
---|
117 | var binaryBooleanFunctionSymbols = new List<Symbol>() { or, and };
|
---|
118 | var relationalFunctionSymbols = new List<Symbol>() { gt, lt };
|
---|
119 | var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
|
---|
120 | var realValuedSymbols = unaryFunctionSymbols.Concat(binaryFunctionSymbols).Concat(terminalSymbols).Concat(new List<Symbol>() { @if });
|
---|
121 | var booleanSymbols = unaryBooleanFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols);
|
---|
122 |
|
---|
123 | foreach (var symb in allSymbols)
|
---|
124 | AddSymbol(symb);
|
---|
125 |
|
---|
126 | foreach (var unaryFun in unaryFunctionSymbols.Concat(unaryBooleanFunctionSymbols)) {
|
---|
127 | SetMinSubtreeCount(unaryFun, 1);
|
---|
128 | SetMaxSubtreeCount(unaryFun, 1);
|
---|
129 | }
|
---|
130 | foreach (var binaryFun in binaryFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols)) {
|
---|
131 | SetMinSubtreeCount(binaryFun, 2);
|
---|
132 | SetMaxSubtreeCount(binaryFun, 2);
|
---|
133 | }
|
---|
134 |
|
---|
135 | foreach (var terminalSymbol in terminalSymbols) {
|
---|
136 | SetMinSubtreeCount(terminalSymbol, 0);
|
---|
137 | SetMaxSubtreeCount(terminalSymbol, 0);
|
---|
138 | }
|
---|
139 |
|
---|
140 | SetMinSubtreeCount(@if, 3);
|
---|
141 | SetMaxSubtreeCount(@if, 3);
|
---|
142 |
|
---|
143 |
|
---|
144 | // allow only real-valued expressions as child of the start symbol
|
---|
145 | foreach (var symb in realValuedSymbols) {
|
---|
146 | SetAllowedChild(StartSymbol, symb, 0);
|
---|
147 | }
|
---|
148 |
|
---|
149 | foreach (var symb in unaryFunctionSymbols) {
|
---|
150 | foreach (var childSymb in realValuedSymbols) {
|
---|
151 | SetAllowedChild(symb, childSymb, 0);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | foreach (var symb in binaryFunctionSymbols) {
|
---|
156 | foreach (var childSymb in realValuedSymbols) {
|
---|
157 | SetAllowedChild(symb, childSymb, 0);
|
---|
158 | SetAllowedChild(symb, childSymb, 1);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | foreach (var childSymb in booleanSymbols) {
|
---|
163 | SetAllowedChild(@if, childSymb, 0);
|
---|
164 | }
|
---|
165 | foreach (var childSymb in realValuedSymbols) {
|
---|
166 | SetAllowedChild(@if, childSymb, 1);
|
---|
167 | SetAllowedChild(@if, childSymb, 2);
|
---|
168 | }
|
---|
169 |
|
---|
170 | foreach (var symb in relationalFunctionSymbols) {
|
---|
171 | foreach (var childSymb in realValuedSymbols) {
|
---|
172 | SetAllowedChild(symb, childSymb, 0);
|
---|
173 | SetAllowedChild(symb, childSymb, 1);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | foreach (var symb in binaryBooleanFunctionSymbols) {
|
---|
177 | foreach (var childSymb in booleanSymbols) {
|
---|
178 | SetAllowedChild(symb, childSymb, 0);
|
---|
179 | SetAllowedChild(symb, childSymb, 1);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | foreach (var symb in unaryBooleanFunctionSymbols) {
|
---|
183 | foreach (var childSymb in booleanSymbols) {
|
---|
184 | SetAllowedChild(symb, childSymb, 0);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|