Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6180 was 5942, checked in by mkommend, 14 years ago

#1453: Renamed IOnlineEvaluator to IOnlineCalculator

File size: 5.7 KB
RevLine 
[5624]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 {
[5720]35    [Storable]
36    private double lowerEstimationLimit;
37    [Storable]
38    private double upperEstimationLimit;
[5624]39
40    [StorableConstructor]
41    protected SymbolicRegressionModel(bool deserializing) : base(deserializing) { }
42    protected SymbolicRegressionModel(SymbolicRegressionModel original, Cloner cloner)
43      : base(original, cloner) {
[5720]44      this.lowerEstimationLimit = original.lowerEstimationLimit;
45      this.upperEstimationLimit = original.upperEstimationLimit;
[5624]46    }
[5720]47    public SymbolicRegressionModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
48      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
[5624]49      : base(tree, interpreter) {
[5720]50      this.lowerEstimationLimit = lowerEstimationLimit;
51      this.upperEstimationLimit = upperEstimationLimit;
[5624]52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new SymbolicRegressionModel(this, cloner);
56    }
57
[5649]58    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
[5720]59      return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows)
60        .LimitToRange(lowerEstimationLimit, upperEstimationLimit);
[5624]61    }
[5818]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;
[5942]71      OnlineCalculatorError errorState;
[5894]72      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
[5942]73      if (errorState != OnlineCalculatorError.None) return;
[5818]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(beta, mainBranch), 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 node = (new Addition()).CreateTreeNode();
107        var alphaConst = MakeConstant(alpha);
108        node.AddSubtree(treeNode);
109        node.AddSubtree(alphaConst);
110        return node;
111      }
112    }
113
114    private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) {
115      if (beta.IsAlmost(1.0)) {
116        return treeNode;
117      } else {
118        var node = (new Multiplication()).CreateTreeNode();
119        var betaConst = MakeConstant(beta);
120        node.AddSubtree(treeNode);
121        node.AddSubtree(betaConst);
122        return node;
123      }
124    }
125
126    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
127      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
128      node.Value = c;
129      return node;
130    }
[5624]131  }
132}
Note: See TracBrowser for help on using the repository browser.