Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectivePenaltyScoreEvaluator.cs @ 15428

Last change on this file since 15428 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
31  [Item("Penalty Score Evaluator", "Calculates the penalty score of a symbolic classification solution.")]
32  [StorableType("68c6d9c5-0718-49e7-bf5c-d144234cefc3")]
33  public class SymbolicClassificationSingleObjectivePenaltyScoreEvaluator : SymbolicClassificationSingleObjectiveEvaluator, ISymbolicClassificationModelCreatorOperator {
34    private const string ModelCreatorParameterName = "ModelCreator";
35    public override bool Maximization { get { return false; } }
36
37    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
38      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
39    }
40    ILookupParameter<ISymbolicClassificationModelCreator> ISymbolicClassificationModelCreatorOperator.ModelCreatorParameter {
41      get { return ModelCreatorParameter; }
42    }
43
44    [StorableConstructor]
45    protected SymbolicClassificationSingleObjectivePenaltyScoreEvaluator(StorableConstructorFlag deserializing) : base(deserializing) { }
46    protected SymbolicClassificationSingleObjectivePenaltyScoreEvaluator(SymbolicClassificationSingleObjectivePenaltyScoreEvaluator original, Cloner cloner) : base(original, cloner) { }
47    public SymbolicClassificationSingleObjectivePenaltyScoreEvaluator()
48      : base() {
49      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new SymbolicClassificationSingleObjectivePenaltyScoreEvaluator(this, cloner);
54    }
55
56    [StorableHook(HookType.AfterDeserialization)]
57    private void AfterDeserialization() {
58      // BackwardsCompatibility3.4
59      #region Backwards compatible code, remove with 3.5
60      if (!Parameters.ContainsKey(ModelCreatorParameterName))
61        Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
62      #endregion
63    }
64
65
66    public override IOperation InstrumentedApply() {
67      double quality = Evaluate(ExecutionContext, SymbolicExpressionTreeParameter.ActualValue, ProblemDataParameter.ActualValue, GenerateRowsToEvaluate());
68      QualityParameter.ActualValue = new DoubleValue(quality);
69      return base.InstrumentedApply();
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
76      var penalty = 0.0;
77      var count = 0;
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();
82        count++;
83      }
84      return penalty / count;
85    }
86
87    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
88      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
89      EstimationLimitsParameter.ExecutionContext = context;
90      ModelCreatorParameter.ExecutionContext = context;
91      ApplyLinearScalingParameter.ExecutionContext = context;
92
93      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel(problemData.TargetVariable, tree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
94      if (ApplyLinearScalingParameter.ActualValue.Value) model.Scale(problemData);
95      model.RecalculateModelParameters(problemData, rows);
96      double penalty = Calculate(model, problemData, rows);
97
98      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
99      EstimationLimitsParameter.ExecutionContext = null;
100      ModelCreatorParameter.ExecutionContext = null;
101      ApplyLinearScalingParameter.ExecutionContext = null;
102
103      return penalty;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.