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

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

#2457:

  • added two types of problem instance mappings: PCA and MDS
File size: 11.5 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.ComponentModel;
24using System.Linq;
25using System.Text.RegularExpressions;
26using System.Windows.Forms;
27using System.Windows.Forms.DataVisualization.Charting;
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 MaxEvaluationsTextBoxOnValidating(object sender, CancelEventArgs e) {
175      if (SuppressEvents) return;
176      if (InvokeRequired) {
177        Invoke((Action<object, CancelEventArgs>)MaxEvaluationsTextBoxOnValidating, 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    #endregion
192
193    private void RefreshMapButtonOnClick(object sender, EventArgs e) {
194      Content.UpdateInstanceProjection();
195      UpdateProjectionComboBox();
196    }
197
198    private void UpdateProjectionComboBox() {
199      projectionComboBox.Items.Clear();
200      var projStrings = Content.ProblemInstances
201        .SelectMany(x => x.Results.Where(y => Regex.IsMatch(y.Key, "^Projection[.].*[.][XY]$")))
202        .Select(x => Regex.Match(x.Key, "Projection[.](?<g>.*)[.][XY]").Groups["g"].Value)
203        .Distinct();
204      foreach (var str in projStrings) {
205        projectionComboBox.Items.Add(str);
206      }
207    }
208
209    private void OkbDownloadButtonOnClick(object sender, EventArgs e) {
210      if (Content.Problem.ProblemId < 0) {
211        MessageBox.Show("Please select a problem instance first.");
212        return;
213      }
214      var progress = new Progress();
215      progress.ProgressStateChanged += OkbDownloadProgressOnStateChanged;
216      Content.UpdateKnowledgeBaseAsync(progress);
217      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(progressPanel, progress);
218      progressPanel.Visible = true;
219      SetEnabledStateOfControls();
220    }
221
222    private void OkbDownloadProgressOnStateChanged(object sender, EventArgs eventArgs) {
223      var progress = (IProgress)sender;
224      okbDownloadInProgress = progress.ProgressState == ProgressState.Started;
225      SetEnabledStateOfControls();
226      if (!okbDownloadInProgress) {
227        progressPanel.Visible = false;
228        progress.ProgressStateChanged -= OkbDownloadProgressOnStateChanged;
229      }
230    }
231
232    private void AlgorithmStartButtonOnClick(object sender, EventArgs e) {
233      var selectedInstance = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
234      var clone = (IAlgorithm)selectedInstance.Clone();
235      clone.Prepare(true);
236      clone.Start();
237      algorithmViewHost.Content = clone.Results;
238    }
239
240    private void ProjectionComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
241      if (projectionComboBox.SelectedIndex < 0) return;
242      var projection = (string)projectionComboBox.SelectedItem;
243      var instancesSeries = instanceMapChart.Series["InstancesSeries"];
244      var currentInstanceSeries = instanceMapChart.Series["CurrentInstanceSeries"];
245
246      instancesSeries.Points.Clear();
247      currentInstanceSeries.Points.Clear();
248
249      foreach (var run in Content.ProblemInstances) {
250        var xKey = "Projection." + projection + ".X";
251        var yKey = "Projection." + projection + ".Y";
252        if (!run.Results.ContainsKey(xKey) || !run.Results.ContainsKey(yKey)
253          || !(run.Results[xKey] is Data.DoubleValue) || !(run.Results[yKey] is Data.DoubleValue)) continue;
254        var x = ((Data.DoubleValue)run.Results[xKey]).Value;
255        var y = ((Data.DoubleValue)run.Results[yKey]).Value;
256        var dataPoint = new DataPoint(x, y) {
257          Label = run.Name
258        };
259        instancesSeries.Points.Add(dataPoint);
260      }
261
262      var curPoint = Content.ProjectCurrentInstance(projection);
263      if (curPoint != null) {
264        var dp = new DataPoint(curPoint.Item1, curPoint.Item2) {
265          Label = Content.Problem.Problem.Name
266        };
267        currentInstanceSeries.Points.Add(dp);
268      }
269    }
270  }
271}
Note: See TracBrowser for help on using the repository browser.