Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionImpactValuesCalculator.cs @ 15301

Last change on this file since 15301 was 14826, checked in by gkronber, 8 years ago

#2650: merged the factors branch into trunk

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
30  [StorableClass]
31  [Item("SymbolicRegressionSolutionImpactValuesCalculator", "Calculate symbolic expression tree node impact values for regression problems.")]
32  public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
33    public SymbolicRegressionSolutionImpactValuesCalculator() { }
34
35    protected SymbolicRegressionSolutionImpactValuesCalculator(SymbolicRegressionSolutionImpactValuesCalculator original, Cloner cloner)
36      : base(original, cloner) { }
37    public override IDeepCloneable Clone(Cloner cloner) {
38      return new SymbolicRegressionSolutionImpactValuesCalculator(this, cloner);
39    }
40
41    [StorableConstructor]
42    protected SymbolicRegressionSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { }
43
44    public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node,
45      IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation,
46      double qualityForImpactsCalculation = double.NaN) {
47      var regressionModel = (ISymbolicRegressionModel)model;
48      var regressionProblemData = (IRegressionProblemData)problemData;
49
50      var dataset = regressionProblemData.Dataset;
51      var targetValues = dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows);
52
53      if (double.IsNaN(qualityForImpactsCalculation))
54        qualityForImpactsCalculation = CalculateQualityForImpacts(regressionModel, regressionProblemData, rows);
55
56      var cloner = new Cloner();
57      var tempModel = cloner.Clone(regressionModel);
58      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
59
60      var tempModelParentNode = tempModelNode.Parent;
61      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
62
63      double bestReplacementValue = 0.0;
64      double bestImpactValue = double.PositiveInfinity;
65      newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize
66      // try the potentially reasonable replacement values and use the best one
67      foreach (var repValue in CalculateReplacementValues(node, regressionModel.SymbolicExpressionTree, regressionModel.Interpreter, regressionProblemData.Dataset, regressionProblemData.TrainingIndices)) {
68
69        tempModelParentNode.RemoveSubtree(i);
70
71        var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue };
72
73        tempModelParentNode.InsertSubtree(i, constantNode);
74
75        var estimatedValues = tempModel.GetEstimatedValues(dataset, rows);
76        OnlineCalculatorError errorState;
77        double r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out errorState);
78        if (errorState != OnlineCalculatorError.None) r = 0.0;
79        newQualityForImpactsCalculation = r * r;
80
81        impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
82        if (impactValue < bestImpactValue) {
83          bestImpactValue = impactValue;
84          bestReplacementValue = repValue;
85        }
86      }
87      replacementValue = bestReplacementValue;
88      impactValue = bestImpactValue;
89    }
90
91    public static double CalculateQualityForImpacts(ISymbolicRegressionModel model, IRegressionProblemData problemData, IEnumerable<int> rows) {
92      var estimatedValues = model.GetEstimatedValues(problemData.Dataset, rows); // also bounds the values
93      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
94      OnlineCalculatorError errorState;
95      var r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out errorState);
96      var quality = r * r;
97      if (errorState != OnlineCalculatorError.None) return double.NaN;
98      return quality;
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.