Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationModelComparison/HeuristicLab.Problems.DataAnalysis.Views/3.4/MenuItems/CreateEnsembleMenuItem.cs @ 13082

Last change on this file since 13082 was 13082, checked in by gkronber, 8 years ago

#1998: merged changesets r10553:13081 (only on HeuristicLab.Problems.DataAnalysis.Views) from trunk to branch

File size: 6.0 KB
RevLine 
[6520]1#region License Information
2/* HeuristicLab
[13082]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6520]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;
[6613]23using System.Collections.Generic;
[6520]24using System.Linq;
[10553]25using HeuristicLab.Common;
[6520]26using HeuristicLab.MainForm;
[10553]27using HeuristicLab.MainForm.WindowsForms;
[6520]28using HeuristicLab.Optimization;
[10553]29using HeuristicLab.Optimization.Views;
[6520]30using HeuristicLab.Optimizer;
31
[10553]32namespace HeuristicLab.Problems.DataAnalysis.Views {
[6520]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 {
[10553]38      get { return new string[] { "&Edit", "&Data Analysis" }; }
[6520]39    }
40    public override int Position {
[10553]41      get { return 5100; }
[6520]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;
[10553]52      ToolStripItem.Enabled = GetDataAnalysisResults(activeView).Any();
[6520]53    }
[10553]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    }
[6520]59
60    public override void Execute() {
61      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
[10553]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);
[6520]89        }
90      }
91    }
92
[10553]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
[13082]115                       where r.Visible
[6520]116                       select r.Results;
117      return from r in allResults
118             from result in r
[13082]119             let solution = result.Value as IDataAnalysisSolution
120             where solution != null
121             let s = (IDataAnalysisSolution)cloner.Clone(result.Value)
[10553]122             select new KeyValuePair<string, IDataAnalysisSolution>(result.Key, s);
[6520]123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.