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

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

#2457: fixed bugs in suggestions

File size: 11.6 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 HeuristicLab.Common.Resources;
23using HeuristicLab.Core.Views;
24using HeuristicLab.MainForm;
25using HeuristicLab.Optimization;
26using HeuristicLab.Optimization.Views;
27using System;
28using System.ComponentModel;
29using System.Linq;
30using System.Text.RegularExpressions;
31using System.Windows.Forms;
32using System.Windows.Forms.DataVisualization.Charting;
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          algorithmViewHost.Content = Content.CurrentResult;
107        }
108      } finally { SuppressEvents = false; }
109      UpdateSuggestedInstancesCombobox();
110    }
111
112    protected override void SetEnabledStateOfControls() {
113      base.SetEnabledStateOfControls();
114      maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked;
115      problemViewHost.Enabled = Content != null && !ReadOnly && !Locked && !okbDownloadInProgress;
116      suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked && !okbDownloadInProgress;
117      algorithmStartButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0;
118      algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked;
119      runsView.Enabled = Content != null;
120      kbViewHost.Enabled = Content != null && !okbDownloadInProgress;
121      problemInstancesView.Enabled = Content != null && !okbDownloadInProgress;
122      refreshMapButton.Enabled = Content != null;
123      okbDownloadButton.Enabled = Content != null && Content.Problem != null && Content.Problem.ProblemId >= 0 && !ReadOnly && !Locked && !okbDownloadInProgress;
124    }
125
126    private void UpdateSuggestedInstancesCombobox() {
127      var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
128      var prevNewIndex = -1;
129      suggestedInstancesComboBox.Items.Clear();
130      if (Content == null) return;
131
132      for (var i = 0; i < Content.SuggestedInstances.Count; i++) {
133        suggestedInstancesComboBox.Items.Add(Content.SuggestedInstances[i]);
134        if (prevSelection == null || Content.SuggestedInstances[i].Name == prevSelection.Name)
135          prevNewIndex = prevSelection == null ? 0 : i;
136      }
137      if (prevNewIndex >= 0) {
138        suggestedInstancesComboBox.SelectedIndex = prevNewIndex;
139      }
140    }
141
142    #region Event Handlers
143    #region Content events
144    private void ContentOnProblemChanged(object sender, EventArgs eventArgs) {
145      UpdateSuggestedInstancesCombobox();
146      SetEnabledStateOfControls();
147    }
148
149    private void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
150      if (InvokeRequired) {
151        Invoke((Action<object, PropertyChangedEventArgs>)ContentOnPropertyChanged, sender, e);
152        return;
153      }
154      SuppressEvents = true;
155      try {
156        switch (e.PropertyName) {
157          case "KnowledgeBase": kbViewHost.Content = Content.KnowledgeBase; break;
158          case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break;
159          case "Problem":
160            problemViewHost.Content = Content.Problem;
161            Content.Problem.ProblemChanged += ContentOnProblemChanged;
162            break;
163          case "ProblemInstances": problemInstancesView.Content = Content.ProblemInstances; break;
164          case "CurrentResult": algorithmViewHost.Content = Content.CurrentResult; break;
165        }
166      } finally { SuppressEvents = false; }
167      SetEnabledStateOfControls();
168    }
169
170    private void SuggestedInstancesOnChanged(object sender, EventArgs e) {
171      UpdateSuggestedInstancesCombobox();
172    }
173    #endregion
174
175    #region Control events
176    private void MaxEvaluationsTextBoxOnValidating(object sender, CancelEventArgs e) {
177      if (SuppressEvents) return;
178      if (InvokeRequired) {
179        Invoke((Action<object, CancelEventArgs>)MaxEvaluationsTextBoxOnValidating, sender, e);
180        return;
181      }
182      int value;
183      if (!int.TryParse(maxEvaluationsTextBox.Text, out value)) {
184        e.Cancel = !maxEvaluationsTextBox.ReadOnly && maxEvaluationsTextBox.Enabled;
185        errorProvider.SetError(maxEvaluationsTextBox, "Please enter a valid integer number.");
186      } else {
187        Content.MaximumEvaluations = value;
188        e.Cancel = false;
189        errorProvider.SetError(maxEvaluationsTextBox, null);
190      }
191    }
192    #endregion
193    #endregion
194
195    private void RefreshMapButtonOnClick(object sender, EventArgs e) {
196      Content.UpdateInstanceProjection();
197      UpdateProjectionComboBox();
198    }
199
200    private void UpdateProjectionComboBox() {
201      projectionComboBox.Items.Clear();
202      var projStrings = Content.ProblemInstances
203        .SelectMany(x => x.Results.Where(y => Regex.IsMatch(y.Key, "^Projection[.].*[.][XY]$")))
204        .Select(x => Regex.Match(x.Key, "Projection[.](?<g>.*)[.][XY]").Groups["g"].Value)
205        .Distinct();
206      foreach (var str in projStrings) {
207        projectionComboBox.Items.Add(str);
208      }
209    }
210
211    private void OkbDownloadButtonOnClick(object sender, EventArgs e) {
212      if (Content.Problem.ProblemId == -1) {
213        MessageBox.Show("Please select a problem instance first.");
214        return;
215      }
216      var progress = new Progress();
217      progress.ProgressStateChanged += OkbDownloadProgressOnStateChanged;
218      Content.UpdateKnowledgeBaseAsync(progress);
219      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(progressPanel, progress);
220      progressPanel.Visible = true;
221      SetEnabledStateOfControls();
222    }
223
224    private void OkbDownloadProgressOnStateChanged(object sender, EventArgs eventArgs) {
225      var progress = (IProgress)sender;
226      okbDownloadInProgress = progress.ProgressState == ProgressState.Started;
227      SetEnabledStateOfControls();
228      if (!okbDownloadInProgress) {
229        progressPanel.Visible = false;
230        progress.ProgressStateChanged -= OkbDownloadProgressOnStateChanged;
231      }
232    }
233
234    private void AlgorithmStartButtonOnClick(object sender, EventArgs e) {
235      if (suggestedInstancesComboBox.SelectedIndex >= 0)
236        Content.StartAlgorithmAsync(suggestedInstancesComboBox.SelectedIndex);
237    }
238
239    private void ProjectionComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
240      if (projectionComboBox.SelectedIndex < 0) return;
241      var projection = (string)projectionComboBox.SelectedItem;
242      var instancesSeries = instanceMapChart.Series["InstancesSeries"];
243      var currentInstanceSeries = instanceMapChart.Series["CurrentInstanceSeries"];
244
245      instancesSeries.Points.Clear();
246      currentInstanceSeries.Points.Clear();
247
248      foreach (var run in Content.ProblemInstances) {
249        var xKey = "Projection." + projection + ".X";
250        var yKey = "Projection." + projection + ".Y";
251        if (!run.Results.ContainsKey(xKey) || !run.Results.ContainsKey(yKey)
252          || !(run.Results[xKey] is Data.DoubleValue) || !(run.Results[yKey] is Data.DoubleValue)) continue;
253        var x = ((Data.DoubleValue)run.Results[xKey]).Value;
254        var y = ((Data.DoubleValue)run.Results[yKey]).Value;
255        var dataPoint = new DataPoint(x, y) {
256          Label = run.Name
257        };
258        instancesSeries.Points.Add(dataPoint);
259      }
260
261      var curPoint = Content.ProjectCurrentInstance(projection);
262      if (curPoint != null) {
263        var dp = new DataPoint(curPoint.Item1, curPoint.Item2) {
264          Label = Content.Problem.Problem.Name
265        };
266        currentInstanceSeries.Points.Add(dp);
267      }
268    }
269
270    private void SuggestedInstancesComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
271      SetEnabledStateOfControls();
272    }
273  }
274}
Note: See TracBrowser for help on using the repository browser.