Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2457:

  • added two types of problem instance mappings: PCA and MDS
File size: 11.5 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
22using System;
23using System.ComponentModel;
[13475]24using System.Linq;
[13561]25using System.Text.RegularExpressions;
[8955]26using System.Windows.Forms;
[13561]27using System.Windows.Forms.DataVisualization.Charting;
[13475]28using HeuristicLab.Common.Resources;
[8955]29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
[12847]31using HeuristicLab.Optimization;
[13551]32using HeuristicLab.Optimization.Views;
[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;
[8955]106        }
107      } finally { SuppressEvents = false; }
[12860]108      UpdateSuggestedInstancesCombobox();
[8955]109    }
[8956]110
[8955]111    protected override void SetEnabledStateOfControls() {
112      base.SetEnabledStateOfControls();
[12804]113      maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked;
[13551]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;
[12847]117      algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked;
[8955]118      runsView.Enabled = Content != null;
[13551]119      kbViewHost.Enabled = Content != null && !okbDownloadInProgress;
120      problemInstancesView.Enabled = Content != null && !okbDownloadInProgress;
[13475]121      refreshMapButton.Enabled = Content != null;
[13551]122      okbDownloadButton.Enabled = Content != null && Content.Problem != null && Content.Problem.ProblemId >= 0 && !ReadOnly && !Locked && !okbDownloadInProgress;
[8955]123    }
124
[12847]125    private void UpdateSuggestedInstancesCombobox() {
[12860]126      var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem;
127      var prevNewIndex = -1;
128      suggestedInstancesComboBox.Items.Clear();
129      if (Content == null) return;
[12847]130
[12860]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      }
[12847]139    }
140
[8955]141    #region Event Handlers
142    #region Content events
[13551]143    private void ContentOnProblemChanged(object sender, EventArgs eventArgs) {
144      UpdateSuggestedInstancesCombobox();
145      SetEnabledStateOfControls();
146    }
147
148    private void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
[12825]149      if (InvokeRequired) {
[13551]150        Invoke((Action<object, PropertyChangedEventArgs>)ContentOnPropertyChanged, sender, e);
[12825]151        return;
152      }
[8961]153      SuppressEvents = true;
154      try {
[12804]155        switch (e.PropertyName) {
[13551]156          case "KnowledgeBase": kbViewHost.Content = Content.KnowledgeBase; break;
[12804]157          case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break;
[13551]158          case "Problem":
159            problemViewHost.Content = Content.Problem;
160            Content.Problem.ProblemChanged += ContentOnProblemChanged;
161            break;
[13475]162          case "ProblemInstances": problemInstancesView.Content = Content.ProblemInstances; break;
[12804]163        }
[8961]164      } finally { SuppressEvents = false; }
[13475]165      SetEnabledStateOfControls();
[8961]166    }
[12847]167
168    private void SuggestedInstancesOnChanged(object sender, EventArgs e) {
[12860]169      UpdateSuggestedInstancesCombobox();
[12847]170    }
[8955]171    #endregion
172
173    #region Control events
[13561]174    private void MaxEvaluationsTextBoxOnValidating(object sender, CancelEventArgs e) {
[8961]175      if (SuppressEvents) return;
[12804]176      if (InvokeRequired) {
[13561]177        Invoke((Action<object, CancelEventArgs>)MaxEvaluationsTextBoxOnValidating, sender, e);
[12804]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    }
[8955]190    #endregion
[13561]191    #endregion
[13475]192
[13561]193    private void RefreshMapButtonOnClick(object sender, EventArgs e) {
194      Content.UpdateInstanceProjection();
195      UpdateProjectionComboBox();
[13475]196    }
[13561]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);
[13475]206      }
207    }
208
[13561]209    private void OkbDownloadButtonOnClick(object sender, EventArgs e) {
[13551]210      if (Content.Problem.ProblemId < 0) {
211        MessageBox.Show("Please select a problem instance first.");
212        return;
213      }
[13485]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    }
[13475]221
[13485]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;
[13475]229      }
230    }
[13551]231
[13561]232    private void AlgorithmStartButtonOnClick(object sender, EventArgs e) {
[13551]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    }
[13561]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    }
[8955]270  }
271}
Note: See TracBrowser for help on using the repository browser.