Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/Views/SolverView.cs @ 13791

Last change on this file since 13791 was 13791, checked in by abeham, 8 years ago

#2457: working on recommendation algorithms

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Common.Resources;
23using HeuristicLab.Core;
24using HeuristicLab.Core.Views;
25using HeuristicLab.Data.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.Optimization;
28using HeuristicLab.OptimizationExpertSystem.Common;
29using System;
30using System.Linq;
31using System.Windows.Forms;
32
33namespace HeuristicLab.OptimizationExpertSystem {
34  [View("Solver View")]
35  [Content(typeof(KnowledgeCenter), IsDefaultView = false)]
36  public partial class SolverView : KnowledgeCenterViewBase {
37    private EnumValueView<SeedingStrategyTypes> seedingStrategyView;
38    private CheckedItemListView<IScope> seedingSolutionsView;
39    protected virtual bool SuppressEvents { get; set; }
40
41    public SolverView() {
42      InitializeComponent();
43      algorithmStartButton.Text = string.Empty;
44      algorithmStartButton.Image = VSImageLibrary.Play;
45      algorithmCloneButton.Text = string.Empty;
46      algorithmCloneButton.Image = VSImageLibrary.Clone;
47      seedingStrategyView = new EnumValueView<SeedingStrategyTypes>() {
48        Dock = DockStyle.Fill
49      };
50      seedingSolutionsView = new CheckedItemListView<IScope>() {
51        Dock = DockStyle.Fill
52      };
53      seedingStrategyPanel.Controls.Add(seedingStrategyView);
54      solutionSeedingTabPage.Controls.Add(seedingSolutionsView);
55    }
56
57    protected override void OnContentChanged() {
58      base.OnContentChanged();
59      SuppressEvents = true;
60      try {
61        if (Content == null) {
62          maxEvaluationsView.Content = null;
63          solverParametersView.Content = null;
64          runsView.Content = null;
65          seededRunsView.Content = null;
66          seedingStrategyView.Content = null;
67          seedingSolutionsView.Content = null;
68        } else {
69          maxEvaluationsView.Content = Content.MaximumEvaluations;
70          runsView.Content = Content.InstanceRuns;
71          seededRunsView.Content = Content.SeededRuns;
72          seedingStrategyView.Content = Content.SeedingStrategy;
73          seedingSolutionsView.Content = Content.SolutionSeedingPool;
74        }
75      } finally { SuppressEvents = false; }
76      UpdateSuggestedInstancesCombobox();
77    }
78
79    protected override void SetEnabledStateOfControls() {
80      base.SetEnabledStateOfControls();
81      suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked;
82      algorithmStartButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0;
83      algorithmCloneButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0;
84      runsView.Enabled = Content != null;
85    }
86
87    #region Update Controls
88    private void UpdateSuggestedInstancesCombobox() {
89      var prevSelection = (AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem;
90      var prevNewIndex = -1;
91      suggestedInstancesComboBox.Items.Clear();
92      if (Content == null) return;
93
94      var ranking = Content.GetAlgorithmInstanceRanking().ToList();
95
96      for (var i = 0; i < ranking.Count; i++) {
97        suggestedInstancesComboBox.Items.Add(new AlgorithmInstanceItem(ranking[i]));
98        if (prevSelection == null || ranking[i].Item1.Name == prevSelection.Item.Item1.Name)
99          prevNewIndex = prevSelection == null ? 0 : i;
100      }
101      suggestedInstancesComboBox.SelectedIndex = prevNewIndex;
102    }
103    #endregion
104
105    #region Content Event Handlers
106    protected override void OnAlgorithmInstanceStarted(IAlgorithm algorithm) {
107      base.OnAlgorithmInstanceStarted(algorithm);
108      var form = new AlgorithmControlForm(algorithm, showOnlyFinalResultCheckBox.Checked);
109      form.Show(resultsDockPanel);
110    }
111
112    protected override void OnRecommendationModelChanged() {
113      base.OnRecommendationModelChanged();
114      UpdateSuggestedInstancesCombobox();
115    }
116    #endregion
117
118    #region Control Event Handlers
119    private void AlgorithmStartButtonOnClick(object sender, EventArgs e) {
120      if (suggestedInstancesComboBox.SelectedIndex >= 0)
121        Content.StartAlgorithmAsync(Content.AlgorithmInstances.Select((a, i) => new { Alg = a, Index = i}).Single(x => x.Alg.Name == ((AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem).Item.Item1.Name).Index);
122    }
123
124    private void AlgorithmCloneButtonOnClick(object sender, EventArgs e) {
125      if (suggestedInstancesComboBox.SelectedIndex >= 0)
126        MainForm.ShowContent((IAlgorithm)Content.AlgorithmInstances[Content.AlgorithmInstances.Select((a, i) => new { Alg = a, Index = i }).Single(x => x.Alg.Name == ((AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem).Item.Item1.Name).Index].Clone());
127    }
128
129    private void SuggestedInstancesComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
130      if (InvokeRequired) { Invoke((Action<object, EventArgs>)SuggestedInstancesComboBoxOnSelectedIndexChanged, sender, e); return; }
131      if (suggestedInstancesComboBox.SelectedIndex >= 0) {
132        var alg = Content.AlgorithmInstances[Content.AlgorithmInstances.Select((a, i) => new { Alg = a, Index = i }).Single(x => x.Alg.Name == ((AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem).Item.Item1.Name).Index];
133        solverParametersView.Content = alg.Parameters;
134        var engineAlg = alg as EngineAlgorithm;
135        if (engineAlg != null) operatorGraphViewHost.Content = engineAlg.OperatorGraph;
136        else operatorGraphViewHost.Content = new Data.StringValue("Algorithm is not modeled as an operator graph.");
137      } else {
138        solverParametersView.Content = null;
139        operatorGraphViewHost.Content = null;
140      }
141      SetEnabledStateOfControls();
142    }
143    #endregion
144
145    #region Helper Classes and Methods
146    private class AlgorithmInstanceItem {
147      private readonly Tuple<IAlgorithm, double> item;
148      public Tuple<IAlgorithm, double> Item { get { return item; } }
149
150      public AlgorithmInstanceItem(Tuple<IAlgorithm, double> item) {
151        this.item = item;
152      }
153
154      public override string ToString() {
155        return item.Item2.ToString("N0") + ": " + item.Item1.Name;
156      }
157    }
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.