Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11552 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 4.8 KB
RevLine 
[8935]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 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;
[10469]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[8409]28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
[10469]30  [StorableClass]
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]
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
[8946]49    public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN) {
[10469]50      double impactValue, replacementValue;
51      CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, originalQuality);
52      return impactValue;
53    }
54
55    public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node,
56      IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue,
57      double originalQuality = Double.NaN) {
[8946]58      var classificationModel = (ISymbolicClassificationModel)model;
59      var classificationProblemData = (IClassificationProblemData)problemData;
60
61      var dataset = classificationProblemData.Dataset;
62      var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
63
[8409]64      OnlineCalculatorError errorState;
[8946]65      if (double.IsNaN(originalQuality)) {
66        var originalClassValues = classificationModel.GetEstimatedClassValues(dataset, rows);
67        originalQuality = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState);
68        if (errorState != OnlineCalculatorError.None) originalQuality = 0.0;
69      }
[8409]70
[10469]71      replacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows);
[8946]72      var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
[10273]73
[8946]74      var cloner = new Cloner();
75      var tempModel = cloner.Clone(classificationModel);
[10273]76      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
[8409]77
[10273]78      var tempModelParentNode = tempModelNode.Parent;
79      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
80      tempModelParentNode.RemoveSubtree(i);
81      tempModelParentNode.InsertSubtree(i, constantNode);
82
[8946]83      var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
84      double newQuality = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues, out errorState);
85      if (errorState != OnlineCalculatorError.None) newQuality = 0.0;
[8409]86
[10469]87      impactValue = originalQuality - newQuality;
[8409]88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.