Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Problems.DataAnalysis.Views/3.4/MenuItems/CreateEnsembleMenuItem.cs @ 7284

Last change on this file since 7284 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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            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
98    private IEnumerable<KeyValuePair<string, IItem>> GetDataAnalysisResults(IOptimizer optimizer) {
99      var allResults = from r in optimizer.Runs
100                       select r.Results;
101      return from r in allResults
102             from result in r
103             let s = result.Value as IDataAnalysisSolution
104             where s != null
105             select result;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.