Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.4 KB
RevLine 
[10854]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10854]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;
[10878]24using HeuristicLab.Common;
[10854]25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26
27namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
28  public class SymbolicExpressionTreeBacktransformator : IModelBacktransformator {
[10872]29    private readonly ITransformationMapper<ISymbolicExpressionTreeNode> transformationMapper;
[10854]30
[10872]31    public SymbolicExpressionTreeBacktransformator(ITransformationMapper<ISymbolicExpressionTreeNode> transformationMapper) {
[10854]32      this.transformationMapper = transformationMapper;
33    }
34
[11156]35    public ISymbolicDataAnalysisModel Backtransform(ISymbolicDataAnalysisModel model, IEnumerable<ITransformation> transformations, string targetVariable) {
[11011]36      var symbolicModel = (ISymbolicDataAnalysisModel)model.Clone();
[10854]37
[10980]38      foreach (var transformation in transformations.Reverse()) {
[10880]39        ApplyBacktransformation(transformation, symbolicModel.SymbolicExpressionTree, targetVariable);
[10854]40      }
[11011]41
42      return symbolicModel;
[10854]43    }
44
[10880]45    private void ApplyBacktransformation(ITransformation transformation, ISymbolicExpressionTree symbolicExpressionTree, string targetVariable) {
[10883]46      if (transformation.Column != targetVariable) {
47        var variableNodes = symbolicExpressionTree.IterateNodesBreadth()
48          .OfType<VariableTreeNode>()
49          .Where(n => n.VariableName == transformation.Column);
50        ApplyRegularBacktransformation(transformation, variableNodes);
[10980]51      } else if (!(transformation is CopyColumnTransformation)) {
[10883]52        ApplyInverseBacktransformation(transformation, symbolicExpressionTree);
53      }
54    }
[10869]55
[10883]56    private void ApplyRegularBacktransformation(ITransformation transformation, IEnumerable<VariableTreeNode> variableNodes) {
[10869]57      foreach (var variableNode in variableNodes) {
[10878]58        // generate new subtrees because same subtree cannot be added more than once
59        var transformationTree = transformationMapper.GenerateModel(transformation);
[10883]60        SwapVariableWithTree(variableNode, transformationTree);
[10869]61      }
[10854]62    }
63
[10883]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) {
[10874]80      var parent = variableNode.Parent;
81      int index = parent.IndexOfSubtree(variableNode);
[10869]82      parent.RemoveSubtree(index);
83
[10883]84      if (!variableNode.Weight.IsAlmost(1.0))
85        treeNode = CreateNodeFromWeight(treeNode, variableNode);
[10878]86
[10883]87      parent.InsertSubtree(index, treeNode);
[10878]88    }
89
90    private ISymbolicExpressionTreeNode CreateNodeFromWeight(ISymbolicExpressionTreeNode transformationTree, VariableTreeNode variableNode) {
[10874]91      var multiplicationNode = new SymbolicExpressionTreeNode(new Multiplication());
92      multiplicationNode.AddSubtree(new ConstantTreeNode(new Constant()) { Value = variableNode.Weight });
93      multiplicationNode.AddSubtree(transformationTree);
[10883]94      return multiplicationNode;
[10854]95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.