Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression/3.3/Symbolic/SymbolicVectorRegressionSolutionLinearScaler.cs @ 4112

Last change on this file since 4112 was 4112, checked in by gkronber, 14 years ago

Fixed some bugs in multi-variate regression classes. #1089

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
31
32namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic {
33  /// <summary>
34  /// An operator that creates a linearly transformed symbolic regression solution (given alpha and beta).
35  /// </summary>
36  [Item("SymbolicVectorRegressionSolutionLinearScaler", "An operator that creates a linearly transformed symbolic regression solution (given alpha and beta).")]
37  [StorableClass]
38  public sealed class SymbolicVectorRegressionSolutionLinearScaler : SingleSuccessorOperator {
39    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
40    private const string ScaledSymbolicExpressionTreeParameterName = "ScaledSymbolicExpressionTree";
41    private const string AlphaParameterName = "Alpha";
42    private const string BetaParameterName = "Beta";
43
44    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
45      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
46    }
47    public ILookupParameter<SymbolicExpressionTree> ScaledSymbolicExpressionTreeParameter {
48      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[ScaledSymbolicExpressionTreeParameterName]; }
49    }
50    public ILookupParameter<DoubleArray> AlphaParameter {
51      get { return (ILookupParameter<DoubleArray>)Parameters[AlphaParameterName]; }
52    }
53    public ILookupParameter<DoubleArray> BetaParameter {
54      get { return (ILookupParameter<DoubleArray>)Parameters[BetaParameterName]; }
55    }
56
57    public SymbolicVectorRegressionSolutionLinearScaler()
58      : base() {
59      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to transform."));
60      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(ScaledSymbolicExpressionTreeParameterName, "The resulting symbolic expression trees after transformation."));
61      Parameters.Add(new LookupParameter<DoubleArray>(AlphaParameterName, "Alpha parameter for linear transformation."));
62      Parameters.Add(new LookupParameter<DoubleArray>(BetaParameterName, "Beta parameter for linear transformation."));
63    }
64
65    public override IOperation Apply() {
66      SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
67      DoubleArray alpha = AlphaParameter.ActualValue;
68      DoubleArray beta = BetaParameter.ActualValue;
69      if (alpha != null && beta != null) {
70        ScaledSymbolicExpressionTreeParameter.ActualValue = Scale(tree, beta.ToArray(), alpha.ToArray());
71      } else {
72        // alpha or beta parameter not available => do not scale tree
73        ScaledSymbolicExpressionTreeParameter.ActualValue = tree;
74      }
75
76      return base.Apply();
77    }
78
79    public static SymbolicExpressionTree Scale(SymbolicExpressionTree original, double[] beta, double[] alpha) {
80      List<SymbolicExpressionTreeNode> resultProducingBranches = new List<SymbolicExpressionTreeNode>(original.Root.SubTrees[0].SubTrees);
81      // remove the main branch before cloning to prevent cloning of sub-trees
82      while (original.Root.SubTrees[0].SubTrees.Count > 0)
83        original.Root.SubTrees[0].RemoveSubTree(0);
84      var scaledTree = (SymbolicExpressionTree)original.Clone();
85      int i = 0;
86      foreach (var resultProducingBranch in resultProducingBranches) {
87        var scaledMainBranch = MakeSum(MakeProduct(beta[i], resultProducingBranch), alpha[i]);
88
89        // insert main branch into the original tree again
90        original.Root.SubTrees[0].AddSubTree(resultProducingBranch);
91        // insert the scaled main branch into the cloned tree
92        scaledTree.Root.SubTrees[0].AddSubTree(scaledMainBranch);
93        i++;
94      }
95      return scaledTree;
96    }
97
98    private static SymbolicExpressionTreeNode MakeSum(SymbolicExpressionTreeNode treeNode, double alpha) {
99      var node = (new Addition()).CreateTreeNode();
100      var alphaConst = MakeConstant(alpha);
101      node.AddSubTree(treeNode);
102      node.AddSubTree(alphaConst);
103      return node;
104    }
105
106    private static SymbolicExpressionTreeNode MakeProduct(double beta, SymbolicExpressionTreeNode treeNode) {
107      var node = (new Multiplication()).CreateTreeNode();
108      var betaConst = MakeConstant(beta);
109      node.AddSubTree(treeNode);
110      node.AddSubTree(betaConst);
111      return node;
112    }
113
114    private static SymbolicExpressionTreeNode MakeConstant(double c) {
115      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
116      node.Value = c;
117      return node;
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.