Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectivePenaltyScoreEvaluator.cs @ 8594

Last change on this file since 8594 was 8594, checked in by mkommend, 12 years ago

#1940: Added support in symbolic classification for different methods to create the classification ModelCreator.

  • Added ModelCreators
  • Refactored SymbolicClassificationModel and SymbolicDiscriminantFunctionClassificationModel
  • Added ModelCreatorParameter to Analyzers and Evaluators if needed
  • Corrected wiring in symbolic classification problems (single- and multiobjective
  • Adapted simplifier
File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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      if (!Parameters.ContainsKey(ModelCreatorParameterName))
59        Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
60    }
61
62
63    public override IOperation Apply() {
64      double quality = Evaluate(ExecutionContext, SymbolicExpressionTreeParameter.ActualValue, ProblemDataParameter.ActualValue, GenerateRowsToEvaluate());
65      QualityParameter.ActualValue = new DoubleValue(quality);
66      return base.Apply();
67    }
68
69    public static double Calculate(IClassificationModel model, IClassificationProblemData problemData, IEnumerable<int> rows) {
70      var estimations = model.GetEstimatedClassValues(problemData.Dataset, rows).GetEnumerator();
71      if (!estimations.MoveNext()) return double.NaN;
72
73      var penalty = 0.0;
74      var count = 0;
75      foreach (var r in rows) {
76        var actualClass = problemData.Dataset.GetDoubleValue(problemData.TargetVariable, r);
77        penalty += problemData.GetClassificationPenalty(actualClass, estimations.Current);
78        estimations.MoveNext();
79        count++;
80      }
81      return penalty / count;
82    }
83
84    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
85      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
86      EstimationLimitsParameter.ExecutionContext = context;
87      ModelCreatorParameter.ExecutionContext = context;
88
89      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel(tree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
90      model.RecalculateModelParameters(problemData, rows);
91      double penalty = Calculate(model, problemData, rows);
92
93      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
94      EstimationLimitsParameter.ExecutionContext = null;
95      ModelCreatorParameter.ExecutionContext = null;
96
97      return penalty;
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.