Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs @ 7871

Last change on this file since 7871 was 7270, checked in by spimming, 13 years ago

#1680:

  • merged changes from trunk into branch
File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
29  /// <summary>
30  /// Represents a symbolic regression model
31  /// </summary>
32  [StorableClass]
33  [Item(Name = "Symbolic Regression Model", Description = "Represents a symbolic regression model.")]
34  public class SymbolicRegressionModel : SymbolicDataAnalysisModel, ISymbolicRegressionModel {
35    [Storable]
36    private double lowerEstimationLimit;
37    [Storable]
38    private double upperEstimationLimit;
39
40    [StorableConstructor]
41    protected SymbolicRegressionModel(bool deserializing) : base(deserializing) { }
42    protected SymbolicRegressionModel(SymbolicRegressionModel original, Cloner cloner)
43      : base(original, cloner) {
44      this.lowerEstimationLimit = original.lowerEstimationLimit;
45      this.upperEstimationLimit = original.upperEstimationLimit;
46    }
47    public SymbolicRegressionModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
48      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
49      : base(tree, interpreter) {
50      this.lowerEstimationLimit = lowerEstimationLimit;
51      this.upperEstimationLimit = upperEstimationLimit;
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new SymbolicRegressionModel(this, cloner);
56    }
57
58    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
59      return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows)
60        .LimitToRange(lowerEstimationLimit, upperEstimationLimit);
61    }
62
63    public ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
64      return new SymbolicRegressionSolution(this, problemData);
65    }
66    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
67      return CreateRegressionSolution(problemData);
68    }
69
70    public static void Scale(SymbolicRegressionModel model, IRegressionProblemData problemData) {
71      var dataset = problemData.Dataset;
72      var targetVariable = problemData.TargetVariable;
73      var rows = problemData.TrainingIndizes;
74      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
75      var targetValues = dataset.GetDoubleValues(targetVariable, rows);
76      double alpha;
77      double beta;
78      OnlineCalculatorError errorState;
79      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
80      if (errorState != OnlineCalculatorError.None) return;
81
82      ConstantTreeNode alphaTreeNode = null;
83      ConstantTreeNode betaTreeNode = null;
84      // check if model has been scaled previously by analyzing the structure of the tree
85      var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
86      if (startNode.GetSubtree(0).Symbol is Addition) {
87        var addNode = startNode.GetSubtree(0);
88        if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
89          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
90          var mulNode = addNode.GetSubtree(0);
91          if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
92            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
93          }
94        }
95      }
96      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
97      if (alphaTreeNode != null && betaTreeNode != null) {
98        betaTreeNode.Value *= beta;
99        alphaTreeNode.Value *= beta;
100        alphaTreeNode.Value += alpha;
101      } else {
102        var mainBranch = startNode.GetSubtree(0);
103        startNode.RemoveSubtree(0);
104        var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
105        startNode.AddSubtree(scaledMainBranch);
106      }
107    }
108
109    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
110      if (alpha.IsAlmost(0.0)) {
111        return treeNode;
112      } else {
113        var addition = new Addition();
114        var node = addition.CreateTreeNode();
115        var alphaConst = MakeConstant(alpha);
116        node.AddSubtree(treeNode);
117        node.AddSubtree(alphaConst);
118        return node;
119      }
120    }
121
122    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
123      if (beta.IsAlmost(1.0)) {
124        return treeNode;
125      } else {
126        var multipliciation = new Multiplication();
127        var node = multipliciation.CreateTreeNode();
128        var betaConst = MakeConstant(beta);
129        node.AddSubtree(treeNode);
130        node.AddSubtree(betaConst);
131        return node;
132      }
133    }
134
135    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
136      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
137      node.Value = c;
138      return node;
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.