Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolutionImpactValuesCalculator.cs @ 16101

Last change on this file since 16101 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 5.2 KB
RevLine 
[8935]1#region License Information
2/* HeuristicLab
[14185]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
[10469]22using System;
[8935]23using System.Collections.Generic;
[8409]24using HeuristicLab.Common;
[10469]25using HeuristicLab.Core;
[8409]26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[14927]27using HeuristicLab.Persistence;
[8409]28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
[14927]30  [StorableType("a9d6ca7c-519e-417a-90ec-f7f42c27820a")]
[10469]31  [Item("SymbolicClassificationSolutionImpactValuesCalculator", "Calculate symbolic expression tree node impact values for classification problems.")]
[8946]32  public class SymbolicClassificationSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
[10469]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]
[15018]40    protected SymbolicClassificationSolutionImpactValuesCalculator(StorableConstructorFlag deserializing) : base(deserializing) { }
[10469]41
[14826]42    public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model,
43      ISymbolicExpressionTreeNode node,
44      IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue,
45      out double newQualityForImpactsCalculation,
[12720]46      double qualityForImpactsCalculation = Double.NaN) {
[8946]47      var classificationModel = (ISymbolicClassificationModel)model;
48      var classificationProblemData = (IClassificationProblemData)problemData;
49
[12720]50      if (double.IsNaN(qualityForImpactsCalculation))
51        qualityForImpactsCalculation = CalculateQualityForImpacts(classificationModel, classificationProblemData, rows);
[8946]52
[10273]53
[8946]54      var cloner = new Cloner();
55      var tempModel = cloner.Clone(classificationModel);
[10273]56      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
[8409]57
[10273]58      var tempModelParentNode = tempModelNode.Parent;
59      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
[14826]60      double bestReplacementValue = 0.0;
61      double bestImpactValue = double.PositiveInfinity;
62      newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize
63      // try the potentially reasonable replacement values and use the best one
64      foreach (var repValue in CalculateReplacementValues(node, classificationModel.SymbolicExpressionTree, classificationModel.Interpreter, classificationProblemData.Dataset, classificationProblemData.TrainingIndices)) {
65        tempModelParentNode.RemoveSubtree(i);
[10273]66
[14826]67        var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue };
68        tempModelParentNode.InsertSubtree(i, constantNode);
[8409]69
[14826]70        var dataset = classificationProblemData.Dataset;
71        var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
72        var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
73        OnlineCalculatorError errorState;
74        newQualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues,
75          out errorState);
76        if (errorState != OnlineCalculatorError.None) newQualityForImpactsCalculation = 0.0;
77
78        impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
79
80        if (impactValue < bestImpactValue) {
81          bestImpactValue = impactValue;
82          bestReplacementValue = repValue;
83        }
84      }
85      replacementValue = bestReplacementValue;
86      impactValue = bestImpactValue;
[8409]87    }
[12720]88
89    public static double CalculateQualityForImpacts(ISymbolicClassificationModel model, IClassificationProblemData problemData, IEnumerable<int> rows) {
90      OnlineCalculatorError errorState;
91      var dataset = problemData.Dataset;
92      var targetClassValues = dataset.GetDoubleValues(problemData.TargetVariable, rows);
93      var originalClassValues = model.GetEstimatedClassValues(dataset, rows);
94      var qualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState);
95      if (errorState != OnlineCalculatorError.None) qualityForImpactsCalculation = 0.0;
96
97      return qualityForImpactsCalculation;
98    }
[8409]99  }
100}
Note: See TracBrowser for help on using the repository browser.