Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectivePenaltyScoreEvaluator.cs @ 14027

Last change on this file since 14027 was 14027, checked in by mkommend, 8 years ago

#2604: Merged r13826,r13921, r13922, r13941, r13992, r13993, r14000 intos table.

File size: 5.2 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
31  [Item("Penalty Score Evaluator", "Calculates the penalty score of a symbolic classification solution.")]
32  [StorableClass]
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(bool 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.