Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12210 was 12210, checked in by ehopf, 9 years ago

#2361: Implemented the first prototype of the new evaluator.

File size: 7.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
10using HeuristicLab.Parameters;
11using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
12
13namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.SingleObjective {
14  [Item("Weighted Performance Measures Evaluator", "Description")]
15  [StorableClass]
16  public class SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator : SymbolicClassificationSingleObjectiveEvaluator {
17    private const string NormalizedMeanSquaredErrorWeightingFactorParameterName = "NormalizedMeanSquaredErrorWeightingFactor";
18    private const string BetaParameterName = "Beta";
19    private const string GammaParameterName = "Gamma";
20    private const string ModelCreatorParameterName = "ModelCreator";
21
22    public override bool Maximization { get { return false; } }
23
24    #region parameter properties
25    public IFixedValueParameter<DoubleValue> NormalizedMeanSquaredErrorWeightingFactorParameter {
26      get { return (IFixedValueParameter<DoubleValue>)Parameters[NormalizedMeanSquaredErrorWeightingFactorParameterName]; }
27    }
28    public IFixedValueParameter<DoubleValue> BetaParameter {
29      get { return (IFixedValueParameter<DoubleValue>)Parameters[BetaParameterName]; }
30    }
31    public IFixedValueParameter<DoubleValue> GammaParameter {
32      get { return (IFixedValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
33    }
34    public ILookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
35      get { return (ILookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
36    }
37    #endregion
38
39    [StorableConstructor]
40    protected SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator(bool deserializing) : base(deserializing) { }
41    protected SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator(SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator original, Cloner cloner)
42      : base(original, cloner) {
43    }
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator(this, cloner);
46    }
47
48    public SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator()
49      : base() {
50      Parameters.Add(new FixedValueParameter<DoubleValue>(NormalizedMeanSquaredErrorWeightingFactorParameterName, "The weighting factor of the normalized mean squared error.", new DoubleValue(1)));
51      Parameters.Add(new FixedValueParameter<DoubleValue>(BetaParameterName, "Beta1", new DoubleValue(1)));
52      Parameters.Add(new FixedValueParameter<DoubleValue>(GammaParameterName, "Gamma1", new DoubleValue(1)));
53      Parameters.Add(new LookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "ModelCreator"));
54    }
55
56    public override IOperation InstrumentedApply() {
57      IEnumerable<int> rows = GenerateRowsToEvaluate();
58      var solution = SymbolicExpressionTreeParameter.ActualValue;
59      var creator = ModelCreatorParameter.ActualValue;
60      double normalizedMeanSquaredErrorWeightingFactor = NormalizedMeanSquaredErrorWeightingFactorParameter.Value.Value;
61      double beta = BetaParameter.Value.Value;
62      double gamma = GammaParameter.Value.Value;
63      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
64              ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value, creator, normalizedMeanSquaredErrorWeightingFactor, beta, gamma);
65      QualityParameter.ActualValue = new DoubleValue(quality);
66      return base.InstrumentedApply();
67    }
68
69    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData,
70                IEnumerable<int> rows, bool applyLinearScaling, ISymbolicClassificationModelCreator modelCreator, double normalizedMeanSquaredErrorWeightingFactor, double betaParameter, double gammaParameter) {
71      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
72      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
73      OnlineCalculatorError errorState;
74      double nmse;
75     
76      //calculate quality measures
77      var model = modelCreator.CreateSymbolicClassificationModel(solution, interpreter, lowerEstimationLimit, upperEstimationLimit);
78      string positiveClassName = problemData.PositiveClass;
79      model.RecalculateModelParameters(problemData, rows);
80      var performanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassName, problemData.GetClassValue(positiveClassName));
81      var estimated = model.GetEstimatedClassValues(problemData.Dataset, rows);
82      performanceCalculator.Calculate(estimated, targetValues);
83      if (performanceCalculator.ErrorState != OnlineCalculatorError.None) return Double.NaN;
84      double falsePositiveRate = performanceCalculator.FalsePositiveRate;
85      double falseNegativeRate = 1 - performanceCalculator.TruePositiveRate;
86
87      if (applyLinearScaling) {
88        var nmseCalculator = new OnlineNormalizedMeanSquaredErrorCalculator();
89        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, nmseCalculator, problemData.Dataset.Rows);
90        errorState = nmseCalculator.ErrorState;
91        nmse = nmseCalculator.NormalizedMeanSquaredError;
92      } else {
93        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
94        nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
95      }
96      if (errorState != OnlineCalculatorError.None) return Double.NaN;
97      return normalizedMeanSquaredErrorWeightingFactor * nmse + betaParameter * falsePositiveRate + gammaParameter * falseNegativeRate;
98    }
99
100    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
101      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
102      EstimationLimitsParameter.ExecutionContext = context;
103      ApplyLinearScalingParameter.ExecutionContext = context;
104      ModelCreatorParameter.ExecutionContext = context;
105
106      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
107                                      problemData, rows, ApplyLinearScalingParameter.ActualValue.Value, ModelCreatorParameter.ActualValue, NormalizedMeanSquaredErrorWeightingFactorParameter.Value.Value, BetaParameter.Value.Value, GammaParameter.Value.Value);
108
109      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
110      EstimationLimitsParameter.ExecutionContext = null;
111      ApplyLinearScalingParameter.ExecutionContext = null;
112      ModelCreatorParameter.ExecutionContext = null;
113
114      return quality;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.