Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9840 was 9840, checked in by mkommend, 11 years ago

#2021: Fixed a bug node impact caluclation code and adapted the cloner to include a method to get the matching cloned object given the original one.

File size: 3.4 KB
RevLine 
[8409]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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
22using System.Collections.Generic;
[8935]23using HeuristicLab.Common;
[8409]24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25
26namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
27  public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
[8946]28    public override double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows) {
29      var regressionModel = (ISymbolicRegressionModel)model;
30      var regressionProblemData = (IRegressionProblemData)problemData;
31
32      return CalculateReplacementValue(node, regressionModel.SymbolicExpressionTree, regressionModel.Interpreter, regressionProblemData.Dataset, rows);
[8409]33    }
34
[8946]35    public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN) {
36      var regressionModel = (ISymbolicRegressionModel)model;
37      var regressionProblemData = (IRegressionProblemData)problemData;
38
39      var dataset = regressionProblemData.Dataset;
40      var targetValues = dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows);
41
[8409]42      OnlineCalculatorError errorState;
[8946]43      if (double.IsNaN(originalQuality)) {
[9052]44        var originalValues = regressionModel.GetEstimatedValues(dataset, rows);
45        originalQuality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalValues, out errorState);
[8946]46        if (errorState != OnlineCalculatorError.None) originalQuality = 0.0;
47      }
[8409]48
[8946]49      var replacementValue = CalculateReplacementValue(regressionModel, node, regressionProblemData, rows);
50      var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
[9840]51
[8946]52      var cloner = new Cloner();
53      var tempModel = cloner.Clone(regressionModel);
[9840]54      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
[8409]55
[9840]56      var tempModelParentNode = tempModelNode.Parent;
57      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
58      tempModelParentNode.RemoveSubtree(i);
59      tempModelParentNode.InsertSubtree(i, constantNode);
60
[8946]61      var estimatedValues = tempModel.GetEstimatedValues(dataset, rows);
62      double newQuality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, estimatedValues, out errorState);
63      if (errorState != OnlineCalculatorError.None) newQuality = 0.0;
[8935]64
[8946]65      return originalQuality - newQuality;
[8409]66    }
[8946]67
[8409]68  }
69}
Note: See TracBrowser for help on using the repository browser.