Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolutionImpactValuesCalculator.cs @ 14813

Last change on this file since 14813 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.5 KB
RevLine 
[8935]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8935]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
[11145]22using System;
[8935]23using System.Collections.Generic;
[8409]24using HeuristicLab.Common;
[11145]25using HeuristicLab.Core;
[8409]26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[11145]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[8409]28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
[11145]30  [StorableClass]
31  [Item("SymbolicClassificationSolutionImpactValuesCalculator", "Calculate symbolic expression tree node impact values for classification problems.")]
[8946]32  public class SymbolicClassificationSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
[11145]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
[8946]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);
[8409]47    }
[8935]48
[12745]49    public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double qualityForImpactsCalculation = double.NaN) {
[11145]50      double impactValue, replacementValue;
[12745]51      double newQualityForImpactsCalculation;
52      CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, out newQualityForImpactsCalculation, qualityForImpactsCalculation);
[11145]53      return impactValue;
54    }
55
56    public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node,
[12745]57      IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation,
58      double qualityForImpactsCalculation = Double.NaN) {
[8946]59      var classificationModel = (ISymbolicClassificationModel)model;
60      var classificationProblemData = (IClassificationProblemData)problemData;
61
[12745]62      if (double.IsNaN(qualityForImpactsCalculation))
63        qualityForImpactsCalculation = CalculateQualityForImpacts(classificationModel, classificationProblemData, rows);
[8946]64
[11145]65      replacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows);
[8946]66      var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
[10281]67
[8946]68      var cloner = new Cloner();
69      var tempModel = cloner.Clone(classificationModel);
[10281]70      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
[8409]71
[10281]72      var tempModelParentNode = tempModelNode.Parent;
73      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
74      tempModelParentNode.RemoveSubtree(i);
75      tempModelParentNode.InsertSubtree(i, constantNode);
76
[12745]77      OnlineCalculatorError errorState;
78      var dataset = classificationProblemData.Dataset;
79      var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
[8946]80      var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
[12745]81      newQualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues, out errorState);
82      if (errorState != OnlineCalculatorError.None) newQualityForImpactsCalculation = 0.0;
[8409]83
[12745]84      impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
[8409]85    }
[12745]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    }
[8409]97  }
98}
Note: See TracBrowser for help on using the repository browser.