Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2650: merged the factors branch into trunk

File size: 4.9 KB
RevLine 
[8409]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8409]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
[10469]22using System;
[8409]23using System.Collections.Generic;
[8935]24using HeuristicLab.Common;
[10469]25using HeuristicLab.Core;
[8409]26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[10469]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[8409]28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[10469]30  [StorableClass]
31  [Item("SymbolicRegressionSolutionImpactValuesCalculator", "Calculate symbolic expression tree node impact values for regression problems.")]
[8409]32  public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
[10469]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) { }
[8946]43
[10469]44    public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node,
[12720]45      IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation,
[14826]46      double qualityForImpactsCalculation = double.NaN) {
[8946]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
[12720]53      if (double.IsNaN(qualityForImpactsCalculation))
54        qualityForImpactsCalculation = CalculateQualityForImpacts(regressionModel, regressionProblemData, rows);
[8409]55
[8946]56      var cloner = new Cloner();
57      var tempModel = cloner.Clone(regressionModel);
[9840]58      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
[8409]59
[9840]60      var tempModelParentNode = tempModelNode.Parent;
61      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
62
[14826]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)) {
[8935]68
[14826]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;
[8409]89    }
[12720]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    }
[8409]100  }
101}
Note: See TracBrowser for help on using the repository browser.