source: branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Views/3.3/ExpertSystemView.cs @ 13551

Last change on this file since 13551 was 13551, checked in by abeham, 5 years ago

#2457: Added characteristic calculator for qap and adapted expert system view

File size: 11.7 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;
24using System.ComponentModel;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31using HeuristicLab.Optimization;
32using HeuristicLab.Optimization.Views;
33
34namespace HeuristicLab.OptimizationExpertSystem.Views {
35  [View("Expert-system Optimizer View")]
36  [Content(typeof(ExpertSystem), IsDefaultView = true)]
37  public partial class ExpertSystemView : NamedItemView {
38    protected TypeSelectorDialog problemTypeSelectorDialog;
39    protected virtual bool SuppressEvents { get; set; }
40    private bool okbDownloadInProgress;
41
42    public new ExpertSystem Content {
43      get { return (ExpertSystem)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public ExpertSystemView() {
48      InitializeComponent();
49      // brings progress panel to front (it is not visible by default, but obstructs other elements in designer)
50      this.Controls.SetChildIndex(this.progressPanel, 0);
51      algorithmStartButton.Text = string.Empty;
52      algorithmStartButton.Image = VSImageLibrary.Play;
53      refreshMapButton.Text = string.Empty;
54      refreshMapButton.Image = VSImageLibrary.Refresh;
55    }
56
57    protected override void Dispose(bool disposing) {
58      if (disposing) {
59        if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
60        if (components != null) components.Dispose();
61      }
62      base.Dispose(disposing);
63    }
64
65    protected override void DeregisterContentEvents() {
66      Content.PropertyChanged -= ContentOnPropertyChanged;
67      Content.SuggestedInstances.CollectionReset -= SuggestedInstancesOnChanged;
68      Content.SuggestedInstances.ItemsAdded -= SuggestedInstancesOnChanged;
69      Content.SuggestedInstances.ItemsMoved -= SuggestedInstancesOnChanged;
70      Content.SuggestedInstances.ItemsRemoved -= SuggestedInstancesOnChanged;
71      Content.SuggestedInstances.ItemsReplaced -= SuggestedInstancesOnChanged;
72      if (Content.Problem != null) Content.Problem.ProblemChanged -= ContentOnProblemChanged;
73      base.DeregisterContentEvents();
74    }
75    protected override void RegisterContentEvents() {
76      base.RegisterContentEvents();
77      Content.PropertyChanged += ContentOnPropertyChanged;
78      Content.SuggestedInstances.CollectionReset += SuggestedInstancesOnChanged;
79      Content.SuggestedInstances.ItemsAdded += SuggestedInstancesOnChanged;
80      Content.SuggestedInstances.ItemsMoved += SuggestedInstancesOnChanged;
81      Content.SuggestedInstances.ItemsRemoved += SuggestedInstancesOnChanged;
82      Content.SuggestedInstances.ItemsReplaced += SuggestedInstancesOnChanged;
83      if (Content.Problem != null) Content.Problem.ProblemChanged += ContentOnProblemChanged;
84    }
85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88      SuppressEvents = true;
89      okbDownloadInProgress = false;
90      try {
91        if (Content == null) {
92          maxEvaluationsTextBox.Text = String.Empty;
93
94          problemViewHost.Content = null;
95          algorithmViewHost.Content = null;
96          runsView.Content = null;
97          kbViewHost.Content = null;
98          problemInstancesView.Content = null;
99        } else {
100          maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString();
101          problemViewHost.Content = Content.Problem;
102          runsView.Content = Content.Runs;
103          kbViewHost.ViewType = typeof(RunCollectionRLDView);
104          kbViewHost.Content = Content.KnowledgeBase;
105          problemInstancesView.Content = Content.ProblemInstances;
106        }
107      } finally { SuppressEvents = false; }
108      UpdateSuggestedInstancesCombobox();
109    }
110
111    protected override void SetEnabledStateOfControls() {
112      base.SetEnabledStateOfControls();
113      maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked;
114      problemViewHost.Enabled = Content != null && !ReadOnly && !Locked && !okbDownloadInProgress;
115      suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked && !okbDownloadInProgress;
116      algorithmStartButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0;
117      algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked;
118      runsView.Enabled = Content != null;
119      kbViewHost.Enabled = Content != null && !okbDownloadInProgress;
120      problemInstancesView.Enabled = Content != null && !okbDownloadInProgress;
121      refreshMapButton.Enabled = Content != null;
122      okbDownloadButton.Enabled = Content != null && Content.Problem != null && Content.Problem.ProblemId >= 0 && !ReadOnly && !Locked && !okbDownloadInProgress;
123    }
124
125    private void UpdateSuggestedInstancesCombobox() {
126      var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
127      var prevNewIndex = -1;
128      suggestedInstancesComboBox.Items.Clear();
129      if (Content == null) return;
130
131      for (var i = 0; i < Content.SuggestedInstances.Count; i++) {
132        suggestedInstancesComboBox.Items.Add(Content.SuggestedInstances[i]);
133        if (prevSelection == null || Content.SuggestedInstances[i].Name == prevSelection.Name)
134          prevNewIndex = prevSelection == null ? 0 : i;
135      }
136      if (prevNewIndex >= 0) {
137        suggestedInstancesComboBox.SelectedIndex = prevNewIndex;
138      }
139    }
140
141    #region Event Handlers
142    #region Content events
143    private void ContentOnProblemChanged(object sender, EventArgs eventArgs) {
144      UpdateSuggestedInstancesCombobox();
145      SetEnabledStateOfControls();
146    }
147
148    private void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
149      if (InvokeRequired) {
150        Invoke((Action<object, PropertyChangedEventArgs>)ContentOnPropertyChanged, sender, e);
151        return;
152      }
153      SuppressEvents = true;
154      try {
155        switch (e.PropertyName) {
156          case "KnowledgeBase": kbViewHost.Content = Content.KnowledgeBase; break;
157          case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break;
158          case "Problem":
159            problemViewHost.Content = Content.Problem;
160            Content.Problem.ProblemChanged += ContentOnProblemChanged;
161            break;
162          case "ProblemInstances": problemInstancesView.Content = Content.ProblemInstances; break;
163        }
164      } finally { SuppressEvents = false; }
165      SetEnabledStateOfControls();
166    }
167
168    private void SuggestedInstancesOnChanged(object sender, EventArgs e) {
169      UpdateSuggestedInstancesCombobox();
170    }
171    #endregion
172
173    #region Control events
174    private void maxEvaluationsTextBox_Validating(object sender, CancelEventArgs e) {
175      if (SuppressEvents) return;
176      if (InvokeRequired) {
177        Invoke((Action<object, CancelEventArgs>)maxEvaluationsTextBox_Validating, sender, e);
178        return;
179      }
180      int value;
181      if (!int.TryParse(maxEvaluationsTextBox.Text, out value)) {
182        e.Cancel = !maxEvaluationsTextBox.ReadOnly && maxEvaluationsTextBox.Enabled;
183        errorProvider.SetError(maxEvaluationsTextBox, "Please enter a valid integer number.");
184      } else {
185        Content.MaximumEvaluations = value;
186        e.Cancel = false;
187        errorProvider.SetError(maxEvaluationsTextBox, null);
188      }
189    }
190    #endregion
191
192    private bool validDragOperation;
193    private void instancesDropPanel_DragEnter(object sender, DragEventArgs e) {
194      validDragOperation = false;
195      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun)) {
196        validDragOperation = true;
197      } else if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
198        validDragOperation = true;
199        var items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
200        foreach (object item in items)
201          validDragOperation = validDragOperation && (item is IRun);
202      }
203    }
204    private void instancesDropPanel_DragOver(object sender, DragEventArgs e) {
205      e.Effect = DragDropEffects.None;
206      if (!validDragOperation) return;
207      if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
208      else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
209      else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
210      else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
211      else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
212    }
213    private void instancesDropPanel_DragDrop(object sender, DragEventArgs e) {
214      if (e.Effect == DragDropEffects.None) return;
215      if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun) {
216        var item = (IRun)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
217        Content.ProblemInstances.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (IRun)item.Clone() : item);
218      } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
219        var items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<IRun>();
220        if (e.Effect.HasFlag(DragDropEffects.Copy)) {
221          var cloner = new Cloner();
222          items = items.Select(x => cloner.Clone(x));
223        }
224        Content.ProblemInstances.AddRange(items);
225      }
226    }
227    #endregion
228
229    private void refreshMapButton_Click(object sender, EventArgs e) {
230      Content.UpdateInstanceMap();
231    }
232
233    private void okbDownloadButton_Click(object sender, EventArgs e) {
234      if (Content.Problem.ProblemId < 0) {
235        MessageBox.Show("Please select a problem instance first.");
236        return;
237      }
238      var progress = new Progress();
239      progress.ProgressStateChanged += OkbDownloadProgressOnStateChanged;
240      Content.UpdateKnowledgeBaseAsync(progress);
241      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(progressPanel, progress);
242      progressPanel.Visible = true;
243      SetEnabledStateOfControls();
244    }
245
246    private void OkbDownloadProgressOnStateChanged(object sender, EventArgs eventArgs) {
247      var progress = (IProgress)sender;
248      okbDownloadInProgress = progress.ProgressState == ProgressState.Started;
249      SetEnabledStateOfControls();
250      if (!okbDownloadInProgress) {
251        progressPanel.Visible = false;
252        progress.ProgressStateChanged -= OkbDownloadProgressOnStateChanged;
253      }
254    }
255
256    private void algorithmStartButton_Click(object sender, EventArgs e) {
257      var selectedInstance = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
258      var clone = (IAlgorithm)selectedInstance.Clone();
259      clone.Prepare(true);
260      clone.Start();
261      algorithmViewHost.Content = clone.Results;
262    }
263  }
264}
Note: See TracBrowser for help on using the repository browser.