1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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;
|
---|
23 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
24 | using HeuristicLab.Problems.DataAnalysis.Transformations;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
27 | public class TransformationToSymbolicTreeMapper : ITransformationMapper<ISymbolicExpressionTreeNode> {
|
---|
28 | private ITransformation transformation;
|
---|
29 | private string column;
|
---|
30 |
|
---|
31 | #region ITransformationMapper<ISymbolicExpressionTree> Members
|
---|
32 |
|
---|
33 | public ISymbolicExpressionTreeNode GenerateModel(ITransformation transformation) {
|
---|
34 | InitComponents(transformation);
|
---|
35 |
|
---|
36 | if (transformation is LinearTransformation) {
|
---|
37 | return GenerateModelForLinearTransformation();
|
---|
38 | } else if (transformation is ExponentialTransformation) {
|
---|
39 | return GenerateModelForExponentialTransformation();
|
---|
40 | } else if (transformation is LogarithmicTransformation) {
|
---|
41 | return GenerateModelForLogarithmicTransformation();
|
---|
42 | } else if (transformation is PowerTransformation) {
|
---|
43 | return GenerateModelForPowerTransformation();
|
---|
44 | } else if (transformation is ReciprocalTransformation) {
|
---|
45 | return GenerateModelForReciprocalTransformation();
|
---|
46 | } else if (transformation is ShiftStandardDistributionTransformation) {
|
---|
47 | return GenerateModelForShiftStandardDistributionTransformation();
|
---|
48 | }
|
---|
49 | throw new NotImplementedException();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public ISymbolicExpressionTreeNode GenerateInverseModel(ITransformation transformation) {
|
---|
53 | InitComponents(transformation);
|
---|
54 |
|
---|
55 | if (transformation is LinearTransformation) {
|
---|
56 | return GenerateInverseModelForLinearTransformation();
|
---|
57 | } else if (transformation is ExponentialTransformation) {
|
---|
58 | return GenerateInverseModelForExponentialTransformation();
|
---|
59 | } else if (transformation is LogarithmicTransformation) {
|
---|
60 | return GenerateInverseModelForLogarithmicTransformation();
|
---|
61 | } else if (transformation is PowerTransformation) {
|
---|
62 | return GenerateInverseModelForPowerTransformation();
|
---|
63 | } else if (transformation is ReciprocalTransformation) {
|
---|
64 | return GenerateInverseModelForReciprocalTransformation();
|
---|
65 | } else if (transformation is ShiftStandardDistributionTransformation) {
|
---|
66 | GenerateInverseModelForShiftStandardDistributionTransformation();
|
---|
67 | }
|
---|
68 |
|
---|
69 | throw new NotImplementedException();
|
---|
70 | }
|
---|
71 |
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | // helper
|
---|
75 |
|
---|
76 | private ISymbolicExpressionTreeNode GenerateModelForLinearTransformation() {
|
---|
77 | var linearTransformation = (LinearTransformation)transformation;
|
---|
78 | var kValue = linearTransformation.Multiplier;
|
---|
79 | var dValue = linearTransformation.Addend;
|
---|
80 |
|
---|
81 | // k * x
|
---|
82 | var multiplicationNode = new Multiplication().CreateTreeNode();
|
---|
83 | var kNode = CreateConstantTreeNode("k", kValue);
|
---|
84 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
85 | multiplicationNode.AddSubtree(kNode);
|
---|
86 | multiplicationNode.AddSubtree(xNode);
|
---|
87 |
|
---|
88 | // ( k * x ) + d
|
---|
89 | var additionNode = new Addition().CreateTreeNode();
|
---|
90 | var dNode = CreateConstantTreeNode("d", dValue);
|
---|
91 | additionNode.AddSubtree(multiplicationNode);
|
---|
92 | additionNode.AddSubtree(dNode);
|
---|
93 |
|
---|
94 | return additionNode;
|
---|
95 | }
|
---|
96 |
|
---|
97 | private ISymbolicExpressionTreeNode GenerateInverseModelForLinearTransformation() {
|
---|
98 | var linearTransformation = (LinearTransformation)transformation;
|
---|
99 | var kValue = linearTransformation.Multiplier;
|
---|
100 | var dValue = linearTransformation.Addend;
|
---|
101 |
|
---|
102 | // x - d
|
---|
103 | var substractionNode = new Subtraction().CreateTreeNode();
|
---|
104 | var dNode = CreateConstantTreeNode("d", dValue);
|
---|
105 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
106 | substractionNode.AddSubtree(xNode);
|
---|
107 | substractionNode.AddSubtree(dNode);
|
---|
108 |
|
---|
109 | // ( x - d ) / k
|
---|
110 | var divisionNode = new Division().CreateTreeNode();
|
---|
111 | var kNode = CreateConstantTreeNode("k", kValue);
|
---|
112 | divisionNode.AddSubtree(substractionNode);
|
---|
113 | divisionNode.AddSubtree(kNode);
|
---|
114 |
|
---|
115 | return divisionNode;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | private ISymbolicExpressionTreeNode GenerateModelForExponentialTransformation() {
|
---|
120 | var exponentialTransformation = (ExponentialTransformation)transformation;
|
---|
121 | var bValue = exponentialTransformation.Base;
|
---|
122 |
|
---|
123 | return GenTreePow_b_x(bValue);
|
---|
124 | }
|
---|
125 |
|
---|
126 | private ISymbolicExpressionTreeNode GenerateInverseModelForExponentialTransformation() {
|
---|
127 | var exponentialTransformation = (ExponentialTransformation)transformation;
|
---|
128 | var bValue = exponentialTransformation.Base;
|
---|
129 |
|
---|
130 | return GenTreeLog_x_b(bValue);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | private ISymbolicExpressionTreeNode GenerateModelForLogarithmicTransformation() {
|
---|
135 | var logarithmicTransformation = (LogarithmicTransformation)transformation;
|
---|
136 | var bValue = logarithmicTransformation.Base;
|
---|
137 |
|
---|
138 | return GenTreeLog_x_b(bValue);
|
---|
139 | }
|
---|
140 |
|
---|
141 | private ISymbolicExpressionTreeNode GenerateInverseModelForLogarithmicTransformation() {
|
---|
142 | var logarithmicTransformation = (LogarithmicTransformation)transformation;
|
---|
143 | var bValue = logarithmicTransformation.Base;
|
---|
144 |
|
---|
145 | return GenTreePow_b_x(bValue);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | private ISymbolicExpressionTreeNode GenerateModelForPowerTransformation() {
|
---|
150 | var powerTransformation = (PowerTransformation)transformation;
|
---|
151 | var expValue = powerTransformation.Exponent;
|
---|
152 |
|
---|
153 | // x ^ exp
|
---|
154 | var powerNode = new Power().CreateTreeNode();
|
---|
155 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
156 | var expNode = CreateConstantTreeNode("exp", expValue);
|
---|
157 | powerNode.AddSubtree(xNode);
|
---|
158 | powerNode.AddSubtree(expNode);
|
---|
159 |
|
---|
160 | return powerNode;
|
---|
161 | }
|
---|
162 |
|
---|
163 | private ISymbolicExpressionTreeNode GenerateInverseModelForPowerTransformation() {
|
---|
164 | var powerTransformation = (PowerTransformation)transformation;
|
---|
165 | var expValue = powerTransformation.Exponent;
|
---|
166 |
|
---|
167 | // rt(x, b)
|
---|
168 | var rootNode = new Root().CreateTreeNode();
|
---|
169 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
170 | var bNode = CreateConstantTreeNode("b", expValue);
|
---|
171 | rootNode.AddSubtree(xNode);
|
---|
172 | rootNode.AddSubtree(bNode);
|
---|
173 |
|
---|
174 | return rootNode;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | private ISymbolicExpressionTreeNode GenerateModelForReciprocalTransformation() {
|
---|
179 | return GenTreeDiv_1_x();
|
---|
180 | }
|
---|
181 |
|
---|
182 | private ISymbolicExpressionTreeNode GenerateInverseModelForReciprocalTransformation() {
|
---|
183 | return GenTreeDiv_1_x();
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | private ISymbolicExpressionTreeNode GenerateModelForShiftStandardDistributionTransformation() {
|
---|
188 | var shiftStandardDistributionTransformation = (ShiftStandardDistributionTransformation)transformation;
|
---|
189 | var m_orgValue = shiftStandardDistributionTransformation.OriginalMean;
|
---|
190 | var s_orgValue = shiftStandardDistributionTransformation.OriginalStandardDeviation;
|
---|
191 | var m_tarValue = shiftStandardDistributionTransformation.Mean;
|
---|
192 | var s_tarValue = shiftStandardDistributionTransformation.StandardDeviation;
|
---|
193 |
|
---|
194 | return GenTreeShiftStdDist(column, m_orgValue, s_orgValue, m_tarValue, s_tarValue);
|
---|
195 | }
|
---|
196 |
|
---|
197 | private ISymbolicExpressionTreeNode GenerateInverseModelForShiftStandardDistributionTransformation() {
|
---|
198 | var shiftStandardDistributionTransformation = (ShiftStandardDistributionTransformation)transformation;
|
---|
199 | var m_orgValue = shiftStandardDistributionTransformation.OriginalMean;
|
---|
200 | var s_orgValue = shiftStandardDistributionTransformation.OriginalStandardDeviation;
|
---|
201 | var m_tarValue = shiftStandardDistributionTransformation.Mean;
|
---|
202 | var s_tarValue = shiftStandardDistributionTransformation.StandardDeviation;
|
---|
203 |
|
---|
204 | return GenTreeShiftStdDist(column, m_tarValue, s_tarValue, m_orgValue, s_orgValue);
|
---|
205 | }
|
---|
206 |
|
---|
207 | // helper's helper:
|
---|
208 |
|
---|
209 | private ISymbolicExpressionTreeNode GenTreeLog_x_b(double b) {
|
---|
210 | // log(x, b)
|
---|
211 | var logNode = new Logarithm().CreateTreeNode();
|
---|
212 | var bNode = CreateConstantTreeNode("b", b);
|
---|
213 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
214 | logNode.AddSubtree(xNode);
|
---|
215 | logNode.AddSubtree(bNode);
|
---|
216 |
|
---|
217 | return logNode;
|
---|
218 | }
|
---|
219 |
|
---|
220 | private ISymbolicExpressionTreeNode GenTreePow_b_x(double b) {
|
---|
221 | // b ^ x
|
---|
222 | var powerNode = new Power().CreateTreeNode();
|
---|
223 | var bNode = CreateConstantTreeNode("b", b);
|
---|
224 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
225 | powerNode.AddSubtree(bNode);
|
---|
226 | powerNode.AddSubtree(xNode);
|
---|
227 |
|
---|
228 | return powerNode;
|
---|
229 | }
|
---|
230 |
|
---|
231 | private ISymbolicExpressionTreeNode GenTreeDiv_1_x() {
|
---|
232 | // 1 / x
|
---|
233 | var divNode = new Division().CreateTreeNode();
|
---|
234 | var oneNode = CreateConstantTreeNode("1", 1.0);
|
---|
235 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
236 | divNode.AddSubtree(oneNode);
|
---|
237 | divNode.AddSubtree(xNode);
|
---|
238 |
|
---|
239 | return divNode;
|
---|
240 | }
|
---|
241 |
|
---|
242 | private ISymbolicExpressionTreeNode GenTreeShiftStdDist(string variable, double m_org, double s_org, double m_tar, double s_tar) {
|
---|
243 | // x - m_org
|
---|
244 | var substractionNode = new Subtraction().CreateTreeNode();
|
---|
245 | var xNode = CreateVariableTreeNode(variable, "x");
|
---|
246 | var m_orgNode = CreateConstantTreeNode("m_org", m_org);
|
---|
247 | substractionNode.AddSubtree(xNode);
|
---|
248 | substractionNode.AddSubtree(m_orgNode);
|
---|
249 |
|
---|
250 | // (x - m_org) / s_org
|
---|
251 | var divisionNode = new Division().CreateTreeNode();
|
---|
252 | var s_orgNode = CreateConstantTreeNode("s_org", s_org);
|
---|
253 | divisionNode.AddSubtree(substractionNode);
|
---|
254 | divisionNode.AddSubtree(s_orgNode);
|
---|
255 |
|
---|
256 | // ((x - m_org) / s_org ) * s_tar
|
---|
257 | var multiplicationNode = new Multiplication().CreateTreeNode();
|
---|
258 | var s_tarNode = CreateConstantTreeNode("s_tar", s_tar);
|
---|
259 | multiplicationNode.AddSubtree(divisionNode);
|
---|
260 | multiplicationNode.AddSubtree(s_tarNode);
|
---|
261 |
|
---|
262 | // ((x - m_org) / s_org ) * s_tar + m_tar
|
---|
263 | var additionNode = new Addition().CreateTreeNode();
|
---|
264 | var m_tarNode = CreateConstantTreeNode("m_tar", m_tar);
|
---|
265 | additionNode.AddSubtree(multiplicationNode);
|
---|
266 | additionNode.AddSubtree(m_tarNode);
|
---|
267 |
|
---|
268 | return additionNode;
|
---|
269 | }
|
---|
270 |
|
---|
271 | private ConstantTreeNode CreateConstantTreeNode(string description, double value) {
|
---|
272 | return new ConstantTreeNode(new Constant()) { Value = value };
|
---|
273 | }
|
---|
274 |
|
---|
275 | private VariableTreeNode CreateVariableTreeNode(string name, string description) {
|
---|
276 | return new VariableTreeNode(new Variable(name, description)) { VariableName = name, Weight = 1.0 };
|
---|
277 | }
|
---|
278 |
|
---|
279 | private void InitComponents(ITransformation transformation) {
|
---|
280 | this.transformation = transformation;
|
---|
281 | column = transformation.Column;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|