[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] | 22 | using System;
|
---|
[8935] | 23 | using System.Collections.Generic;
|
---|
[8409] | 24 | using HeuristicLab.Common;
|
---|
[10469] | 25 | using HeuristicLab.Core;
|
---|
[8409] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[10469] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[8409] | 28 |
|
---|
| 29 | namespace 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 |
|
---|
[12720] | 49 | public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double qualityForImpactsCalculation = double.NaN) {
|
---|
[10469] | 50 | double impactValue, replacementValue;
|
---|
[12720] | 51 | double newQualityForImpactsCalculation;
|
---|
| 52 | CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, out newQualityForImpactsCalculation, qualityForImpactsCalculation);
|
---|
[10469] | 53 | return impactValue;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node,
|
---|
[12720] | 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 |
|
---|
[12720] | 62 | if (double.IsNaN(qualityForImpactsCalculation))
|
---|
| 63 | qualityForImpactsCalculation = CalculateQualityForImpacts(classificationModel, classificationProblemData, rows);
|
---|
[8946] | 64 |
|
---|
[10469] | 65 | replacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows);
|
---|
[8946] | 66 | var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
|
---|
[10273] | 67 |
|
---|
[8946] | 68 | var cloner = new Cloner();
|
---|
| 69 | var tempModel = cloner.Clone(classificationModel);
|
---|
[10273] | 70 | var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
|
---|
[8409] | 71 |
|
---|
[10273] | 72 | var tempModelParentNode = tempModelNode.Parent;
|
---|
| 73 | int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
|
---|
| 74 | tempModelParentNode.RemoveSubtree(i);
|
---|
| 75 | tempModelParentNode.InsertSubtree(i, constantNode);
|
---|
| 76 |
|
---|
[12720] | 77 | OnlineCalculatorError errorState;
|
---|
| 78 | var dataset = classificationProblemData.Dataset;
|
---|
| 79 | var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
|
---|
[8946] | 80 | var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
|
---|
[12720] | 81 | newQualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues, out errorState);
|
---|
| 82 | if (errorState != OnlineCalculatorError.None) newQualityForImpactsCalculation = 0.0;
|
---|
[8409] | 83 |
|
---|
[12720] | 84 | impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
|
---|
[8409] | 85 | }
|
---|
[12720] | 86 |
|
---|
| 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 | }
|
---|