Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs @ 10316

Last change on this file since 10316 was 8660, checked in by gkronber, 12 years ago

#1847 merged r8205:8635 from trunk into branch

File size: 6.2 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    public double LowerEstimationLimit { get { return lowerEstimationLimit; } }
38    [Storable]
39    private double upperEstimationLimit;
40    public double UpperEstimationLimit { get { return upperEstimationLimit; } }
41
42    [StorableConstructor]
43    protected SymbolicRegressionModel(bool deserializing) : base(deserializing) { }
44    protected SymbolicRegressionModel(SymbolicRegressionModel original, Cloner cloner)
45      : base(original, cloner) {
46      this.lowerEstimationLimit = original.lowerEstimationLimit;
47      this.upperEstimationLimit = original.upperEstimationLimit;
48    }
49    public SymbolicRegressionModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
50      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
51      : base(tree, interpreter) {
52      this.lowerEstimationLimit = lowerEstimationLimit;
53      this.upperEstimationLimit = upperEstimationLimit;
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new SymbolicRegressionModel(this, cloner);
58    }
59
60    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
61      return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows)
62        .LimitToRange(lowerEstimationLimit, upperEstimationLimit);
63    }
64
65    public ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
66      return new SymbolicRegressionSolution(this, new RegressionProblemData(problemData));
67    }
68    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
69      return CreateRegressionSolution(problemData);
70    }
71
72    public static void Scale(SymbolicRegressionModel model, IRegressionProblemData problemData) {
73      var dataset = problemData.Dataset;
74      var targetVariable = problemData.TargetVariable;
75      var rows = problemData.TrainingIndices;
76      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
77      var targetValues = dataset.GetDoubleValues(targetVariable, rows);
78      double alpha;
79      double beta;
80      OnlineCalculatorError errorState;
81      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
82      if (errorState != OnlineCalculatorError.None) return;
83
84      ConstantTreeNode alphaTreeNode = null;
85      ConstantTreeNode betaTreeNode = null;
86      // check if model has been scaled previously by analyzing the structure of the tree
87      var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
88      if (startNode.GetSubtree(0).Symbol is Addition) {
89        var addNode = startNode.GetSubtree(0);
90        if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
91          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
92          var mulNode = addNode.GetSubtree(0);
93          if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
94            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
95          }
96        }
97      }
98      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
99      if (alphaTreeNode != null && betaTreeNode != null) {
100        betaTreeNode.Value *= beta;
101        alphaTreeNode.Value *= beta;
102        alphaTreeNode.Value += alpha;
103      } else {
104        var mainBranch = startNode.GetSubtree(0);
105        startNode.RemoveSubtree(0);
106        var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
107        startNode.AddSubtree(scaledMainBranch);
108      }
109    }
110
111    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
112      if (alpha.IsAlmost(0.0)) {
113        return treeNode;
114      } else {
115        var addition = new Addition();
116        var node = addition.CreateTreeNode();
117        var alphaConst = MakeConstant(alpha);
118        node.AddSubtree(treeNode);
119        node.AddSubtree(alphaConst);
120        return node;
121      }
122    }
123
124    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
125      if (beta.IsAlmost(1.0)) {
126        return treeNode;
127      } else {
128        var multipliciation = new Multiplication();
129        var node = multipliciation.CreateTreeNode();
130        var betaConst = MakeConstant(beta);
131        node.AddSubtree(treeNode);
132        node.AddSubtree(betaConst);
133        return node;
134      }
135    }
136
137    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
138      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
139      node.Value = c;
140      return node;
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.