Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/MenuItems/CreateEnsembleMenuItem.cs @ 17495

Last change on this file since 17495 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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