Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionLinearScaler.cs @ 5722

Last change on this file since 5722 was 5722, checked in by gkronber, 13 years ago

#1418 fixed evaluator call from validation analyzers, fixed bugs in interactive simplifier view and added apply linear scaling flag to analyzers.

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis.Symbolic;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
32  /// <summary>
33  /// An operator that creates a linearly transformed symbolic regression solution (given alpha and beta).
34  /// </summary>
35  [Item("SymbolicRegressionSolutionLinearScaler", "An operator that creates a linearly transformed symbolic regression solution (given alpha and beta).")]
36  [StorableClass]
37  public sealed class SymbolicRegressionSolutionLinearScaler : SingleSuccessorOperator {
38    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
39    private const string ScaledSymbolicExpressionTreeParameterName = "ScaledSymbolicExpressionTree";
40    private const string AlphaParameterName = "Alpha";
41    private const string BetaParameterName = "Beta";
42
43    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
44      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
45    }
46    public ILookupParameter<SymbolicExpressionTree> ScaledSymbolicExpressionTreeParameter {
47      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[ScaledSymbolicExpressionTreeParameterName]; }
48    }
49    public ILookupParameter<DoubleValue> AlphaParameter {
50      get { return (ILookupParameter<DoubleValue>)Parameters[AlphaParameterName]; }
51    }
52    public ILookupParameter<DoubleValue> BetaParameter {
53      get { return (ILookupParameter<DoubleValue>)Parameters[BetaParameterName]; }
54    }
55
56    [StorableConstructor]
57    private SymbolicRegressionSolutionLinearScaler(bool deserializing) : base(deserializing) { }
58    private SymbolicRegressionSolutionLinearScaler(SymbolicRegressionSolutionLinearScaler original, Cloner cloner) : base(original, cloner) { }
59    public SymbolicRegressionSolutionLinearScaler()
60      : base() {
61      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to transform."));
62      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(ScaledSymbolicExpressionTreeParameterName, "The resulting symbolic expression trees after transformation."));
63      Parameters.Add(new LookupParameter<DoubleValue>(AlphaParameterName, "Alpha parameter for linear transformation."));
64      Parameters.Add(new LookupParameter<DoubleValue>(BetaParameterName, "Beta parameter for linear transformation."));
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new SymbolicRegressionSolutionLinearScaler(this, cloner);
69    }
70
71    public override IOperation Apply() {
72      // TODO
73      return base.Apply();
74    }
75
76    private static ISymbolicExpressionTree Scale(ISymbolicExpressionTree original, double alpha, double beta) {
77      var mainBranch = original.Root.GetSubTree(0).GetSubTree(0);
78      var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
79
80      // remove the main branch before cloning to prevent cloning of sub-trees
81      original.Root.GetSubTree(0).RemoveSubTree(0);
82      var scaledTree = (SymbolicExpressionTree)original.Clone();
83      // insert main branch into the original tree again
84      original.Root.GetSubTree(0).InsertSubTree(0, mainBranch);
85      // insert the scaled main branch into the cloned tree
86      scaledTree.Root.GetSubTree(0).InsertSubTree(0, scaledMainBranch);
87      return scaledTree;
88    }
89
90    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
91      var node = (new Addition()).CreateTreeNode();
92      var alphaConst = MakeConstant(alpha);
93      node.AddSubTree(treeNode);
94      node.AddSubTree(alphaConst);
95      return node;
96    }
97
98    private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) {
99      var node = (new Multiplication()).CreateTreeNode();
100      var betaConst = MakeConstant(beta);
101      node.AddSubTree(treeNode);
102      node.AddSubTree(betaConst);
103      return node;
104    }
105
106    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
107      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
108      node.Value = c;
109      return node;
110    }
111
112    internal static void Scale(SymbolicRegressionSolution solution) {
113      var dataset = solution.ProblemData.Dataset;
114      var targetVariable = solution.ProblemData.TargetVariable;
115      var rows = solution.ProblemData.TrainingIndizes;
116      var estimatedValues = solution.GetEstimatedValues(rows);
117      var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
118      double alpha;
119      double beta;
120      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta);
121
122      var originalModel = solution.Model;
123      solution.Model = new SymbolicRegressionModel(Scale(originalModel.SymbolicExpressionTree, alpha, beta), originalModel.Interpreter);
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.