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

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