Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.OptimizationExpertSystem/3.3/Views/SolverView.cs @ 16300

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

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

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.Collections.Generic;
31using System.Linq;
32using System.Windows.Forms;
33
34namespace HeuristicLab.OptimizationExpertSystem {
35  [View("Solver View")]
36  [Content(typeof(KnowledgeCenter), IsDefaultView = false)]
37  public partial class SolverView : KnowledgeCenterViewBase {
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) {
63          maxEvaluationsView.Content = null;
64          solverParametersView.Content = null;
65          runsView.Content = null;
66          seededRunsView.Content = null;
67          seedingStrategyView.Content = null;
68          seedingSolutionsView.Content = null;
69        } else {
70          maxEvaluationsView.Content = Content.MaximumEvaluations;
71          runsView.Content = Content.InstanceRuns;
72          seededRunsView.Content = Content.SeededRuns;
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
88    #region Update Controls
89    private void UpdateSuggestedInstancesCombobox() {
90      var prevSelection = (AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem;
91      var prevNewIndex = -1;
92      suggestedInstancesComboBox.Items.Clear();
93      if (Content == null) return;
94
95      var ranking = Content.GetAlgorithmInstanceRanking().ToList();
96
97      for (var i = 0; i < ranking.Count; i++) {
98        suggestedInstancesComboBox.Items.Add(new AlgorithmInstanceItem(ranking[i]));
99        if (prevSelection == null || ranking[i].Key.Name == prevSelection.Item.Key.Name)
100          prevNewIndex = prevSelection == null ? 0 : i;
101      }
102      suggestedInstancesComboBox.SelectedIndex = prevNewIndex;
103    }
104    #endregion
105
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    }
112
113    protected override void OnRecommendationModelChanged() {
114      base.OnRecommendationModelChanged();
115      UpdateSuggestedInstancesCombobox();
116    }
117    #endregion
118
119    #region Control Event Handlers
120    private void AlgorithmStartButtonOnClick(object sender, EventArgs e) {
121      if (suggestedInstancesComboBox.SelectedIndex >= 0)
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);
123    }
124
125    private void AlgorithmCloneButtonOnClick(object sender, EventArgs e) {
126      if (suggestedInstancesComboBox.SelectedIndex >= 0)
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());
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) {
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];
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    }
144    #endregion
145
146    #region Helper Classes and Methods
147    private class AlgorithmInstanceItem {
148      private readonly KeyValuePair<IAlgorithm, double> item;
149      public KeyValuePair<IAlgorithm, double> Item { get { return item; } }
150
151      public AlgorithmInstanceItem(KeyValuePair<IAlgorithm, double> item) {
152        this.item = item;
153      }
154
155      public override string ToString() {
156        return item.Value.ToString("N0") + ": " + item.Key.Name;
157      }
158    }
159    #endregion
160  }
161}
Note: See TracBrowser for help on using the repository browser.