#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis { /// /// Represents a classification solution that uses a discriminant function and classification thresholds. /// [StorableClass] [Item("DiscriminantFunctionClassificationSolution", "Represents a classification solution that uses a discriminant function and classification thresholds.")] public class DiscriminantFunctionClassificationSolution : DiscriminantFunctionClassificationSolutionBase { protected readonly Dictionary valueEvaluationCache; protected readonly Dictionary classValueEvaluationCache; [StorableConstructor] protected DiscriminantFunctionClassificationSolution(bool deserializing) : base(deserializing) { valueEvaluationCache = new Dictionary(); classValueEvaluationCache = new Dictionary(); } protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner) : base(original, cloner) { valueEvaluationCache = new Dictionary(original.valueEvaluationCache); classValueEvaluationCache = new Dictionary(original.classValueEvaluationCache); } public DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) : base(model, problemData) { valueEvaluationCache = new Dictionary(); classValueEvaluationCache = new Dictionary(); CalculateRegressionResults(); CalculateClassificationResults(); } public override IDeepCloneable Clone(Cloner cloner) { return new DiscriminantFunctionClassificationSolution(this, cloner); } public override IEnumerable EstimatedClassValues { get { return GetEstimatedClassValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); } } public override IEnumerable EstimatedTrainingClassValues { get { return GetEstimatedClassValues(ProblemData.TrainingIndices); } } public override IEnumerable EstimatedTestClassValues { get { return GetEstimatedClassValues(ProblemData.TestIndices); } } public override IEnumerable GetEstimatedClassValues(IEnumerable rows) { var rowsToEvaluate = rows.Except(classValueEvaluationCache.Keys); var rowsEnumerator = rowsToEvaluate.GetEnumerator(); var valuesEnumerator = Model.GetEstimatedClassValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator(); while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) { classValueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current); } return rows.Select(row => classValueEvaluationCache[row]); } public override IEnumerable EstimatedValues { get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); } } public override IEnumerable EstimatedTrainingValues { get { return GetEstimatedValues(ProblemData.TrainingIndices); } } public override IEnumerable EstimatedTestValues { get { return GetEstimatedValues(ProblemData.TestIndices); } } public override IEnumerable GetEstimatedValues(IEnumerable rows) { var rowsToEvaluate = rows.Except(valueEvaluationCache.Keys); var rowsEnumerator = rowsToEvaluate.GetEnumerator(); var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator(); while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) { valueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current); } return rows.Select(row => valueEvaluationCache[row]); } protected override void OnModelChanged() { valueEvaluationCache.Clear(); classValueEvaluationCache.Clear(); base.OnModelChanged(); } protected override void OnModelThresholdsChanged(System.EventArgs e) { classValueEvaluationCache.Clear(); base.OnModelThresholdsChanged(e); } protected override void OnProblemDataChanged() { valueEvaluationCache.Clear(); classValueEvaluationCache.Clear(); base.OnProblemDataChanged(); } } }