[8548] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8548] | 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;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[8594] | 27 | using HeuristicLab.Parameters;
|
---|
[8548] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
| 31 | [Item("Penalty Score Evaluator", "Calculates the penalty score of a symbolic classification solution.")]
|
---|
| 32 | [StorableClass]
|
---|
[8594] | 33 | public class SymbolicClassificationSingleObjectivePenaltyScoreEvaluator : SymbolicClassificationSingleObjectiveEvaluator, ISymbolicClassificationModelCreatorOperator {
|
---|
| 34 | private const string ModelCreatorParameterName = "ModelCreator";
|
---|
[8548] | 35 | public override bool Maximization { get { return false; } }
|
---|
| 36 |
|
---|
[8594] | 37 | public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
|
---|
| 38 | get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
|
---|
| 39 | }
|
---|
| 40 | ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter {
|
---|
| 41 | get { return ModelCreatorParameter; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[8548] | 44 | [StorableConstructor]
|
---|
| 45 | protected SymbolicClassificationSingleObjectivePenaltyScoreEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 46 | protected SymbolicClassificationSingleObjectivePenaltyScoreEvaluator(SymbolicClassificationSingleObjectivePenaltyScoreEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
[8594] | 47 | public SymbolicClassificationSingleObjectivePenaltyScoreEvaluator()
|
---|
| 48 | : base() {
|
---|
| 49 | Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
|
---|
| 50 | }
|
---|
[8548] | 51 |
|
---|
| 52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 53 | return new SymbolicClassificationSingleObjectivePenaltyScoreEvaluator(this, cloner);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[8594] | 56 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 57 | private void AfterDeserialization() {
|
---|
[8883] | 58 | // BackwardsCompatibility3.4
|
---|
| 59 | #region Backwards compatible code, remove with 3.5
|
---|
[8594] | 60 | if (!Parameters.ContainsKey(ModelCreatorParameterName))
|
---|
| 61 | Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
|
---|
[8883] | 62 | #endregion
|
---|
[8594] | 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
[10291] | 66 | public override IOperation InstrumentedApply() {
|
---|
[8548] | 67 | double quality = Evaluate(ExecutionContext, SymbolicExpressionTreeParameter.ActualValue, ProblemDataParameter.ActualValue, GenerateRowsToEvaluate());
|
---|
| 68 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
[10291] | 69 | return base.InstrumentedApply();
|
---|
[8548] | 70 | }
|
---|
| 71 |
|
---|
| 72 | public static double Calculate(IClassificationModel model, IClassificationProblemData problemData, IEnumerable<int> rows) {
|
---|
| 73 | var estimations = model.GetEstimatedClassValues(problemData.Dataset, rows).GetEnumerator();
|
---|
| 74 | if (!estimations.MoveNext()) return double.NaN;
|
---|
| 75 |
|
---|
[8549] | 76 | var penalty = 0.0;
|
---|
| 77 | var count = 0;
|
---|
[8548] | 78 | foreach (var r in rows) {
|
---|
| 79 | var actualClass = problemData.Dataset.GetDoubleValue(problemData.TargetVariable, r);
|
---|
| 80 | penalty += problemData.GetClassificationPenalty(actualClass, estimations.Current);
|
---|
| 81 | estimations.MoveNext();
|
---|
[8549] | 82 | count++;
|
---|
[8548] | 83 | }
|
---|
[8549] | 84 | return penalty / count;
|
---|
[8548] | 85 | }
|
---|
| 86 |
|
---|
| 87 | public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
|
---|
| 88 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
| 89 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
[8594] | 90 | ModelCreatorParameter.ExecutionContext = context;
|
---|
[8664] | 91 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
[8548] | 92 |
|
---|
[8594] | 93 | var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel(tree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
|
---|
[8972] | 94 | if (ApplyLinearScalingParameter.ActualValue.Value) model.Scale(problemData);
|
---|
[8594] | 95 | model.RecalculateModelParameters(problemData, rows);
|
---|
[8548] | 96 | double penalty = Calculate(model, problemData, rows);
|
---|
| 97 |
|
---|
| 98 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
| 99 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
[8594] | 100 | ModelCreatorParameter.ExecutionContext = null;
|
---|
[8664] | 101 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
[8548] | 102 |
|
---|
| 103 | return penalty;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|