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.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
29 | [StorableClass]
|
---|
30 | [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.")]
|
---|
31 | public class TypeCoherentExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
|
---|
32 |
|
---|
33 | [StorableConstructor]
|
---|
34 | protected TypeCoherentExpressionGrammar(bool deserializing) : base(deserializing) { }
|
---|
35 | protected TypeCoherentExpressionGrammar(TypeCoherentExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
36 | public TypeCoherentExpressionGrammar()
|
---|
37 | : base(ItemAttribute.GetName(typeof(TypeCoherentExpressionGrammar)), ItemAttribute.GetDescription(typeof(TypeCoherentExpressionGrammar))) {
|
---|
38 | Initialize();
|
---|
39 | }
|
---|
40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
41 | return new TypeCoherentExpressionGrammar(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 | var variableCondition = new VariableCondition();
|
---|
73 | variableCondition.InitialFrequency = 0.0;
|
---|
74 |
|
---|
75 | var constant = new Constant();
|
---|
76 | constant.MinValue = -20;
|
---|
77 | constant.MaxValue = 20;
|
---|
78 | var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
|
---|
79 | var laggedVariable = new LaggedVariable();
|
---|
80 |
|
---|
81 | laggedVariable.InitialFrequency = 0.0;
|
---|
82 | mean.InitialFrequency = 0.0;
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * Start = RealValueExpression
|
---|
86 | *
|
---|
87 | * RealValueExpression =
|
---|
88 | * "Variable" |
|
---|
89 | * "Constant" |
|
---|
90 | * BinaryOperator RealValueExpression RealValueExpression |
|
---|
91 | * UnaryOperator RealValueExpression |
|
---|
92 | * "IF" BooleanExpression RealValueExpression RealValueExpression |
|
---|
93 | * "VariableCondition" RealValueExpression RealValueExpression
|
---|
94 | *
|
---|
95 | * BinaryOperator =
|
---|
96 | * "+" | "-" | "*" | "/" | "Power"
|
---|
97 | *
|
---|
98 | * UnaryOperator =
|
---|
99 | * "Sin" | "Cos" | "Tan" | "Log" | "Exp"
|
---|
100 | *
|
---|
101 | * BooleanExpression =
|
---|
102 | * "AND" BooleanExpression BooleanExpression |
|
---|
103 | * "OR" BooleanExpression BooleanExpression |
|
---|
104 | * "NOT" BooleanExpression |
|
---|
105 | * ">" RealValueExpression RealValueExpression |
|
---|
106 | * "<" RealValueExpression RealValueExpression
|
---|
107 | */
|
---|
108 |
|
---|
109 | 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 };
|
---|
110 |
|
---|
111 | var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, timeLag, integral, derivative };
|
---|
112 | var binaryFunctionSymbols = new List<Symbol>() { add, sub, mul, div, mean, pow, root, variableCondition };
|
---|
113 |
|
---|
114 | var unaryBooleanFunctionSymbols = new List<Symbol>() { not };
|
---|
115 | var binaryBooleanFunctionSymbols = new List<Symbol>() { or, and };
|
---|
116 | var relationalFunctionSymbols = new List<Symbol>() { gt, lt };
|
---|
117 | var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable };
|
---|
118 | var realValuedSymbols = unaryFunctionSymbols.Concat(binaryFunctionSymbols).Concat(terminalSymbols).Concat(new List<Symbol>() { @if });
|
---|
119 | var booleanSymbols = unaryBooleanFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols);
|
---|
120 |
|
---|
121 | foreach (var symb in allSymbols)
|
---|
122 | AddSymbol(symb);
|
---|
123 |
|
---|
124 | foreach (var unaryFun in unaryFunctionSymbols.Concat(unaryBooleanFunctionSymbols)) {
|
---|
125 | SetSubtreeCount(unaryFun, 1, 1);
|
---|
126 | }
|
---|
127 | foreach (var binaryFun in binaryFunctionSymbols.Concat(binaryBooleanFunctionSymbols).Concat(relationalFunctionSymbols)) {
|
---|
128 | SetSubtreeCount(binaryFun, 2, 2);
|
---|
129 | }
|
---|
130 |
|
---|
131 | foreach (var terminalSymbol in terminalSymbols) {
|
---|
132 | SetSubtreeCount(terminalSymbol, 0, 0);
|
---|
133 | }
|
---|
134 |
|
---|
135 | SetSubtreeCount(@if, 3, 3);
|
---|
136 |
|
---|
137 |
|
---|
138 | // allow only real-valued expressions as child of the start symbol
|
---|
139 | foreach (var symb in realValuedSymbols) {
|
---|
140 | AddAllowedChildSymbol(StartSymbol, symb);
|
---|
141 | AddAllowedChildSymbol(DefunSymbol, symb);
|
---|
142 | }
|
---|
143 |
|
---|
144 | foreach (var symb in unaryFunctionSymbols) {
|
---|
145 | foreach (var childSymb in realValuedSymbols) {
|
---|
146 | AddAllowedChildSymbol(symb, childSymb);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | foreach (var symb in binaryFunctionSymbols) {
|
---|
151 | foreach (var childSymb in realValuedSymbols) {
|
---|
152 | AddAllowedChildSymbol(symb, childSymb);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | foreach (var childSymb in booleanSymbols) {
|
---|
157 | AddAllowedChildSymbol(@if, childSymb, 0);
|
---|
158 | }
|
---|
159 | foreach (var childSymb in realValuedSymbols) {
|
---|
160 | AddAllowedChildSymbol(@if, childSymb, 1);
|
---|
161 | AddAllowedChildSymbol(@if, childSymb, 2);
|
---|
162 | }
|
---|
163 |
|
---|
164 | foreach (var symb in relationalFunctionSymbols) {
|
---|
165 | foreach (var childSymb in realValuedSymbols) {
|
---|
166 | AddAllowedChildSymbol(symb, childSymb);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | foreach (var symb in binaryBooleanFunctionSymbols) {
|
---|
170 | foreach (var childSymb in booleanSymbols) {
|
---|
171 | AddAllowedChildSymbol(symb, childSymb);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | foreach (var symb in unaryBooleanFunctionSymbols) {
|
---|
175 | foreach (var childSymb in booleanSymbols) {
|
---|
176 | AddAllowedChildSymbol(symb, childSymb);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|