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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
33 | [Item("Weighted Residuals Mean Squared Error Evaluator", @"A modified mean squared error evaluator that enables the possibility to weight residuals differently.
|
---|
34 | The first residual category belongs to estimated values which definitely belong to a specific class because the estimated value is located above the maximum or below the minimum of all the class values (DefiniteResidualsWeight).
|
---|
35 | The second residual category represents residuals which belong to the positive class whereby the estimated value is located between the positive and a negative class (PositiveClassResidualsWeight).
|
---|
36 | All other cases are represented by the third category (NegativeClassesResidualsWeight).
|
---|
37 | The weight gets multiplied to the squared error. Note that the Evaluator acts like a normal MSE-Evaluator if all the weights are set to 1.")]
|
---|
38 | [StorableClass]
|
---|
39 | public class SymbolicClassificationSingleObjectiveWeightedResidualsMeanSquaredErrorEvaluator : SymbolicClassificationSingleObjectiveEvaluator {
|
---|
40 | private const string DefiniteResidualsWeightParameterName = "DefiniteResidualsWeight";
|
---|
41 | private const string PositiveClassResidualsWeightParameterName = "PositiveClassResidualsWeight";
|
---|
42 | private const string NegativeClassesResidualsWeightParameterName = "NegativeClassesResidualsWeight";
|
---|
43 | [StorableConstructor]
|
---|
44 | protected SymbolicClassificationSingleObjectiveWeightedResidualsMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { }
|
---|
45 | protected SymbolicClassificationSingleObjectiveWeightedResidualsMeanSquaredErrorEvaluator(SymbolicClassificationSingleObjectiveWeightedResidualsMeanSquaredErrorEvaluator original, Cloner cloner)
|
---|
46 | : base(original, cloner) {
|
---|
47 | }
|
---|
48 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
49 | return new SymbolicClassificationSingleObjectiveWeightedResidualsMeanSquaredErrorEvaluator(this, cloner);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public SymbolicClassificationSingleObjectiveWeightedResidualsMeanSquaredErrorEvaluator()
|
---|
53 | : base() {
|
---|
54 | Parameters.Add(new FixedValueParameter<DoubleValue>(DefiniteResidualsWeightParameterName, "Weight of residuals which definitely belong to a specific class because the estimated values is located above the maximum or below the minimum of all the class values.", new DoubleValue(1)));
|
---|
55 | Parameters.Add(new FixedValueParameter<DoubleValue>(PositiveClassResidualsWeightParameterName, "Weight of residuals which belong to the positive class whereby the estimated value is located between the positive and a negative class.", new DoubleValue(1)));
|
---|
56 | Parameters.Add(new FixedValueParameter<DoubleValue>(NegativeClassesResidualsWeightParameterName, "Weight of residuals which are not covered by the DefiniteResidualsWeight or the PositiveClassResidualsWeight.", new DoubleValue(1)));
|
---|
57 | }
|
---|
58 |
|
---|
59 | #region parameter properties
|
---|
60 | public IFixedValueParameter<DoubleValue> DefiniteResidualsWeightParameter {
|
---|
61 | get { return (IFixedValueParameter<DoubleValue>)Parameters[DefiniteResidualsWeightParameterName]; }
|
---|
62 | }
|
---|
63 | public IFixedValueParameter<DoubleValue> PositiveClassResidualsWeightParameter {
|
---|
64 | get { return (IFixedValueParameter<DoubleValue>)Parameters[PositiveClassResidualsWeightParameterName]; }
|
---|
65 | }
|
---|
66 | public IFixedValueParameter<DoubleValue> NegativeClassesResidualsWeightParameter {
|
---|
67 | get { return (IFixedValueParameter<DoubleValue>)Parameters[NegativeClassesResidualsWeightParameterName]; }
|
---|
68 | }
|
---|
69 | #endregion
|
---|
70 |
|
---|
71 | #region properties
|
---|
72 | public override bool Maximization { get { return false; } }
|
---|
73 |
|
---|
74 | public double DefiniteResidualsWeight {
|
---|
75 | get {
|
---|
76 | return DefiniteResidualsWeightParameter.Value.Value;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | public double PositiveClassResidualsWeight {
|
---|
80 | get {
|
---|
81 | return PositiveClassResidualsWeightParameter.Value.Value;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | public double NegativeClassesResidualsWeight {
|
---|
85 | get {
|
---|
86 | return NegativeClassesResidualsWeightParameter.Value.Value;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | #endregion
|
---|
90 |
|
---|
91 | public override IOperation InstrumentedApply() {
|
---|
92 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
93 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
94 | double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value,
|
---|
95 | DefiniteResidualsWeight, PositiveClassResidualsWeight, NegativeClassesResidualsWeight);
|
---|
96 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
97 | return base.InstrumentedApply();
|
---|
98 | }
|
---|
99 |
|
---|
100 | public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling,
|
---|
101 | double definiteResidualsWeight, double positiveClassResidualsWeight, double negativeClassesResidualsWeight) {
|
---|
102 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
|
---|
103 | IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
104 | OnlineCalculatorError errorState;
|
---|
105 |
|
---|
106 | double positiveClassValue = problemData.GetClassValue(problemData.PositiveClass);
|
---|
107 | //get class values min/max
|
---|
108 | double classValuesMin = problemData.ClassValues.ElementAtOrDefault(0);
|
---|
109 | double classValuesMax = classValuesMin;
|
---|
110 | foreach (double classValue in problemData.ClassValues) {
|
---|
111 | if (classValuesMin > classValue) classValuesMin = classValue;
|
---|
112 | if (classValuesMax < classValue) classValuesMax = classValue;
|
---|
113 | }
|
---|
114 |
|
---|
115 | double quality;
|
---|
116 | if (applyLinearScaling) {
|
---|
117 | var calculator = new OnlineWeightedResidualsMeanSquaredErrorCalculator(positiveClassValue, classValuesMax, classValuesMin,
|
---|
118 | definiteResidualsWeight, positiveClassResidualsWeight, negativeClassesResidualsWeight);
|
---|
119 | CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, calculator, problemData.Dataset.Rows);
|
---|
120 | errorState = calculator.ErrorState;
|
---|
121 | quality = calculator.WeightedResidualsMeanSquaredError;
|
---|
122 | } else {
|
---|
123 | IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
124 | quality = OnlineWeightedResidualsMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, positiveClassValue, classValuesMax,
|
---|
125 | classValuesMin, definiteResidualsWeight, positiveClassResidualsWeight, negativeClassesResidualsWeight, out errorState);
|
---|
126 | }
|
---|
127 | if (errorState != OnlineCalculatorError.None) return Double.NaN;
|
---|
128 | return quality;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
|
---|
132 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
133 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
134 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
135 |
|
---|
136 | double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value, DefiniteResidualsWeight, PositiveClassResidualsWeight, NegativeClassesResidualsWeight);
|
---|
137 |
|
---|
138 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
139 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
140 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
141 |
|
---|
142 | return quality;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|