[5816] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5816] | 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 |
|
---|
[6589] | 22 | using System;
|
---|
[5816] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[6613] | 25 | using HeuristicLab.Collections;
|
---|
[5816] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[6589] | 28 | using HeuristicLab.Data;
|
---|
[5816] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7459] | 30 | using HeuristicLab.Problems.DataAnalysis.Interfaces.Classification;
|
---|
[5816] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// Represents classification solutions that contain an ensemble of multiple classification models
|
---|
| 35 | /// </summary>
|
---|
| 36 | [StorableClass]
|
---|
| 37 | [Item("Classification Ensemble Solution", "A classification solution that contains an ensemble of multiple classification models")]
|
---|
[6666] | 38 | [Creatable("Data Analysis - Ensembles")]
|
---|
[6592] | 39 | public sealed class ClassificationEnsembleSolution : ClassificationSolution, IClassificationEnsembleSolution {
|
---|
[6239] | 40 | public new IClassificationEnsembleModel Model {
|
---|
| 41 | get { return (IClassificationEnsembleModel)base.Model; }
|
---|
| 42 | }
|
---|
[6666] | 43 | public new ClassificationEnsembleProblemData ProblemData {
|
---|
| 44 | get { return (ClassificationEnsembleProblemData)base.ProblemData; }
|
---|
| 45 | set { base.ProblemData = value; }
|
---|
| 46 | }
|
---|
[6239] | 47 |
|
---|
[7549] | 48 | private readonly CheckedItemCollection<IClassificationSolution> classificationSolutions;
|
---|
| 49 | public ICheckedItemCollection<IClassificationSolution> ClassificationSolutions {
|
---|
[6613] | 50 | get { return classificationSolutions; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[7459] | 53 | private IClassificationEnsembleSolutionWeightCalculator weightCalculator;
|
---|
| 54 |
|
---|
[7464] | 55 | public IClassificationEnsembleSolutionWeightCalculator WeightCalculator {
|
---|
| 56 | set {
|
---|
| 57 | if (value != null) {
|
---|
| 58 | weightCalculator = value;
|
---|
| 59 | if (!ProblemData.IsEmpty)
|
---|
| 60 | RecalculateResults();
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
[7562] | 63 | get { return weightCalculator; }
|
---|
[7464] | 64 | }
|
---|
| 65 |
|
---|
[6613] | 66 | [StorableConstructor]
|
---|
| 67 | private ClassificationEnsembleSolution(bool deserializing)
|
---|
| 68 | : base(deserializing) {
|
---|
[7549] | 69 | classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
|
---|
[6613] | 70 | }
|
---|
| 71 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 72 | private void AfterDeserialization() {
|
---|
| 73 | foreach (var model in Model.Models) {
|
---|
| 74 | IClassificationProblemData problemData = (IClassificationProblemData)ProblemData.Clone();
|
---|
| 75 | classificationSolutions.Add(model.CreateClassificationSolution(problemData));
|
---|
| 76 | }
|
---|
| 77 | RegisterClassificationSolutionsEventHandler();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[6592] | 80 | private ClassificationEnsembleSolution(ClassificationEnsembleSolution original, Cloner cloner)
|
---|
[5816] | 81 | : base(original, cloner) {
|
---|
[6613] | 82 | classificationSolutions = cloner.Clone(original.classificationSolutions);
|
---|
| 83 | RegisterClassificationSolutionsEventHandler();
|
---|
[5816] | 84 | }
|
---|
[6613] | 85 |
|
---|
[6666] | 86 | public ClassificationEnsembleSolution()
|
---|
| 87 | : base(new ClassificationEnsembleModel(), ClassificationEnsembleProblemData.EmptyProblemData) {
|
---|
[7549] | 88 | classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
|
---|
[6666] | 89 |
|
---|
[7464] | 90 | weightCalculator = new MajorityVoteWeightCalculator();
|
---|
[7459] | 91 |
|
---|
[6666] | 92 | RegisterClassificationSolutionsEventHandler();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[6239] | 95 | public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData)
|
---|
[6613] | 96 | : this(models, problemData,
|
---|
| 97 | models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
|
---|
| 98 | models.Select(m => (IntRange)problemData.TestPartition.Clone())
|
---|
| 99 | ) { }
|
---|
[5816] | 100 |
|
---|
[6239] | 101 | public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
[6613] | 102 | : base(new ClassificationEnsembleModel(Enumerable.Empty<IClassificationModel>()), new ClassificationEnsembleProblemData(problemData)) {
|
---|
[7549] | 103 | this.classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
|
---|
[6613] | 104 |
|
---|
| 105 | List<IClassificationSolution> solutions = new List<IClassificationSolution>();
|
---|
| 106 | var modelEnumerator = models.GetEnumerator();
|
---|
| 107 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
| 108 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
| 109 |
|
---|
| 110 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
| 111 | var p = (IClassificationProblemData)problemData.Clone();
|
---|
| 112 | p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
|
---|
| 113 | p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
|
---|
| 114 | p.TestPartition.Start = testPartitionEnumerator.Current.Start;
|
---|
| 115 | p.TestPartition.End = testPartitionEnumerator.Current.End;
|
---|
| 116 |
|
---|
| 117 | solutions.Add(modelEnumerator.Current.CreateClassificationSolution(p));
|
---|
| 118 | }
|
---|
| 119 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
| 120 | throw new ArgumentException();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | RegisterClassificationSolutionsEventHandler();
|
---|
| 124 | classificationSolutions.AddRange(solutions);
|
---|
[6239] | 125 | }
|
---|
| 126 |
|
---|
[5816] | 127 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 128 | return new ClassificationEnsembleSolution(this, cloner);
|
---|
| 129 | }
|
---|
[6613] | 130 | private void RegisterClassificationSolutionsEventHandler() {
|
---|
| 131 | classificationSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsAdded);
|
---|
| 132 | classificationSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsRemoved);
|
---|
| 133 | classificationSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CollectionReset);
|
---|
[7549] | 134 | classificationSolutions.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CheckedItemsChanged);
|
---|
[6613] | 135 | }
|
---|
[5816] | 136 |
|
---|
[6589] | 137 | protected override void RecalculateResults() {
|
---|
[7549] | 138 | weightCalculator.CalculateNormalizedWeights(classificationSolutions.CheckedItems);
|
---|
[6589] | 139 | CalculateResults();
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[6613] | 142 | #region Evaluation
|
---|
[6239] | 143 | public override IEnumerable<double> EstimatedTrainingClassValues {
|
---|
[7549] | 144 | get {
|
---|
| 145 | return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
|
---|
| 146 | ProblemData.Dataset,
|
---|
| 147 | ProblemData.TrainingIndizes,
|
---|
| 148 | weightCalculator.GetTrainingClassDelegate());
|
---|
| 149 | }
|
---|
[6239] | 150 | }
|
---|
| 151 |
|
---|
| 152 | public override IEnumerable<double> EstimatedTestClassValues {
|
---|
[7549] | 153 | get {
|
---|
| 154 | return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
|
---|
| 155 | ProblemData.Dataset,
|
---|
| 156 | ProblemData.TestIndizes,
|
---|
| 157 | weightCalculator.GetTestClassDelegate());
|
---|
| 158 | }
|
---|
[6239] | 159 | }
|
---|
| 160 |
|
---|
| 161 | public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
|
---|
[7549] | 162 | return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
|
---|
| 163 | ProblemData.Dataset,
|
---|
| 164 | rows,
|
---|
| 165 | weightCalculator.GetAllClassDelegate());
|
---|
[6239] | 166 | }
|
---|
| 167 |
|
---|
[5816] | 168 | public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
[7549] | 169 | IEnumerable<IClassificationModel> models = classificationSolutions.CheckedItems.Select(sol => sol.Model);
|
---|
| 170 | if (!models.Any()) yield break;
|
---|
| 171 | var estimatedValuesEnumerators = (from model in models
|
---|
[5816] | 172 | select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
|
---|
| 173 | .ToList();
|
---|
| 174 |
|
---|
| 175 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
| 176 | yield return from enumerator in estimatedValuesEnumerators
|
---|
| 177 | select enumerator.Current;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
[6613] | 180 | #endregion
|
---|
[6520] | 181 |
|
---|
[6666] | 182 | protected override void OnProblemDataChanged() {
|
---|
| 183 | IClassificationProblemData problemData = new ClassificationProblemData(ProblemData.Dataset,
|
---|
| 184 | ProblemData.AllowedInputVariables,
|
---|
| 185 | ProblemData.TargetVariable);
|
---|
| 186 | problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
| 187 | problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
|
---|
| 188 | problemData.TestPartition.Start = ProblemData.TestPartition.Start;
|
---|
| 189 | problemData.TestPartition.End = ProblemData.TestPartition.End;
|
---|
| 190 |
|
---|
| 191 | foreach (var solution in ClassificationSolutions) {
|
---|
| 192 | if (solution is ClassificationEnsembleSolution)
|
---|
| 193 | solution.ProblemData = ProblemData;
|
---|
| 194 | else
|
---|
| 195 | solution.ProblemData = problemData;
|
---|
| 196 | }
|
---|
| 197 | base.OnProblemDataChanged();
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[6613] | 200 | public void AddClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
|
---|
| 201 | classificationSolutions.AddRange(solutions);
|
---|
| 202 | }
|
---|
| 203 | public void RemoveClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
|
---|
| 204 | classificationSolutions.RemoveRange(solutions);
|
---|
| 205 | }
|
---|
[6520] | 206 |
|
---|
[6613] | 207 | private void classificationSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
|
---|
| 208 | foreach (var solution in e.Items) AddClassificationSolution(solution);
|
---|
[6520] | 209 | RecalculateResults();
|
---|
| 210 | }
|
---|
[6613] | 211 | private void classificationSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
|
---|
| 212 | foreach (var solution in e.Items) RemoveClassificationSolution(solution);
|
---|
| 213 | RecalculateResults();
|
---|
| 214 | }
|
---|
| 215 | private void classificationSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
|
---|
| 216 | foreach (var solution in e.OldItems) RemoveClassificationSolution(solution);
|
---|
| 217 | foreach (var solution in e.Items) AddClassificationSolution(solution);
|
---|
| 218 | RecalculateResults();
|
---|
| 219 | }
|
---|
[7549] | 220 | private void classificationSolutions_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
|
---|
| 221 | RecalculateResults();
|
---|
| 222 | }
|
---|
[6520] | 223 |
|
---|
[6613] | 224 | private void AddClassificationSolution(IClassificationSolution solution) {
|
---|
| 225 | if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 226 | Model.Add(solution.Model);
|
---|
| 227 | }
|
---|
[6520] | 228 |
|
---|
[6613] | 229 | private void RemoveClassificationSolution(IClassificationSolution solution) {
|
---|
| 230 | if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 231 | Model.Remove(solution.Model);
|
---|
[6520] | 232 | }
|
---|
[5816] | 233 | }
|
---|
| 234 | }
|
---|