source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TransformationToSymbolicTreeMapper.cs @ 10943

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