[8935] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[8409] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
[8946] | 27 | public class SymbolicClassificationSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
|
---|
| 28 | public override double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows) {
|
---|
| 29 | var classificationModel = (ISymbolicClassificationModel)model;
|
---|
| 30 | var classificationProblemData = (IClassificationProblemData)problemData;
|
---|
| 31 |
|
---|
| 32 | return CalculateReplacementValue(node, classificationModel.SymbolicExpressionTree, classificationModel.Interpreter, classificationProblemData.Dataset, rows);
|
---|
[8409] | 33 | }
|
---|
[8935] | 34 |
|
---|
[8946] | 35 | public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN) {
|
---|
| 36 | var classificationModel = (ISymbolicClassificationModel)model;
|
---|
| 37 | var classificationProblemData = (IClassificationProblemData)problemData;
|
---|
| 38 |
|
---|
| 39 | var dataset = classificationProblemData.Dataset;
|
---|
| 40 | var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
|
---|
| 41 |
|
---|
[8409] | 42 | OnlineCalculatorError errorState;
|
---|
[8946] | 43 | if (double.IsNaN(originalQuality)) {
|
---|
| 44 | var originalClassValues = classificationModel.GetEstimatedClassValues(dataset, rows);
|
---|
| 45 | originalQuality = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState);
|
---|
| 46 | if (errorState != OnlineCalculatorError.None) originalQuality = 0.0;
|
---|
| 47 | }
|
---|
[8409] | 48 |
|
---|
[8946] | 49 | var replacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows);
|
---|
| 50 | var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
|
---|
| 51 | var cloner = new Cloner();
|
---|
| 52 | cloner.RegisterClonedObject(node, constantNode);
|
---|
| 53 | var tempModel = cloner.Clone(classificationModel);
|
---|
| 54 | tempModel.RecalculateModelParameters(classificationProblemData, rows);
|
---|
[8409] | 55 |
|
---|
[8946] | 56 | var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
|
---|
| 57 | double newQuality = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues, out errorState);
|
---|
| 58 | if (errorState != OnlineCalculatorError.None) newQuality = 0.0;
|
---|
[8409] | 59 |
|
---|
[8946] | 60 | return originalQuality - newQuality;
|
---|
[8409] | 61 | }
|
---|
[8946] | 62 |
|
---|
[8409] | 63 | }
|
---|
| 64 | }
|
---|