Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationModel.cs @ 8883

Last change on this file since 8883 was 8883, checked in by abeham, 11 years ago

#1940:

  • Added backwards compatibility regions
  • Added lost AfterDeserialization hook in SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer
File size: 6.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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
31  /// <summary>
32  /// Represents a symbolic classification model
33  /// </summary>
34  [StorableClass]
35  [Item(Name = "SymbolicDiscriminantFunctionClassificationModel", Description = "Represents a symbolic classification model unsing a discriminant function.")]
36  public class SymbolicDiscriminantFunctionClassificationModel : SymbolicClassificationModel, ISymbolicDiscriminantFunctionClassificationModel {
37
38    [Storable]
39    private double[] thresholds;
40    public IEnumerable<double> Thresholds {
41      get { return (IEnumerable<double>)thresholds.Clone(); }
42      private set { thresholds = value.ToArray(); }
43    }
44    [Storable]
45    private double[] classValues;
46    public IEnumerable<double> ClassValues {
47      get { return (IEnumerable<double>)classValues.Clone(); }
48      private set { classValues = value.ToArray(); }
49    }
50
51    private IDiscriminantFunctionThresholdCalculator thresholdCalculator;
52    [Storable]
53    public IDiscriminantFunctionThresholdCalculator ThresholdCalculator {
54      get { return thresholdCalculator; }
55      private set { thresholdCalculator = value; }
56    }
57
58
59    [StorableConstructor]
60    protected SymbolicDiscriminantFunctionClassificationModel(bool deserializing) : base(deserializing) { }
61    protected SymbolicDiscriminantFunctionClassificationModel(SymbolicDiscriminantFunctionClassificationModel original, Cloner cloner)
62      : base(original, cloner) {
63      classValues = (double[])original.classValues.Clone();
64      thresholds = (double[])original.thresholds.Clone();
65      thresholdCalculator = cloner.Clone(original.thresholdCalculator);
66    }
67    public SymbolicDiscriminantFunctionClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDiscriminantFunctionThresholdCalculator thresholdCalculator,
68      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
69      : base(tree, interpreter, lowerEstimationLimit, upperEstimationLimit) {
70      this.thresholds = new double[0];
71      this.classValues = new double[0];
72      this.ThresholdCalculator = thresholdCalculator;
73    }
74
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77      // BackwardsCompatibility3.4
78      #region Backwards compatible code, remove with 3.5
79      if (ThresholdCalculator == null) ThresholdCalculator = new AccuracyMaximizationThresholdCalculator();
80      #endregion
81    }
82
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new SymbolicDiscriminantFunctionClassificationModel(this, cloner);
85    }
86
87    public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) {
88      var classValuesArr = classValues.ToArray();
89      var thresholdsArr = thresholds.ToArray();
90      if (thresholdsArr.Length != classValuesArr.Length) throw new ArgumentException();
91
92      this.classValues = classValuesArr;
93      this.thresholds = thresholdsArr;
94      OnThresholdsChanged(EventArgs.Empty);
95    }
96
97    public override void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows) {
98      double[] classValues;
99      double[] thresholds;
100      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
101      var estimatedTrainingValues = GetEstimatedValues(problemData.Dataset, rows);
102      thresholdCalculator.Calculate(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
103      SetThresholdsAndClassValues(thresholds, classValues);
104    }
105
106    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
107      return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows).LimitToRange(LowerEstimationLimit, UpperEstimationLimit);
108    }
109
110    public override IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
111      if (!Thresholds.Any() && !ClassValues.Any()) throw new ArgumentException("No thresholds and class values were set for the current symbolic classification model.");
112      foreach (var x in GetEstimatedValues(dataset, rows)) {
113        int classIndex = 0;
114        // find first threshold value which is larger than x => class index = threshold index + 1
115        for (int i = 0; i < thresholds.Length; i++) {
116          if (x > thresholds[i]) classIndex++;
117          else break;
118        }
119        yield return classValues.ElementAt(classIndex - 1);
120      }
121    }
122
123
124    public override ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
125      return CreateDiscriminantClassificationSolution(problemData);
126    }
127    public SymbolicDiscriminantFunctionClassificationSolution CreateDiscriminantClassificationSolution(IClassificationProblemData problemData) {
128      return new SymbolicDiscriminantFunctionClassificationSolution(this, new ClassificationProblemData(problemData));
129    }
130    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
131      return CreateDiscriminantClassificationSolution(problemData);
132    }
133    IDiscriminantFunctionClassificationSolution IDiscriminantFunctionClassificationModel.CreateDiscriminantFunctionClassificationSolution(IClassificationProblemData problemData) {
134      return CreateDiscriminantClassificationSolution(problemData);
135    }
136
137    #region events
138    public event EventHandler ThresholdsChanged;
139    protected virtual void OnThresholdsChanged(EventArgs e) {
140      var listener = ThresholdsChanged;
141      if (listener != null) listener(this, e);
142    }
143    #endregion
144  }
145}
Note: See TracBrowser for help on using the repository browser.