Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolutionImpactValuesCalculator.cs @ 14362

Last change on this file since 14362 was 12720, checked in by bburlacu, 9 years ago

#2359: Changed the impact calculators so that the quality value necessary for impacts calculation is calculated with a separate method. Refactored the CalculateImpactAndReplacementValues method to return the new quality in an out-parameter (adjusted method signature in interface accordingly). Added Evaluate method to the regression and classification pruning operators that re-evaluates the tree using the problem evaluator after pruning was performed.

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Classification {
30  [StorableClass]
31  [Item("SymbolicClassificationSolutionImpactValuesCalculator", "Calculate symbolic expression tree node impact values for classification problems.")]
32  public class SymbolicClassificationSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
33    public SymbolicClassificationSolutionImpactValuesCalculator() { }
34    protected SymbolicClassificationSolutionImpactValuesCalculator(SymbolicClassificationSolutionImpactValuesCalculator original, Cloner cloner)
35      : base(original, cloner) { }
36    public override IDeepCloneable Clone(Cloner cloner) {
37      return new SymbolicClassificationSolutionImpactValuesCalculator(this, cloner);
38    }
39    [StorableConstructor]
40    protected SymbolicClassificationSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { }
41
42    public override double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows) {
43      var classificationModel = (ISymbolicClassificationModel)model;
44      var classificationProblemData = (IClassificationProblemData)problemData;
45
46      return CalculateReplacementValue(node, classificationModel.SymbolicExpressionTree, classificationModel.Interpreter, classificationProblemData.Dataset, rows);
47    }
48
49    public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double qualityForImpactsCalculation = double.NaN) {
50      double impactValue, replacementValue;
51      double newQualityForImpactsCalculation;
52      CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, out newQualityForImpactsCalculation, qualityForImpactsCalculation);
53      return impactValue;
54    }
55
56    public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node,
57      IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation,
58      double qualityForImpactsCalculation = Double.NaN) {
59      var classificationModel = (ISymbolicClassificationModel)model;
60      var classificationProblemData = (IClassificationProblemData)problemData;
61
62      if (double.IsNaN(qualityForImpactsCalculation))
63        qualityForImpactsCalculation = CalculateQualityForImpacts(classificationModel, classificationProblemData, rows);
64
65      replacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows);
66      var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
67
68      var cloner = new Cloner();
69      var tempModel = cloner.Clone(classificationModel);
70      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
71
72      var tempModelParentNode = tempModelNode.Parent;
73      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
74      tempModelParentNode.RemoveSubtree(i);
75      tempModelParentNode.InsertSubtree(i, constantNode);
76
77      OnlineCalculatorError errorState;
78      var dataset = classificationProblemData.Dataset;
79      var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
80      var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
81      newQualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues, out errorState);
82      if (errorState != OnlineCalculatorError.None) newQualityForImpactsCalculation = 0.0;
83
84      impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
85    }
86
87    public static double CalculateQualityForImpacts(ISymbolicClassificationModel model, IClassificationProblemData problemData, IEnumerable<int> rows) {
88      OnlineCalculatorError errorState;
89      var dataset = problemData.Dataset;
90      var targetClassValues = dataset.GetDoubleValues(problemData.TargetVariable, rows);
91      var originalClassValues = model.GetEstimatedClassValues(dataset, rows);
92      var qualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState);
93      if (errorState != OnlineCalculatorError.None) qualityForImpactsCalculation = 0.0;
94
95      return qualityForImpactsCalculation;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.