#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common.Resources; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Optimization; using HeuristicLab.Optimization.Views; using System; using System.ComponentModel; using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace HeuristicLab.OptimizationExpertSystem.Views { [View("Expert-system Optimizer View")] [Content(typeof(ExpertSystem), IsDefaultView = true)] public partial class ExpertSystemView : NamedItemView { protected TypeSelectorDialog problemTypeSelectorDialog; protected virtual bool SuppressEvents { get; set; } private bool okbDownloadInProgress; public new ExpertSystem Content { get { return (ExpertSystem)base.Content; } set { base.Content = value; } } public ExpertSystemView() { InitializeComponent(); // brings progress panel to front (it is not visible by default, but obstructs other elements in designer) this.Controls.SetChildIndex(this.progressPanel, 0); algorithmStartButton.Text = string.Empty; algorithmStartButton.Image = VSImageLibrary.Play; refreshMapButton.Text = string.Empty; refreshMapButton.Image = VSImageLibrary.Refresh; } protected override void Dispose(bool disposing) { if (disposing) { if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose(); if (components != null) components.Dispose(); } base.Dispose(disposing); } protected override void DeregisterContentEvents() { Content.PropertyChanged -= ContentOnPropertyChanged; Content.SuggestedInstances.CollectionReset -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsAdded -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsMoved -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsRemoved -= SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsReplaced -= SuggestedInstancesOnChanged; if (Content.Problem != null) Content.Problem.ProblemChanged -= ContentOnProblemChanged; base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.PropertyChanged += ContentOnPropertyChanged; Content.SuggestedInstances.CollectionReset += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsAdded += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsMoved += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsRemoved += SuggestedInstancesOnChanged; Content.SuggestedInstances.ItemsReplaced += SuggestedInstancesOnChanged; if (Content.Problem != null) Content.Problem.ProblemChanged += ContentOnProblemChanged; } protected override void OnContentChanged() { base.OnContentChanged(); SuppressEvents = true; okbDownloadInProgress = false; try { if (Content == null) { maxEvaluationsTextBox.Text = String.Empty; problemViewHost.Content = null; algorithmViewHost.Content = null; runsView.Content = null; kbViewHost.Content = null; problemInstancesView.Content = null; } else { maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); problemViewHost.Content = Content.Problem; runsView.Content = Content.Runs; kbViewHost.ViewType = typeof(RunCollectionRLDView); kbViewHost.Content = Content.KnowledgeBase; problemInstancesView.Content = Content.ProblemInstances; algorithmViewHost.Content = Content.CurrentResult; } } finally { SuppressEvents = false; } UpdateSuggestedInstancesCombobox(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked; problemViewHost.Enabled = Content != null && !ReadOnly && !Locked && !okbDownloadInProgress; suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked && !okbDownloadInProgress; algorithmStartButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0; algorithmViewHost.Enabled = Content != null && !ReadOnly && !Locked; runsView.Enabled = Content != null; kbViewHost.Enabled = Content != null && !okbDownloadInProgress; problemInstancesView.Enabled = Content != null && !okbDownloadInProgress; refreshMapButton.Enabled = Content != null; okbDownloadButton.Enabled = Content != null && Content.Problem != null && Content.Problem.ProblemId >= 0 && !ReadOnly && !Locked && !okbDownloadInProgress; } private void UpdateSuggestedInstancesCombobox() { var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem; var prevNewIndex = -1; suggestedInstancesComboBox.Items.Clear(); if (Content == null) return; for (var i = 0; i < Content.SuggestedInstances.Count; i++) { suggestedInstancesComboBox.Items.Add(Content.SuggestedInstances[i]); if (prevSelection == null || Content.SuggestedInstances[i].Name == prevSelection.Name) prevNewIndex = prevSelection == null ? 0 : i; } if (prevNewIndex >= 0) { suggestedInstancesComboBox.SelectedIndex = prevNewIndex; } } #region Event Handlers #region Content events private void ContentOnProblemChanged(object sender, EventArgs eventArgs) { UpdateSuggestedInstancesCombobox(); SetEnabledStateOfControls(); } private void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) { if (InvokeRequired) { Invoke((Action)ContentOnPropertyChanged, sender, e); return; } SuppressEvents = true; try { switch (e.PropertyName) { case "KnowledgeBase": kbViewHost.Content = Content.KnowledgeBase; break; case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break; case "Problem": problemViewHost.Content = Content.Problem; Content.Problem.ProblemChanged += ContentOnProblemChanged; break; case "ProblemInstances": problemInstancesView.Content = Content.ProblemInstances; break; case "CurrentResult": algorithmViewHost.Content = Content.CurrentResult; break; } } finally { SuppressEvents = false; } SetEnabledStateOfControls(); } private void SuggestedInstancesOnChanged(object sender, EventArgs e) { UpdateSuggestedInstancesCombobox(); } #endregion #region Control events private void MaxEvaluationsTextBoxOnValidating(object sender, CancelEventArgs e) { if (SuppressEvents) return; if (InvokeRequired) { Invoke((Action)MaxEvaluationsTextBoxOnValidating, sender, e); return; } int value; if (!int.TryParse(maxEvaluationsTextBox.Text, out value)) { e.Cancel = !maxEvaluationsTextBox.ReadOnly && maxEvaluationsTextBox.Enabled; errorProvider.SetError(maxEvaluationsTextBox, "Please enter a valid integer number."); } else { Content.MaximumEvaluations = value; e.Cancel = false; errorProvider.SetError(maxEvaluationsTextBox, null); } } #endregion #endregion private void RefreshMapButtonOnClick(object sender, EventArgs e) { Content.UpdateInstanceProjection(); UpdateProjectionComboBox(); } private void UpdateProjectionComboBox() { projectionComboBox.Items.Clear(); var projStrings = Content.ProblemInstances .SelectMany(x => x.Results.Where(y => Regex.IsMatch(y.Key, "^Projection[.].*[.][XY]$"))) .Select(x => Regex.Match(x.Key, "Projection[.](?.*)[.][XY]").Groups["g"].Value) .Distinct(); foreach (var str in projStrings) { projectionComboBox.Items.Add(str); } } private void OkbDownloadButtonOnClick(object sender, EventArgs e) { if (Content.Problem.ProblemId == -1) { MessageBox.Show("Please select a problem instance first."); return; } var progress = new Progress(); progress.ProgressStateChanged += OkbDownloadProgressOnStateChanged; Content.UpdateKnowledgeBaseAsync(progress); MainFormManager.GetMainForm().AddOperationProgressToView(progressPanel, progress); progressPanel.Visible = true; SetEnabledStateOfControls(); } private void OkbDownloadProgressOnStateChanged(object sender, EventArgs eventArgs) { var progress = (IProgress)sender; okbDownloadInProgress = progress.ProgressState == ProgressState.Started; SetEnabledStateOfControls(); if (!okbDownloadInProgress) { progressPanel.Visible = false; progress.ProgressStateChanged -= OkbDownloadProgressOnStateChanged; } } private void AlgorithmStartButtonOnClick(object sender, EventArgs e) { if (suggestedInstancesComboBox.SelectedIndex >= 0) Content.StartAlgorithmAsync(suggestedInstancesComboBox.SelectedIndex); } private void ProjectionComboBoxOnSelectedIndexChanged(object sender, EventArgs e) { if (projectionComboBox.SelectedIndex < 0) return; var projection = (string)projectionComboBox.SelectedItem; var instancesSeries = instanceMapChart.Series["InstancesSeries"]; var currentInstanceSeries = instanceMapChart.Series["CurrentInstanceSeries"]; instancesSeries.Points.Clear(); currentInstanceSeries.Points.Clear(); foreach (var run in Content.ProblemInstances) { var xKey = "Projection." + projection + ".X"; var yKey = "Projection." + projection + ".Y"; if (!run.Results.ContainsKey(xKey) || !run.Results.ContainsKey(yKey) || !(run.Results[xKey] is Data.DoubleValue) || !(run.Results[yKey] is Data.DoubleValue)) continue; var x = ((Data.DoubleValue)run.Results[xKey]).Value; var y = ((Data.DoubleValue)run.Results[yKey]).Value; var dataPoint = new DataPoint(x, y) { Label = run.Name }; instancesSeries.Points.Add(dataPoint); } var curPoint = Content.ProjectCurrentInstance(projection); if (curPoint != null) { var dp = new DataPoint(curPoint.Item1, curPoint.Item2) { Label = Content.Problem.Problem.Name }; currentInstanceSeries.Points.Add(dp); } } private void SuggestedInstancesComboBoxOnSelectedIndexChanged(object sender, EventArgs e) { SetEnabledStateOfControls(); } } }