[5649] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
| 28 | namespace 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.")]
|
---|
[6760] | 34 | public abstract class DiscriminantFunctionClassificationSolution : DiscriminantFunctionClassificationSolutionBase {
|
---|
| 35 | protected readonly Dictionary<int, double> valueEvaluationCache;
|
---|
| 36 | protected readonly Dictionary<int, double> classValueEvaluationCache;
|
---|
[5885] | 37 |
|
---|
[6760] | 38 | [StorableConstructor]
|
---|
| 39 | protected DiscriminantFunctionClassificationSolution(bool deserializing)
|
---|
| 40 | : base(deserializing) {
|
---|
| 41 | valueEvaluationCache = new Dictionary<int, double>();
|
---|
| 42 | classValueEvaluationCache = new Dictionary<int, double>();
|
---|
[5717] | 43 | }
|
---|
[5649] | 44 | protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner)
|
---|
| 45 | : base(original, cloner) {
|
---|
[6760] | 46 | valueEvaluationCache = new Dictionary<int, double>(original.valueEvaluationCache);
|
---|
| 47 | classValueEvaluationCache = new Dictionary<int, double>(original.classValueEvaluationCache);
|
---|
[5649] | 48 | }
|
---|
[6760] | 49 | protected DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData)
|
---|
[5649] | 50 | : base(model, problemData) {
|
---|
[6760] | 51 | valueEvaluationCache = new Dictionary<int, double>();
|
---|
| 52 | classValueEvaluationCache = new Dictionary<int, double>();
|
---|
| 53 |
|
---|
[5736] | 54 | SetAccuracyMaximizingThresholds();
|
---|
[5649] | 55 | }
|
---|
| 56 |
|
---|
[6760] | 57 | public override IEnumerable<double> EstimatedClassValues {
|
---|
| 58 | get { return GetEstimatedClassValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
[5736] | 59 | }
|
---|
[6760] | 60 | public override IEnumerable<double> EstimatedTrainingClassValues {
|
---|
| 61 | get { return GetEstimatedClassValues(ProblemData.TrainingIndizes); }
|
---|
| 62 | }
|
---|
| 63 | public override IEnumerable<double> EstimatedTestClassValues {
|
---|
| 64 | get { return GetEstimatedClassValues(ProblemData.TestIndizes); }
|
---|
| 65 | }
|
---|
[5736] | 66 |
|
---|
[6760] | 67 | public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
|
---|
| 68 | var rowsToEvaluate = rows.Except(classValueEvaluationCache.Keys);
|
---|
| 69 | var rowsEnumerator = rowsToEvaluate.GetEnumerator();
|
---|
| 70 | var valuesEnumerator = Model.GetEstimatedClassValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
|
---|
[5885] | 71 |
|
---|
[6760] | 72 | while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
|
---|
| 73 | classValueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
|
---|
| 74 | }
|
---|
[5885] | 75 |
|
---|
[6760] | 76 | return rows.Select(row => classValueEvaluationCache[row]);
|
---|
[5885] | 77 | }
|
---|
| 78 |
|
---|
[6760] | 79 |
|
---|
| 80 | public override IEnumerable<double> EstimatedValues {
|
---|
| 81 | get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
[5736] | 82 | }
|
---|
[6760] | 83 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
| 84 | get { return GetEstimatedValues(ProblemData.TrainingIndizes); }
|
---|
[5736] | 85 | }
|
---|
[6760] | 86 | public override IEnumerable<double> EstimatedTestValues {
|
---|
| 87 | get { return GetEstimatedValues(ProblemData.TestIndizes); }
|
---|
[5736] | 88 | }
|
---|
| 89 |
|
---|
[6760] | 90 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
| 91 | var rowsToEvaluate = rows.Except(valueEvaluationCache.Keys);
|
---|
| 92 | var rowsEnumerator = rowsToEvaluate.GetEnumerator();
|
---|
| 93 | var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
|
---|
[5736] | 94 |
|
---|
[6760] | 95 | while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
|
---|
| 96 | valueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
|
---|
| 97 | }
|
---|
[5736] | 98 |
|
---|
[6760] | 99 | return rows.Select(row => valueEvaluationCache[row]);
|
---|
[5736] | 100 | }
|
---|
| 101 |
|
---|
[6760] | 102 | protected override void OnModelChanged() {
|
---|
| 103 | valueEvaluationCache.Clear();
|
---|
| 104 | classValueEvaluationCache.Clear();
|
---|
| 105 | base.OnModelChanged();
|
---|
[5736] | 106 | }
|
---|
[6760] | 107 | protected override void OnModelThresholdsChanged(System.EventArgs e) {
|
---|
| 108 | classValueEvaluationCache.Clear();
|
---|
| 109 | base.OnModelThresholdsChanged(e);
|
---|
[5736] | 110 | }
|
---|
[6760] | 111 | protected override void OnProblemDataChanged() {
|
---|
| 112 | valueEvaluationCache.Clear();
|
---|
| 113 | classValueEvaluationCache.Clear();
|
---|
| 114 | base.OnProblemDataChanged();
|
---|
[5649] | 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|