#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.Analysis; using HeuristicLab.Analysis.QualityAnalysis; using HeuristicLab.Common; using HeuristicLab.Common.Resources; using HeuristicLab.Core.Views; using HeuristicLab.Data; using HeuristicLab.MainForm; using HeuristicLab.Optimization; using HeuristicLab.Optimization.Views; using HeuristicLab.OptimizationExpertSystem.Common; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace HeuristicLab.OptimizationExpertSystem { [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; Content.SolutionPool.ItemsAdded -= SolutionPoolOnChanged; Content.SolutionPool.ItemsRemoved -= SolutionPoolOnChanged; Content.SolutionPool.ItemsReplaced -= SolutionPoolOnChanged; Content.SolutionPool.CollectionReset -= SolutionPoolOnChanged; 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; Content.SolutionPool.ItemsAdded += SolutionPoolOnChanged; Content.SolutionPool.ItemsRemoved += SolutionPoolOnChanged; Content.SolutionPool.ItemsReplaced += SolutionPoolOnChanged; Content.SolutionPool.CollectionReset += SolutionPoolOnChanged; 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; solutionsViewHost.Content = null; kbViewHost.Content = null; problemInstancesView.Content = null; } else { maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); problemViewHost.Content = Content.Problem; runsView.Content = Content.Runs; solutionsViewHost.Content = Content.SolutionPool; kbViewHost.ViewType = typeof(RunCollectionRLDView); kbViewHost.Content = Content.KnowledgeBase; problemInstancesView.Content = Content.ProblemInstances; algorithmViewHost.Content = Content.CurrentResult; } } finally { SuppressEvents = false; } UpdateSuggestedInstancesCombobox(); UpdateNamesComboboxes(); UpdateSimilarityCalculators(); } 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; } } private void UpdateNamesComboboxes() { var selectedSolutionName = solutionNameComboBox.SelectedIndex >= 0 ? (string)solutionNameComboBox.SelectedItem : string.Empty; var selectedQualityName = qualityNameComboBox.SelectedIndex >= 0 ? (string)qualityNameComboBox.SelectedItem : string.Empty; solutionNameComboBox.Items.Clear(); qualityNameComboBox.Items.Clear(); if (Content == null) return; var qualityNames = new HashSet(Content.SolutionPool.SelectMany(x => x.Variables).Where(x => x.Value is DoubleValue).Select(x => x.Name)); var solutionNames = Content.SolutionPool.SelectMany(x => x.Variables).Where(x => !qualityNames.Contains(x.Name)); foreach (var sn in solutionNames.GroupBy(x => x.Name).OrderBy(x => x.Key)) { solutionNameComboBox.Items.Add(sn.Key); // either it was previously selected, or the variable value is defined in the HeuristicLab.Encodings sub-namespace if (sn.Key == selectedSolutionName || (string.IsNullOrEmpty(selectedSolutionName) && sn.All(x => x.Value != null && x.Value.GetType().FullName.StartsWith("HeuristicLab.Encodings.")))) solutionNameComboBox.SelectedItem = sn.Key; } foreach (var qn in qualityNames.OrderBy(x => x)) { qualityNameComboBox.Items.Add(qn); if (qn == selectedQualityName || string.IsNullOrEmpty(selectedQualityName)) qualityNameComboBox.SelectedItem = qn; } } private void UpdateSimilarityCalculators() { var selected = (ISolutionSimilarityCalculator)(similarityComboBox.SelectedIndex >= 0 ? similarityComboBox.SelectedItem : null); similarityComboBox.Items.Clear(); if (Content == null || Content.Problem == null) return; foreach (var calc in Content.Problem.Operators.OfType()) { similarityComboBox.Items.Add(calc); if (selected != null && calc.ItemName == selected.ItemName) similarityComboBox.SelectedItem = calc; } if (selected == null && similarityComboBox.Items.Count > 0) similarityComboBox.SelectedIndex = 0; } #region Event Handlers #region Content events private void ContentOnProblemChanged(object sender, EventArgs eventArgs) { UpdateSuggestedInstancesCombobox(); UpdateSimilarityCalculators(); 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(); } private void SolutionPoolOnChanged(object sender, EventArgs e) { UpdateNamesComboboxes(); if (qualityNameComboBox.SelectedIndex >= 0) { var qualityName = (string)qualityNameComboBox.SelectedItem; UpdateSolutionQualityAnalysis(qualityName); } if (similarityComboBox.SelectedIndex >= 0) { var calculator = (ISolutionSimilarityCalculator)similarityComboBox.SelectedItem; UpdateSolutionDiversityAnalysis(calculator); UpdateSolutionFdcAnalysis(calculator); UpdateSolutionLengthScaleAnalysis(calculator); } else solutionsDiversityViewHost.Content = null; } #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 UpdateSolutionQualityAnalysis(string qualityName) { var dt = solutionsQualityViewHost.Content as DataTable; if (dt == null) { dt = QualityDistributionAnalyzer.PrepareTable(qualityName); solutionsQualityViewHost.Content = dt; } QualityDistributionAnalyzer.UpdateTable(dt, Content.SolutionPool.Where(x => x.Variables.ContainsKey(qualityName)).Select(x => x.Variables[qualityName].Value).OfType().Select(x => x.Value)); } private void UpdateSolutionDiversityAnalysis(ISolutionSimilarityCalculator calculator) { try { solutionsDiversityViewHost.Content = null; if (Content.SolutionPool == null) return; var similarities = new double[Content.SolutionPool.Count, Content.SolutionPool.Count]; for (int i = 0; i < Content.SolutionPool.Count; i++) { for (int j = 0; j < Content.SolutionPool.Count; j++) similarities[i, j] = calculator.CalculateSolutionSimilarity(Content.SolutionPool[i], Content.SolutionPool[j]); } var hm = new HeatMap(similarities, "Solution Similarities", 0.0, 1.0); solutionsDiversityViewHost.Content = hm; } catch { } } private void UpdateSolutionFdcAnalysis(ISolutionSimilarityCalculator calculator) { try { solutionsFdcViewHost.Content = null; if (Content.SolutionPool == null) return; var points = new List>(); for (int i = 0; i < Content.SolutionPool.Count; i++) { for (int j = 0; j < Content.SolutionPool.Count; j++) { if (i == j) continue; var qDiff = Math.Abs(((DoubleValue)Content.SolutionPool[i].Variables[calculator.QualityVariableName].Value).Value - ((DoubleValue)Content.SolutionPool[j].Variables[calculator.QualityVariableName].Value).Value); points.Add(new Point2D(qDiff, 1.0 - calculator.CalculateSolutionSimilarity(Content.SolutionPool[i], Content.SolutionPool[j]))); } } var splot = new ScatterPlot("Fitness-Distance", ""); var row = new ScatterPlotDataRow("Fdc", "", points); row.VisualProperties.PointSize = 7; splot.Rows.Add(row); solutionsFdcViewHost.Content = splot; } catch { } } private void UpdateSolutionLengthScaleAnalysis(ISolutionSimilarityCalculator calculator) { try { solutionsLengthScaleViewHost.Content = null; if (Content.SolutionPool == null) return; var dt = solutionsLengthScaleViewHost.Content as DataTable; if (dt == null) { dt = QualityDistributionAnalyzer.PrepareTable("Length Scale"); solutionsLengthScaleViewHost.Content = dt; } QualityDistributionAnalyzer.UpdateTable(dt, CalculateLengthScale(calculator)); } catch { } } private IEnumerable CalculateLengthScale(ISolutionSimilarityCalculator calculator) { for (int i = 0; i < Content.SolutionPool.Count; i++) { for (int j = 0; j < Content.SolutionPool.Count; j++) { if (i == j) continue; var sim = calculator.CalculateSolutionSimilarity(Content.SolutionPool[i], Content.SolutionPool[j]); if (sim.IsAlmost(0)) continue; var qDiff = Math.Abs(((DoubleValue)Content.SolutionPool[i].Variables[calculator.QualityVariableName].Value).Value - ((DoubleValue)Content.SolutionPool[j].Variables[calculator.QualityVariableName].Value).Value); yield return qDiff / sim; } } } private void SuggestedInstancesComboBoxOnSelectedIndexChanged(object sender, EventArgs e) { SetEnabledStateOfControls(); } private void qualityNameComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (qualityNameComboBox.SelectedIndex < 0) return; solutionsQualityViewHost.Content = null; solutionsDiversityViewHost.Content = null; var qualityName = (string)qualityNameComboBox.SelectedItem; UpdateSolutionQualityAnalysis(qualityName); var calculator = (ISolutionSimilarityCalculator)similarityComboBox.SelectedItem; if (calculator != null) calculator.QualityVariableName = qualityName; UpdateSolutionDiversityAnalysis(calculator); UpdateSolutionFdcAnalysis(calculator); UpdateSolutionLengthScaleAnalysis(calculator); } private void solutionNameComboBox_SelectedIndexChanged(object sender, EventArgs e) { var calculator = (ISolutionSimilarityCalculator)similarityComboBox.SelectedItem; if (calculator != null) calculator.SolutionVariableName = (string)solutionNameComboBox.SelectedItem; UpdateSolutionDiversityAnalysis(calculator); UpdateSolutionFdcAnalysis(calculator); UpdateSolutionLengthScaleAnalysis(calculator); } private void similarityComboBox_SelectedIndexChanged(object sender, EventArgs e) { var calculator = (ISolutionSimilarityCalculator)similarityComboBox.SelectedItem; if (calculator != null) { calculator.SolutionVariableName = (string)solutionNameComboBox.SelectedItem; calculator.QualityVariableName = (string)qualityNameComboBox.SelectedItem; } UpdateSolutionDiversityAnalysis(calculator); UpdateSolutionFdcAnalysis(calculator); UpdateSolutionLengthScaleAnalysis(calculator); } } }