Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolutionImpactValuesCalculator.cs @ 14906

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

#2650: merged the factors branch into trunk

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model,
43      ISymbolicExpressionTreeNode node,
44      IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue,
45      out double newQualityForImpactsCalculation,
46      double qualityForImpactsCalculation = Double.NaN) {
47      var classificationModel = (ISymbolicClassificationModel)model;
48      var classificationProblemData = (IClassificationProblemData)problemData;
49
50      if (double.IsNaN(qualityForImpactsCalculation))
51        qualityForImpactsCalculation = CalculateQualityForImpacts(classificationModel, classificationProblemData, rows);
52
53
54      var cloner = new Cloner();
55      var tempModel = cloner.Clone(classificationModel);
56      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
57
58      var tempModelParentNode = tempModelNode.Parent;
59      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
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);
66
67        var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue };
68        tempModelParentNode.InsertSubtree(i, constantNode);
69
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;
87    }
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    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.