[6520] | 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;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.MainForm;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Optimizer;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis.MenuItems {
|
---|
| 32 | internal class CreateEnsembleMenuItem : HeuristicLab.MainForm.WindowsForms.MenuItem, IOptimizerUserInterfaceItemProvider {
|
---|
| 33 | public override string Name {
|
---|
| 34 | get { return "Create &Solution Ensembles"; }
|
---|
| 35 | }
|
---|
| 36 | public override IEnumerable<string> Structure {
|
---|
| 37 | get { return new string[] { "&Edit" }; }
|
---|
| 38 | }
|
---|
| 39 | public override int Position {
|
---|
| 40 | get { return 2500; }
|
---|
| 41 | }
|
---|
| 42 | public override string ToolTipText {
|
---|
| 43 | get { return "Create ensembles of data analysis solutions from the solutions in the current optimizer."; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | protected override void OnToolStripItemSet(EventArgs e) {
|
---|
| 47 | ToolStripItem.Enabled = false;
|
---|
| 48 | }
|
---|
| 49 | protected override void OnActiveViewChanged(object sender, EventArgs e) {
|
---|
| 50 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
| 51 | if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked) {
|
---|
| 52 | var optimizer = activeView.Content as IOptimizer;
|
---|
| 53 | ToolStripItem.Enabled = GetDataAnalysisResults(optimizer).Any();
|
---|
| 54 | } else {
|
---|
| 55 | ToolStripItem.Enabled = false;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public override void Execute() {
|
---|
| 60 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
| 61 | if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked) {
|
---|
| 62 | var optimizer = activeView.Content as IOptimizer;
|
---|
| 63 | var solutionGroups = from pair in GetDataAnalysisResults(optimizer)
|
---|
[6582] | 64 | group pair.Value by pair.Key into g
|
---|
[6520] | 65 | select g;
|
---|
| 66 | foreach (var group in solutionGroups) {
|
---|
| 67 | // check if all solutions in the group are either only regression or only classification solutions
|
---|
| 68 | if (group.All(s => s is IRegressionSolution)) {
|
---|
| 69 | // show all regression ensembles
|
---|
| 70 | // clone problemdata (N.B. this assumes all solutions are based on the same problem data!)
|
---|
| 71 | var problemData = (RegressionProblemData)group
|
---|
| 72 | .OfType<IRegressionSolution>()
|
---|
| 73 | .First()
|
---|
| 74 | .ProblemData.Clone();
|
---|
| 75 | var ensemble = new RegressionEnsembleSolution(Enumerable.Empty<IRegressionModel>(), problemData);
|
---|
| 76 | ensemble.Name = group.Key + " ensemble";
|
---|
[6574] | 77 | ensemble.AddModelsAndPartitions(group.OfType<IRegressionSolution>());
|
---|
[6520] | 78 | MainFormManager.MainForm.ShowContent(ensemble);
|
---|
| 79 | } else if (group.All(s => s is IClassificationSolution)) {
|
---|
| 80 | // show all classification ensembles
|
---|
| 81 | var problemData = (ClassificationProblemData)group
|
---|
| 82 | .OfType<IClassificationSolution>()
|
---|
| 83 | .First()
|
---|
| 84 | .ProblemData.Clone();
|
---|
| 85 | var ensemble = new ClassificationEnsembleSolution(Enumerable.Empty<IClassificationModel>(), problemData);
|
---|
| 86 | ensemble.Name = group.Key + " ensemble";
|
---|
[6574] | 87 | ensemble.AddModelsAndPartitions(group.OfType<IClassificationSolution>());
|
---|
[6520] | 88 | MainFormManager.MainForm.ShowContent(ensemble);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | private IEnumerable<KeyValuePair<string, IItem>> GetDataAnalysisResults(IOptimizer optimizer) {
|
---|
| 95 | var allResults = from r in optimizer.Runs
|
---|
| 96 | select r.Results;
|
---|
| 97 | return from r in allResults
|
---|
| 98 | from result in r
|
---|
| 99 | let s = result.Value as IDataAnalysisSolution
|
---|
| 100 | where s != null
|
---|
| 101 | select result;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|