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 |
|
---|
22 | using HeuristicLab.Common.Resources;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Core.Views;
|
---|
25 | using HeuristicLab.Data.Views;
|
---|
26 | using HeuristicLab.MainForm;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.OptimizationExpertSystem.Common;
|
---|
29 | using System;
|
---|
30 | using System.Linq;
|
---|
31 | using System.Windows.Forms;
|
---|
32 |
|
---|
33 | namespace 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 | minTargetView.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 | minTargetView.Content = Content.MinimumTarget;
|
---|
72 | runsView.Content = Content.InstanceRuns;
|
---|
73 | seededRunsView.Content = Content.SeededRuns;
|
---|
74 | seedingStrategyView.Content = Content.SeedingStrategy;
|
---|
75 | seedingSolutionsView.Content = Content.SolutionSeedingPool;
|
---|
76 | }
|
---|
77 | } finally { SuppressEvents = false; }
|
---|
78 | UpdateSuggestedInstancesCombobox();
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected override void SetEnabledStateOfControls() {
|
---|
82 | base.SetEnabledStateOfControls();
|
---|
83 | suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked;
|
---|
84 | algorithmStartButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0;
|
---|
85 | algorithmCloneButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0;
|
---|
86 | runsView.Enabled = Content != null;
|
---|
87 | }
|
---|
88 |
|
---|
89 | #region Update Controls
|
---|
90 | private void UpdateSuggestedInstancesCombobox() {
|
---|
91 | var prevSelection = (AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem;
|
---|
92 | var prevNewIndex = -1;
|
---|
93 | suggestedInstancesComboBox.Items.Clear();
|
---|
94 | if (Content == null) return;
|
---|
95 |
|
---|
96 | var ranking = Content.GetAlgorithmInstanceRanking().ToList();
|
---|
97 |
|
---|
98 | for (var i = 0; i < ranking.Count; i++) {
|
---|
99 | suggestedInstancesComboBox.Items.Add(new AlgorithmInstanceItem(ranking[i]));
|
---|
100 | if (prevSelection == null || ranking[i].Item1.Name == prevSelection.Item.Item1.Name)
|
---|
101 | prevNewIndex = prevSelection == null ? 0 : i;
|
---|
102 | }
|
---|
103 | suggestedInstancesComboBox.SelectedIndex = prevNewIndex;
|
---|
104 | }
|
---|
105 | #endregion
|
---|
106 |
|
---|
107 | #region Content Event Handlers
|
---|
108 | protected override void OnAlgorithmInstanceStarted(IAlgorithm algorithm) {
|
---|
109 | base.OnAlgorithmInstanceStarted(algorithm);
|
---|
110 | var form = new AlgorithmControlForm(algorithm, showOnlyFinalResultCheckBox.Checked);
|
---|
111 | form.Show(resultsDockPanel);
|
---|
112 | }
|
---|
113 |
|
---|
114 | protected override void OnRecommendationModelChanged() {
|
---|
115 | base.OnRecommendationModelChanged();
|
---|
116 | UpdateSuggestedInstancesCombobox();
|
---|
117 | }
|
---|
118 | #endregion
|
---|
119 |
|
---|
120 | #region Control Event Handlers
|
---|
121 | private void AlgorithmStartButtonOnClick(object sender, EventArgs e) {
|
---|
122 | if (suggestedInstancesComboBox.SelectedIndex >= 0)
|
---|
123 | Content.StartAlgorithmAsync(Content.AlgorithmInstances.Select((a, i) => new { Alg = a, Index = i}).Single(x => x.Alg.Name == ((AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem).Item.Item1.Name).Index);
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void AlgorithmCloneButtonOnClick(object sender, EventArgs e) {
|
---|
127 | if (suggestedInstancesComboBox.SelectedIndex >= 0)
|
---|
128 | 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());
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void SuggestedInstancesComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
|
---|
132 | if (InvokeRequired) { Invoke((Action<object, EventArgs>)SuggestedInstancesComboBoxOnSelectedIndexChanged, sender, e); return; }
|
---|
133 | if (suggestedInstancesComboBox.SelectedIndex >= 0) {
|
---|
134 | 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];
|
---|
135 | solverParametersView.Content = alg.Parameters;
|
---|
136 | var engineAlg = alg as EngineAlgorithm;
|
---|
137 | if (engineAlg != null) operatorGraphViewHost.Content = engineAlg.OperatorGraph;
|
---|
138 | else operatorGraphViewHost.Content = new Data.StringValue("Algorithm is not modeled as an operator graph.");
|
---|
139 | } else {
|
---|
140 | solverParametersView.Content = null;
|
---|
141 | operatorGraphViewHost.Content = null;
|
---|
142 | }
|
---|
143 | SetEnabledStateOfControls();
|
---|
144 | }
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | #region Helper Classes and Methods
|
---|
148 | private class AlgorithmInstanceItem {
|
---|
149 | private readonly Tuple<IAlgorithm, double> item;
|
---|
150 | public Tuple<IAlgorithm, double> Item { get { return item; } }
|
---|
151 |
|
---|
152 | public AlgorithmInstanceItem(Tuple<IAlgorithm, double> item) {
|
---|
153 | this.item = item;
|
---|
154 | }
|
---|
155 |
|
---|
156 | public override string ToString() {
|
---|
157 | return item.Item2.ToString("F0") + ": " + item.Item1.Name;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | #endregion
|
---|
161 | }
|
---|
162 | }
|
---|