Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolutionImpactValuesCalculator.cs @ 11512

Last change on this file since 11512 was 10469, checked in by bburlacu, 11 years ago

#2143: Refactored pruning analyzer and operators as per review.

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 originalQuality = double.NaN) {
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) {
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
64      OnlineCalculatorError errorState;
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      }
70
71      replacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows);
72      var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
73
74      var cloner = new Cloner();
75      var tempModel = cloner.Clone(classificationModel);
76      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
77
78      var tempModelParentNode = tempModelNode.Parent;
79      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
80      tempModelParentNode.RemoveSubtree(i);
81      tempModelParentNode.InsertSubtree(i, constantNode);
82
83      var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
84      double newQuality = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues, out errorState);
85      if (errorState != OnlineCalculatorError.None) newQuality = 0.0;
86
87      impactValue = originalQuality - newQuality;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.