1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Optimization.Views;
|
---|
31 | using HeuristicLab.Optimizer;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
34 | internal class CreateEnsembleMenuItem : HeuristicLab.MainForm.WindowsForms.MenuItem, IOptimizerUserInterfaceItemProvider {
|
---|
35 | public override string Name {
|
---|
36 | get { return "Create &Solution Ensembles"; }
|
---|
37 | }
|
---|
38 | public override IEnumerable<string> Structure {
|
---|
39 | get { return new string[] { "&Data Analysis" }; }
|
---|
40 | }
|
---|
41 | public override int Position {
|
---|
42 | get { return 5100; }
|
---|
43 | }
|
---|
44 | public override string ToolTipText {
|
---|
45 | get { return "Create ensembles of data analysis solutions from the solutions in the current optimizer."; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected override void OnToolStripItemSet(EventArgs e) {
|
---|
49 | ToolStripItem.Enabled = false;
|
---|
50 | }
|
---|
51 | protected override void OnActiveViewChanged(object sender, EventArgs e) {
|
---|
52 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
53 | ToolStripItem.Enabled = GetDataAnalysisResults(activeView).Any();
|
---|
54 | }
|
---|
55 | protected override void OnViewChanged(object sender, EventArgs e) {
|
---|
56 | base.OnViewChanged(sender, e);
|
---|
57 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
58 | ToolStripItem.Enabled = GetDataAnalysisResults(activeView).Any();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override void Execute() {
|
---|
62 | IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
|
---|
63 | var solutionGroups = from pair in GetDataAnalysisResults(activeView)
|
---|
64 | group pair.Value by pair.Key into g
|
---|
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(problemData);
|
---|
76 | ensemble.Name = group.Key + " ensemble";
|
---|
77 | var nestedSolutions = group.OfType<RegressionEnsembleSolution>().SelectMany(e => e.RegressionSolutions);
|
---|
78 | var solutions = group.Where(s => !(s is RegressionEnsembleSolution)).OfType<IRegressionSolution>();
|
---|
79 | ensemble.AddRegressionSolutions(nestedSolutions.Concat(solutions));
|
---|
80 | MainFormManager.MainForm.ShowContent(ensemble);
|
---|
81 | } else if (group.All(s => s is IClassificationSolution)) {
|
---|
82 | // show all classification ensembles
|
---|
83 | var problemData = (ClassificationProblemData)group
|
---|
84 | .OfType<IClassificationSolution>()
|
---|
85 | .First()
|
---|
86 | .ProblemData.Clone();
|
---|
87 | var ensemble = new ClassificationEnsembleSolution(Enumerable.Empty<IClassificationModel>(), problemData);
|
---|
88 | ensemble.Name = group.Key + " ensemble";
|
---|
89 | var nestedSolutions = group.OfType<ClassificationEnsembleSolution>().SelectMany(e => e.ClassificationSolutions);
|
---|
90 | var solutions = group.Where(s => !(s is ClassificationEnsembleSolution)).OfType<IClassificationSolution>();
|
---|
91 | ensemble.AddClassificationSolutions(nestedSolutions.Concat(solutions));
|
---|
92 | MainFormManager.MainForm.ShowContent(ensemble);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | private IEnumerable<KeyValuePair<string, IItem>> GetDataAnalysisResults(IContentView view) {
|
---|
98 | var empty = Enumerable.Empty<KeyValuePair<string, IItem>>();
|
---|
99 | if (view == null) return empty;
|
---|
100 | if (view.Content == null) return empty;
|
---|
101 | if (view.Locked) return empty;
|
---|
102 |
|
---|
103 | var optimizer = view.Content as IOptimizer;
|
---|
104 | if (optimizer != null) return GetDataAnalysisResults(optimizer.Runs);
|
---|
105 |
|
---|
106 | RunCollectionBubbleChartView bubbleChart;
|
---|
107 | var viewHost = view as ViewHost;
|
---|
108 | if (viewHost != null)
|
---|
109 | bubbleChart = viewHost.ActiveView as RunCollectionBubbleChartView;
|
---|
110 | else bubbleChart = view as RunCollectionBubbleChartView;
|
---|
111 | if (bubbleChart != null && bubbleChart.SelectedRuns.Any()) return GetDataAnalysisResults(bubbleChart.SelectedRuns);
|
---|
112 |
|
---|
113 | return empty;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private IEnumerable<KeyValuePair<string, IItem>> GetDataAnalysisResults(IEnumerable<IRun> runs) {
|
---|
117 | var allResults = from r in runs
|
---|
118 | select r.Results;
|
---|
119 | return from r in allResults
|
---|
120 | from result in r
|
---|
121 | let s = result.Value as IDataAnalysisSolution
|
---|
122 | where s != null
|
---|
123 | select result;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|