Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6613 was 6613, checked in by mkommend, 13 years ago

#1592: Implemented view for classification solutions contained in a ClassificationEnsembleSolution.

File size: 4.6 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.MainForm;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimizer;
30
31namespace 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)
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(Enumerable.Empty<IRegressionModel>(), problemData);
76            ensemble.Name = group.Key + " ensemble";
77            ensemble.AddRegressionSolutions(group.OfType<IRegressionSolution>());
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";
87            ensemble.AddClassificationSolutions(group.OfType<IClassificationSolution>());
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}
Note: See TracBrowser for help on using the repository browser.