Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14449 was 13794, checked in by abeham, 9 years ago

#2457: worked on recommendation algorithms (x-validation)

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