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

Last change on this file since 10869 was 10869, checked in by pfleck, 6 years ago
  • implemented basic backtransformation without variable weights
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<ISymbolicExpressionTree> {
28    private ITransformation transformation;
29    private string column;
30    private ISymbolicExpressionTree tree;
31
32    #region ITransformationMapper<ISymbolicExpressionTree> Members
33
34    public ISymbolicExpressionTree GenerateModel(ITransformation transformation) {
35      InitComponents(transformation);
36
37      if (transformation is LinearTransformation) {
38        return GenerateModelForLinearTransformation();
39      } else if (transformation is ExponentialTransformation) {
40        return GenerateModelForExponentialTransformation();
41      } else if (transformation is LogarithmicTransformation) {
42        return GenerateModelForLogarithmicTransformation();
43      } else if (transformation is PowerTransformation) {
44        return GenerateModelForPowerTransformation();
45      } else if (transformation is ReciprocalTransformation) {
46        return GenerateModelForReciprocalTransformation();
47      } else if (transformation is ShiftStandardDistributionTransformation) {
48        return GenerateModelForShiftStandardDistributionTransformation();
49      }
50      throw new NotImplementedException();
51    }
52
53    public ISymbolicExpressionTree GenerateInverseModel(ITransformation transformation) {
54      InitComponents(transformation);
55
56      if (transformation is LinearTransformation) {
57        return GenerateInverseModelForLinearTransformation();
58      } else if (transformation is ExponentialTransformation) {
59        return GenerateInverseModelForExponentialTransformation();
60      } else if (transformation is LogarithmicTransformation) {
61        return GenerateInverseModelForLogarithmicTransformation();
62      } else if (transformation is PowerTransformation) {
63        return GenerateInverseModelForPowerTransformation();
64      } else if (transformation is ReciprocalTransformation) {
65        return GenerateInverseModelForReciprocalTransformation();
66      } else if (transformation is ShiftStandardDistributionTransformation) {
67        GenerateInverseModelForShiftStandardDistributionTransformation();
68      }
69
70      throw new NotImplementedException();
71    }
72
73    #endregion
74
75    // helper
76
77    private ISymbolicExpressionTree GenerateModelForLinearTransformation() {
78      var linearTransformation = (LinearTransformation)transformation;
79      var kValue = linearTransformation.Multiplier;
80      var dValue = linearTransformation.Addend;
81
82      // k * x
83      var multiplicationNode = new Multiplication().CreateTreeNode();
84      var kNode = CreateConstantTreeNode("k", kValue);
85      var xNode = CreateVariableTreeNode(column, "x");
86      multiplicationNode.AddSubtree(kNode);
87      multiplicationNode.AddSubtree(xNode);
88
89      // ( k * x ) + d
90      var additionNode = new Addition().CreateTreeNode();
91      var dNode = CreateConstantTreeNode("d", dValue);
92      additionNode.AddSubtree(multiplicationNode);
93      additionNode.AddSubtree(dNode);
94
95      tree.Root.AddSubtree(additionNode);
96      return tree;
97    }
98
99    private ISymbolicExpressionTree 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      tree.Root.AddSubtree(divisionNode);
118      return tree;
119    }
120
121
122    private ISymbolicExpressionTree GenerateModelForExponentialTransformation() {
123      var exponentialTransformation = (ExponentialTransformation)transformation;
124      var bValue = exponentialTransformation.Base;
125
126      return GenTreePow_b_x(bValue);
127    }
128
129    private ISymbolicExpressionTree GenerateInverseModelForExponentialTransformation() {
130      var exponentialTransformation = (ExponentialTransformation)transformation;
131      var bValue = exponentialTransformation.Base;
132
133      return GenTreeLog_x_b(bValue);
134    }
135
136
137    private ISymbolicExpressionTree GenerateModelForLogarithmicTransformation() {
138      var logarithmicTransformation = (LogarithmicTransformation)transformation;
139      var bValue = logarithmicTransformation.Base;
140
141      return GenTreeLog_x_b(bValue);
142    }
143
144    private ISymbolicExpressionTree GenerateInverseModelForLogarithmicTransformation() {
145      var logarithmicTransformation = (LogarithmicTransformation)transformation;
146      var bValue = logarithmicTransformation.Base;
147
148      return GenTreePow_b_x(bValue);
149    }
150
151
152    private ISymbolicExpressionTree GenerateModelForPowerTransformation() {
153      var powerTransformation = (PowerTransformation)transformation;
154      var expValue = powerTransformation.Exponent;
155
156      // x ^ exp
157      var powerNode = new Power().CreateTreeNode();
158      var xNode = CreateVariableTreeNode(column, "x");
159      var expNode = CreateConstantTreeNode("exp", expValue);
160      powerNode.AddSubtree(xNode);
161      powerNode.AddSubtree(expNode);
162
163      tree.Root.AddSubtree(powerNode);
164      return tree;
165    }
166
167    private ISymbolicExpressionTree 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      tree.Root.AddSubtree(rootNode);
179      return tree;
180    }
181
182
183    private ISymbolicExpressionTree GenerateModelForReciprocalTransformation() {
184      return GenTreeDiv_1_x();
185    }
186
187    private ISymbolicExpressionTree GenerateInverseModelForReciprocalTransformation() {
188      return GenTreeDiv_1_x();
189    }
190
191    private ISymbolicExpressionTree 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 ISymbolicExpressionTree 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    // helper's helper:
212
213    private ISymbolicExpressionTree GenTreeLog_x_b(double b) {
214      var tree = CreateNewTree();
215
216      // log(x, b)
217      var logNode = new Logarithm().CreateTreeNode();
218      var bNode = CreateConstantTreeNode("b", b);
219      var xNode = CreateVariableTreeNode(column, "x");
220      logNode.AddSubtree(xNode);
221      logNode.AddSubtree(bNode);
222
223      tree.Root.AddSubtree(logNode);
224      return tree;
225    }
226
227    private ISymbolicExpressionTree GenTreePow_b_x(double b) {
228      var tree = CreateNewTree();
229
230      // b ^ x
231      var powerNode = new Power().CreateTreeNode();
232      var bNode = CreateConstantTreeNode("b", b);
233      var xNode = CreateVariableTreeNode(column, "x");
234      powerNode.AddSubtree(bNode);
235      powerNode.AddSubtree(xNode);
236
237      tree.Root.AddSubtree(powerNode);
238      return tree;
239    }
240
241    private ISymbolicExpressionTree GenTreeDiv_1_x() {
242      var tree = CreateNewTree();
243
244      // 1 / x
245      var divNode = new Division().CreateTreeNode();
246      var oneNode = CreateConstantTreeNode("1", 1.0);
247      var xNode = CreateVariableTreeNode(column, "x");
248      divNode.AddSubtree(oneNode);
249      divNode.AddSubtree(xNode);
250
251      tree.Root.AddSubtree(divNode);
252      return tree;
253    }
254    private ISymbolicExpressionTree GenTreeShiftStdDist(string variable, double m_org, double s_org, double m_tar, double s_tar) {
255      var tree = CreateNewTree();
256      // x - m_org
257      var substractionNode = new Subtraction().CreateTreeNode();
258      var xNode = CreateVariableTreeNode(variable, "x");
259      var m_orgNode = CreateConstantTreeNode("m_org", m_org);
260      substractionNode.AddSubtree(xNode);
261      substractionNode.AddSubtree(m_orgNode);
262
263      // (x - m_org) / s_org
264      var divisionNode = new Division().CreateTreeNode();
265      var s_orgNode = CreateConstantTreeNode("s_org", s_org);
266      divisionNode.AddSubtree(substractionNode);
267      divisionNode.AddSubtree(s_orgNode);
268
269      // ((x - m_org) / s_org ) * s_tar
270      var multiplicationNode = new Multiplication().CreateTreeNode();
271      var s_tarNode = CreateConstantTreeNode("s_tar", s_tar);
272      multiplicationNode.AddSubtree(divisionNode);
273      multiplicationNode.AddSubtree(s_tarNode);
274
275      // ((x - m_org) / s_org ) * s_tar + m_tar
276      var additionNode = new Addition().CreateTreeNode();
277      var m_tarNode = CreateConstantTreeNode("m_tar", m_tar);
278      additionNode.AddSubtree(multiplicationNode);
279      additionNode.AddSubtree(m_tarNode);
280
281      tree.Root.AddSubtree(additionNode);
282      return tree;
283    }
284
285    private ConstantTreeNode CreateConstantTreeNode(string description, double value) {
286      return new ConstantTreeNode(new Constant()) { Value = value };
287    }
288    private ISymbolicExpressionTreeNode CreateVariableTreeNode(string name, string description) {
289      return new Variable(name, description).CreateTreeNode();
290    }
291
292
293    private ISymbolicExpressionTree CreateNewTree() {
294      return new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
295    }
296
297    private void InitComponents(ITransformation transformation) {
298      this.transformation = transformation;
299      column = transformation.Column;
300      tree = CreateNewTree();
301    }
302  }
303}
Note: See TracBrowser for help on using the repository browser.