Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionSolutionLinearScaler.cs @ 4191

Last change on this file since 4191 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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