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