Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolution.cs @ 11359

Last change on this file since 11359 was 8660, checked in by gkronber, 12 years ago

#1847 merged r8205:8635 from trunk into branch

File size: 5.1 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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.DataAnalysis {
29  /// <summary>
30  /// Represents a classification solution that uses a discriminant function and classification thresholds.
31  /// </summary>
32  [StorableClass]
33  [Item("DiscriminantFunctionClassificationSolution", "Represents a classification solution that uses a discriminant function and classification thresholds.")]
34  public abstract class DiscriminantFunctionClassificationSolution : DiscriminantFunctionClassificationSolutionBase {
35    protected readonly Dictionary<int, double> valueEvaluationCache;
36    protected readonly Dictionary<int, double> classValueEvaluationCache;
37
38    [StorableConstructor]
39    protected DiscriminantFunctionClassificationSolution(bool deserializing)
40      : base(deserializing) {
41      valueEvaluationCache = new Dictionary<int, double>();
42      classValueEvaluationCache = new Dictionary<int, double>();
43    }
44    protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner)
45      : base(original, cloner) {
46      valueEvaluationCache = new Dictionary<int, double>(original.valueEvaluationCache);
47      classValueEvaluationCache = new Dictionary<int, double>(original.classValueEvaluationCache);
48    }
49    protected DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData)
50      : base(model, problemData) {
51      valueEvaluationCache = new Dictionary<int, double>();
52      classValueEvaluationCache = new Dictionary<int, double>();
53    }
54
55    public override IEnumerable<double> EstimatedClassValues {
56      get { return GetEstimatedClassValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
57    }
58    public override IEnumerable<double> EstimatedTrainingClassValues {
59      get { return GetEstimatedClassValues(ProblemData.TrainingIndices); }
60    }
61    public override IEnumerable<double> EstimatedTestClassValues {
62      get { return GetEstimatedClassValues(ProblemData.TestIndices); }
63    }
64
65    public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
66      var rowsToEvaluate = rows.Except(classValueEvaluationCache.Keys);
67      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
68      var valuesEnumerator = Model.GetEstimatedClassValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
69
70      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
71        classValueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
72      }
73
74      return rows.Select(row => classValueEvaluationCache[row]);
75    }
76
77
78    public override IEnumerable<double> EstimatedValues {
79      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
80    }
81    public override IEnumerable<double> EstimatedTrainingValues {
82      get { return GetEstimatedValues(ProblemData.TrainingIndices); }
83    }
84    public override IEnumerable<double> EstimatedTestValues {
85      get { return GetEstimatedValues(ProblemData.TestIndices); }
86    }
87
88    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
89      var rowsToEvaluate = rows.Except(valueEvaluationCache.Keys);
90      var rowsEnumerator = rowsToEvaluate.GetEnumerator();
91      var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
92
93      while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
94        valueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
95      }
96
97      return rows.Select(row => valueEvaluationCache[row]);
98    }
99
100    protected override void OnModelChanged() {
101      valueEvaluationCache.Clear();
102      classValueEvaluationCache.Clear();
103      base.OnModelChanged();
104    }
105    protected override void OnModelThresholdsChanged(System.EventArgs e) {
106      classValueEvaluationCache.Clear();
107      base.OnModelThresholdsChanged(e);
108    }
109    protected override void OnProblemDataChanged() {
110      valueEvaluationCache.Clear();
111      classValueEvaluationCache.Clear();
112      base.OnProblemDataChanged();
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.