Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2457: fixed bugs in suggestions

File size: 11.6 KB
RevLine 
[8955]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
[13649]22using HeuristicLab.Common.Resources;
23using HeuristicLab.Core.Views;
24using HeuristicLab.MainForm;
25using HeuristicLab.Optimization;
26using HeuristicLab.Optimization.Views;
[8955]27using System;
28using System.ComponentModel;
[13475]29using System.Linq;
[13561]30using System.Text.RegularExpressions;
[8955]31using System.Windows.Forms;
[13561]32using System.Windows.Forms.DataVisualization.Charting;
[8955]33
[12847]34namespace HeuristicLab.OptimizationExpertSystem.Views {
35  [View("Expert-system Optimizer View")]
[12860]36  [Content(typeof(ExpertSystem), IsDefaultView = true)]
37  public partial class ExpertSystemView : NamedItemView {
[12847]38    protected TypeSelectorDialog problemTypeSelectorDialog;
[8955]39    protected virtual bool SuppressEvents { get; set; }
[13485]40    private bool okbDownloadInProgress;
[8955]41
[12860]42    public new ExpertSystem Content {
43      get { return (ExpertSystem)base.Content; }
[8955]44      set { base.Content = value; }
45    }
46
[12860]47    public ExpertSystemView() {
[8955]48      InitializeComponent();
[13551]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;
[13475]53      refreshMapButton.Text = string.Empty;
54      refreshMapButton.Image = VSImageLibrary.Refresh;
[8955]55    }
56
57    protected override void Dispose(bool disposing) {
58      if (disposing) {
[12847]59        if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
[8955]60        if (components != null) components.Dispose();
61      }
62      base.Dispose(disposing);
63    }
64
65    protected override void DeregisterContentEvents() {
[13551]66      Content.PropertyChanged -= ContentOnPropertyChanged;
[12847]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;
[13551]72      if (Content.Problem != null) Content.Problem.ProblemChanged -= ContentOnProblemChanged;
[8955]73      base.DeregisterContentEvents();
74    }
75    protected override void RegisterContentEvents() {
76      base.RegisterContentEvents();
[13551]77      Content.PropertyChanged += ContentOnPropertyChanged;
[12847]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;
[13551]83      if (Content.Problem != null) Content.Problem.ProblemChanged += ContentOnProblemChanged;
[8955]84    }
85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88      SuppressEvents = true;
[13485]89      okbDownloadInProgress = false;
[8955]90      try {
91        if (Content == null) {
[12804]92          maxEvaluationsTextBox.Text = String.Empty;
93
[12847]94          problemViewHost.Content = null;
[8955]95          algorithmViewHost.Content = null;
96          runsView.Content = null;
[13551]97          kbViewHost.Content = null;
[13475]98          problemInstancesView.Content = null;
[8955]99        } else {
[12804]100          maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString();
[12847]101          problemViewHost.Content = Content.Problem;
[8955]102          runsView.Content = Content.Runs;
[13551]103          kbViewHost.ViewType = typeof(RunCollectionRLDView);
104          kbViewHost.Content = Content.KnowledgeBase;
[13475]105          problemInstancesView.Content = Content.ProblemInstances;
[13649]106          algorithmViewHost.Content = Content.CurrentResult;
[8955]107        }
108      } finally { SuppressEvents = false; }
[12860]109      UpdateSuggestedInstancesCombobox();
[8955]110    }
[8956]111
[8955]112    protected override void SetEnabledStateOfControls() {
113      base.SetEnabledStateOfControls();
[12804]114      maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked;
[13551]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;
[12847]118      algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked;
[8955]119      runsView.Enabled = Content != null;
[13551]120      kbViewHost.Enabled = Content != null && !okbDownloadInProgress;
121      problemInstancesView.Enabled = Content != null && !okbDownloadInProgress;
[13475]122      refreshMapButton.Enabled = Content != null;
[13551]123      okbDownloadButton.Enabled = Content != null && Content.Problem != null && Content.Problem.ProblemId >= 0 && !ReadOnly && !Locked && !okbDownloadInProgress;
[8955]124    }
125
[12847]126    private void UpdateSuggestedInstancesCombobox() {
[12860]127      var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
128      var prevNewIndex = -1;
129      suggestedInstancesComboBox.Items.Clear();
130      if (Content == null) return;
[12847]131
[12860]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      }
[12847]140    }
141
[8955]142    #region Event Handlers
143    #region Content events
[13551]144    private void ContentOnProblemChanged(object sender, EventArgs eventArgs) {
145      UpdateSuggestedInstancesCombobox();
146      SetEnabledStateOfControls();
147    }
148
149    private void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
[12825]150      if (InvokeRequired) {
[13551]151        Invoke((Action<object, PropertyChangedEventArgs>)ContentOnPropertyChanged, sender, e);
[12825]152        return;
153      }
[8961]154      SuppressEvents = true;
155      try {
[12804]156        switch (e.PropertyName) {
[13551]157          case "KnowledgeBase": kbViewHost.Content = Content.KnowledgeBase; break;
[12804]158          case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break;
[13551]159          case "Problem":
160            problemViewHost.Content = Content.Problem;
161            Content.Problem.ProblemChanged += ContentOnProblemChanged;
162            break;
[13475]163          case "ProblemInstances": problemInstancesView.Content = Content.ProblemInstances; break;
[13649]164          case "CurrentResult": algorithmViewHost.Content = Content.CurrentResult; break;
[12804]165        }
[8961]166      } finally { SuppressEvents = false; }
[13475]167      SetEnabledStateOfControls();
[8961]168    }
[12847]169
170    private void SuggestedInstancesOnChanged(object sender, EventArgs e) {
[12860]171      UpdateSuggestedInstancesCombobox();
[12847]172    }
[8955]173    #endregion
174
175    #region Control events
[13561]176    private void MaxEvaluationsTextBoxOnValidating(object sender, CancelEventArgs e) {
[8961]177      if (SuppressEvents) return;
[12804]178      if (InvokeRequired) {
[13561]179        Invoke((Action<object, CancelEventArgs>)MaxEvaluationsTextBoxOnValidating, sender, e);
[12804]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    }
[8955]192    #endregion
[13561]193    #endregion
[13475]194
[13561]195    private void RefreshMapButtonOnClick(object sender, EventArgs e) {
196      Content.UpdateInstanceProjection();
197      UpdateProjectionComboBox();
[13475]198    }
[13561]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);
[13475]208      }
209    }
210
[13561]211    private void OkbDownloadButtonOnClick(object sender, EventArgs e) {
[13569]212      if (Content.Problem.ProblemId == -1) {
[13551]213        MessageBox.Show("Please select a problem instance first.");
214        return;
215      }
[13485]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    }
[13475]223
[13485]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;
[13475]231      }
232    }
[13551]233
[13561]234    private void AlgorithmStartButtonOnClick(object sender, EventArgs e) {
[13649]235      if (suggestedInstancesComboBox.SelectedIndex >= 0)
236        Content.StartAlgorithmAsync(suggestedInstancesComboBox.SelectedIndex);
[13551]237    }
[13561]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    }
[13649]269
270    private void SuggestedInstancesComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
271      SetEnabledStateOfControls();
272    }
[8955]273  }
274}
Note: See TracBrowser for help on using the repository browser.