Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SensitivityEvaluator/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator.cs @ 12448

Last change on this file since 12448 was 12418, checked in by ehopf, 10 years ago

#2361: Added backward compatibility code for the WeightedPerformanceMeasuresEvaluator.

File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.SingleObjective {
33  [Item("Weighted Performance Measures Evaluator", "Calculates the quality of a symbolic classification solution based on three weighted measures(normalized mean squared error, false negative rate(1-sensitivity) and false positve rate(1-specificity)).")]
34  [StorableClass]
35  public class SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator : SymbolicClassificationSingleObjectiveEvaluator {
36    private const string NormalizedMeanSquaredErrorWeightingFactorParameterName = "NormalizedMeanSquaredErrorWeightingFactor";
37    private const string FalseNegativeRateWeightingFactorParameterName = "FalseNegativeRateWeightingFactor";
38    private const string FalsePositiveRateWeightingFactorParameterName = "FalsePositiveRateWeightingFactor";
39    private const string ModelCreatorParameterName = "ModelCreator";
40
41    public override bool Maximization { get { return false; } }
42
43    #region parameter properties
44    public IFixedValueParameter<DoubleValue> NormalizedMeanSquaredErrorWeightingFactorParameter {
45      get { return (IFixedValueParameter<DoubleValue>)Parameters[NormalizedMeanSquaredErrorWeightingFactorParameterName]; }
46    }
47    public IFixedValueParameter<DoubleValue> FalseNegativeRateWeightingFactorParameter {
48      get { return (IFixedValueParameter<DoubleValue>)Parameters[FalseNegativeRateWeightingFactorParameterName]; }
49    }
50    public IFixedValueParameter<DoubleValue> FalsePositiveRateWeightingFactorParameter {
51      get { return (IFixedValueParameter<DoubleValue>)Parameters[FalsePositiveRateWeightingFactorParameterName]; }
52    }
53    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
54      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
55    }
56    #endregion
57
58    public double NormalizedMeanSquaredErrorWeightingFactor {
59      get {
60        return NormalizedMeanSquaredErrorWeightingFactorParameter.Value.Value;
61      }
62    }
63    public double FalseNegativeRateWeightingFactor {
64      get {
65        return FalseNegativeRateWeightingFactorParameter.Value.Value;
66      }
67    }
68    public double FalsePositiveRateWeightingFactor {
69      get {
70        return FalsePositiveRateWeightingFactorParameter.Value.Value;
71      }
72    }
73
74    [StorableConstructor]
75    protected SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator(bool deserializing) : base(deserializing) { }
76    protected SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator(SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator original, Cloner cloner)
77      : base(original, cloner) {
78    }
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator(this, cloner);
81    }
82
83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      // BackwardsCompatibility
86      #region Backwards compatible code
87      if (Parameters.ContainsKey(ModelCreatorParameterName)) {
88        Parameters.Remove(ModelCreatorParameterName);
89        Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "The model creator which is used during the evaluations."));
90      }
91      #endregion
92    }
93
94    public SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator()
95      : base() {
96      Parameters.Add(new FixedValueParameter<DoubleValue>(NormalizedMeanSquaredErrorWeightingFactorParameterName, "The weighting factor of the normalized mean squared error.", new DoubleValue(1)));
97      Parameters.Add(new FixedValueParameter<DoubleValue>(FalseNegativeRateWeightingFactorParameterName, "The weighting factor of the false negative rate (1-sensitivity).", new DoubleValue(1)));
98      Parameters.Add(new FixedValueParameter<DoubleValue>(FalsePositiveRateWeightingFactorParameterName, "The weighting factor of the false positive rate (1-specificity).", new DoubleValue(1)));
99      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "The model creator which is used during the evaluations."));
100    }
101
102    public override IOperation InstrumentedApply() {
103      IEnumerable<int> rows = GenerateRowsToEvaluate();
104      var solution = SymbolicExpressionTreeParameter.ActualValue;
105      var creator = ModelCreatorParameter.ActualValue;
106      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
107              ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value, creator, NormalizedMeanSquaredErrorWeightingFactor, FalseNegativeRateWeightingFactor, FalsePositiveRateWeightingFactor);
108      QualityParameter.ActualValue = new DoubleValue(quality);
109      return base.InstrumentedApply();
110    }
111
112    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData,
113                IEnumerable<int> rows, bool applyLinearScaling, ISymbolicClassificationModelCreator modelCreator, double normalizedMeanSquaredErrorWeightingFactor, double falseNegativeRateWeightingFactor, double falsePositiveRateWeightingFactor) {
114      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
115      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
116      var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit).ToArray();
117      OnlineCalculatorError errorState;
118      double nmse;
119
120      //calculate performance measures
121      var model = (ISymbolicDiscriminantFunctionClassificationModel)modelCreator.CreateSymbolicClassificationModel(solution, interpreter, lowerEstimationLimit, upperEstimationLimit);
122      string positiveClassName = problemData.PositiveClass;
123      double[] classValues, thresholds;
124      model.ThresholdCalculator.Calculate(problemData, boundedEstimatedValues, targetClassValues, out classValues, out thresholds);
125      model.SetThresholdsAndClassValues(thresholds, classValues);
126      var performanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassName, problemData.GetClassValue(positiveClassName));
127      var estimatedClassValues = model.GetEstimatedClassValues(boundedEstimatedValues);
128      performanceCalculator.Calculate(targetClassValues, estimatedClassValues);
129      if (performanceCalculator.ErrorState != OnlineCalculatorError.None)
130        return Double.NaN;
131      double falseNegativeRate = 1 - performanceCalculator.TruePositiveRate;
132      double falsePositiveRate = performanceCalculator.FalsePositiveRate;
133
134      if (applyLinearScaling) {
135        throw new NotSupportedException("The Weighted Performance Measures Evaluator does not suppport linear scaling!");
136      }
137      nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetClassValues, boundedEstimatedValues, out errorState);
138      if (errorState != OnlineCalculatorError.None) return Double.NaN;
139      return normalizedMeanSquaredErrorWeightingFactor * nmse + falseNegativeRateWeightingFactor * falseNegativeRate + falsePositiveRateWeightingFactor * falsePositiveRate;
140    }
141
142    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
143      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
144      EstimationLimitsParameter.ExecutionContext = context;
145      ApplyLinearScalingParameter.ExecutionContext = context;
146      ModelCreatorParameter.ExecutionContext = context;
147
148      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
149                                      problemData, rows, ApplyLinearScalingParameter.ActualValue.Value, ModelCreatorParameter.ActualValue, NormalizedMeanSquaredErrorWeightingFactorParameter.Value.Value, FalseNegativeRateWeightingFactor, FalsePositiveRateWeightingFactor);
150
151      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
152      EstimationLimitsParameter.ExecutionContext = null;
153      ApplyLinearScalingParameter.ExecutionContext = null;
154      ModelCreatorParameter.ExecutionContext = null;
155
156      return quality;
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.