Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicExpressionTreeBacktransformator.cs @ 12338

Last change on this file since 12338 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26
27namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
28  public class SymbolicExpressionTreeBacktransformator : IModelBacktransformator {
29    private readonly ITransformationMapper<ISymbolicExpressionTreeNode> transformationMapper;
30
31    public SymbolicExpressionTreeBacktransformator(ITransformationMapper<ISymbolicExpressionTreeNode> transformationMapper) {
32      this.transformationMapper = transformationMapper;
33    }
34
35    public ISymbolicDataAnalysisModel Backtransform(ISymbolicDataAnalysisModel model, IEnumerable<ITransformation> transformations, string targetVariable) {
36      var symbolicModel = (ISymbolicDataAnalysisModel)model.Clone();
37
38      foreach (var transformation in transformations.Reverse()) {
39        ApplyBacktransformation(transformation, symbolicModel.SymbolicExpressionTree, targetVariable);
40      }
41
42      return symbolicModel;
43    }
44
45    private void ApplyBacktransformation(ITransformation transformation, ISymbolicExpressionTree symbolicExpressionTree, string targetVariable) {
46      if (transformation.Column != targetVariable) {
47        var variableNodes = symbolicExpressionTree.IterateNodesBreadth()
48          .OfType<VariableTreeNode>()
49          .Where(n => n.VariableName == transformation.Column);
50        ApplyRegularBacktransformation(transformation, variableNodes);
51      } else if (!(transformation is CopyColumnTransformation)) {
52        ApplyInverseBacktransformation(transformation, symbolicExpressionTree);
53      }
54    }
55
56    private void ApplyRegularBacktransformation(ITransformation transformation, IEnumerable<VariableTreeNode> variableNodes) {
57      foreach (var variableNode in variableNodes) {
58        // generate new subtrees because same subtree cannot be added more than once
59        var transformationTree = transformationMapper.GenerateModel(transformation);
60        SwapVariableWithTree(variableNode, transformationTree);
61      }
62    }
63
64    private void ApplyInverseBacktransformation(ITransformation transformation, ISymbolicExpressionTree symbolicExpressionTree) {
65      var startSymbol = symbolicExpressionTree.Root.GetSubtree(0);
66      var modelTree = startSymbol.GetSubtree(0);
67      startSymbol.RemoveSubtree(0);
68
69      var transformationTree = transformationMapper.GenerateInverseModel(transformation);
70      var variableNode = transformationTree.IterateNodesBreadth()
71        .OfType<VariableTreeNode>()
72        .Single(n => n.VariableName == transformation.Column);
73
74      SwapVariableWithTree(variableNode, modelTree);
75
76      startSymbol.AddSubtree(transformationTree);
77    }
78
79    private void SwapVariableWithTree(VariableTreeNode variableNode, ISymbolicExpressionTreeNode treeNode) {
80      var parent = variableNode.Parent;
81      int index = parent.IndexOfSubtree(variableNode);
82      parent.RemoveSubtree(index);
83
84      if (!variableNode.Weight.IsAlmost(1.0))
85        treeNode = CreateNodeFromWeight(treeNode, variableNode);
86
87      parent.InsertSubtree(index, treeNode);
88    }
89
90    private ISymbolicExpressionTreeNode CreateNodeFromWeight(ISymbolicExpressionTreeNode transformationTree, VariableTreeNode variableNode) {
91      var multiplicationNode = new SymbolicExpressionTreeNode(new Multiplication());
92      multiplicationNode.AddSubtree(new ConstantTreeNode(new Constant()) { Value = variableNode.Weight });
93      multiplicationNode.AddSubtree(transformationTree);
94      return multiplicationNode;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.