Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs @ 6479

Last change on this file since 6479 was 6234, checked in by mkommend, 14 years ago

#1532: Corrected bug in scaling of trees.

File size: 5.7 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 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 = "SymbolicRegressionModel", 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 static void Scale(SymbolicRegressionModel model, IRegressionProblemData problemData) {
64      var dataset = problemData.Dataset;
65      var targetVariable = problemData.TargetVariable;
66      var rows = problemData.TrainingIndizes;
67      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
68      var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
69      double alpha;
70      double beta;
71      OnlineCalculatorError errorState;
72      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
73      if (errorState != OnlineCalculatorError.None) return;
74
75      ConstantTreeNode alphaTreeNode = null;
76      ConstantTreeNode betaTreeNode = null;
77      // check if model has been scaled previously by analyzing the structure of the tree
78      var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
79      if (startNode.GetSubtree(0).Symbol is Addition) {
80        var addNode = startNode.GetSubtree(0);
81        if (addNode.SubtreesCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
82          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
83          var mulNode = addNode.GetSubtree(0);
84          if (mulNode.SubtreesCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
85            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
86          }
87        }
88      }
89      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
90      if (alphaTreeNode != null && betaTreeNode != null) {
91        betaTreeNode.Value *= beta;
92        alphaTreeNode.Value *= beta;
93        alphaTreeNode.Value += alpha;
94      } else {
95        var mainBranch = startNode.GetSubtree(0);
96        startNode.RemoveSubtree(0);
97        var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
98        startNode.AddSubtree(scaledMainBranch);
99      }
100    }
101
102    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
103      if (alpha.IsAlmost(0.0)) {
104        return treeNode;
105      } else {
106        var addition = new Addition();
107        var node = addition.CreateTreeNode();
108        var alphaConst = MakeConstant(alpha);
109        node.AddSubtree(treeNode);
110        node.AddSubtree(alphaConst);
111        return node;
112      }
113    }
114
115    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
116      if (beta.IsAlmost(1.0)) {
117        return treeNode;
118      } else {
119        var multipliciation = new Multiplication();
120        var node = multipliciation.CreateTreeNode();
121        var betaConst = MakeConstant(beta);
122        node.AddSubtree(treeNode);
123        node.AddSubtree(betaConst);
124        return node;
125      }
126    }
127
128    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
129      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
130      node.Value = c;
131      return node;
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.