#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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.Clients.OKB.RunCreation; using HeuristicLab.Common; using HeuristicLab.Common.Resources; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.Data; using HeuristicLab.Data.Views; using HeuristicLab.MainForm; using HeuristicLab.MainForm.WindowsForms; 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("Knowledge Center (all-in-one view)")] [Content(typeof(KnowledgeCenter), IsDefaultView = true)] public partial class KnowledgeCenterAllinOneView : AsynchronousContentView { private EnumValueView seedingStrategyView; private CheckedItemListView seedingSolutionsView; protected virtual bool SuppressEvents { get; set; } private bool okbDownloadInProgress; public new KnowledgeCenter Content { get { return (KnowledgeCenter)base.Content; } set { base.Content = value; } } public KnowledgeCenterAllinOneView() { 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; algorithmCloneButton.Text = string.Empty; algorithmCloneButton.Image = VSImageLibrary.Clone; refreshMapButton.Text = string.Empty; refreshMapButton.Image = VSImageLibrary.Refresh; seedingStrategyView = new EnumValueView() { Dock = DockStyle.Fill }; seedingSolutionsView = new CheckedItemListView() { Dock = DockStyle.Fill }; seedingStrategyPanel.Controls.Add(seedingStrategyView); solutionSeedingTabPage.Controls.Add(seedingSolutionsView); } #region Event Registration protected override void DeregisterContentEvents() { Content.AlgorithmInstances.CollectionReset -= SuggestedInstancesOnChanged; Content.AlgorithmInstances.ItemsAdded -= SuggestedInstancesOnChanged; Content.AlgorithmInstances.ItemsMoved -= SuggestedInstancesOnChanged; Content.AlgorithmInstances.ItemsRemoved -= SuggestedInstancesOnChanged; Content.AlgorithmInstances.ItemsReplaced -= SuggestedInstancesOnChanged; Content.SolutionSeedingPool.CollectionReset -= SeedingPoolOnChanged; Content.SolutionSeedingPool.ItemsAdded -= SeedingPoolOnChanged; Content.SolutionSeedingPool.ItemsRemoved -= SeedingPoolOnChanged; Content.SolutionSeedingPool.ItemsReplaced -= SeedingPoolOnChanged; Content.SolutionSeedingPool.CheckedItemsChanged -= SeedingPoolOnChanged; DeregisterProblemEvents(Content.Problem); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.AlgorithmInstances.CollectionReset += SuggestedInstancesOnChanged; Content.AlgorithmInstances.ItemsAdded += SuggestedInstancesOnChanged; Content.AlgorithmInstances.ItemsMoved += SuggestedInstancesOnChanged; Content.AlgorithmInstances.ItemsRemoved += SuggestedInstancesOnChanged; Content.AlgorithmInstances.ItemsReplaced += SuggestedInstancesOnChanged; Content.SolutionSeedingPool.CollectionReset += SeedingPoolOnChanged; Content.SolutionSeedingPool.ItemsAdded += SeedingPoolOnChanged; Content.SolutionSeedingPool.ItemsRemoved += SeedingPoolOnChanged; Content.SolutionSeedingPool.ItemsReplaced += SeedingPoolOnChanged; Content.SolutionSeedingPool.CheckedItemsChanged += SeedingPoolOnChanged; RegisterProblemEvents(Content.Problem); } private void DeregisterProblemEvents(OKBProblem problem) { if (problem == null) return; problem.Solutions.ItemsAdded -= SolutionsOnChanged; problem.Solutions.ItemsRemoved -= SolutionsOnChanged; problem.Solutions.ItemsReplaced -= SolutionsOnChanged; problem.Solutions.CollectionReset -= SolutionsOnChanged; problem.ProblemChanged -= ContentOnProblemChanged; } private void RegisterProblemEvents(OKBProblem problem) { if (problem == null) return; problem.Solutions.ItemsAdded += SolutionsOnChanged; problem.Solutions.ItemsRemoved += SolutionsOnChanged; problem.Solutions.ItemsReplaced += SolutionsOnChanged; problem.Solutions.CollectionReset += SolutionsOnChanged; problem.ProblemChanged += ContentOnProblemChanged; } #endregion protected override void OnContentChanged() { base.OnContentChanged(); SuppressEvents = true; okbDownloadInProgress = false; try { if (Content == null) { maxEvaluationsTextBox.Text = String.Empty; problemViewHost.Content = null; solverParametersView.Content = null; solverResultsView.Content = null; runsView.Content = null; kbViewHost.Content = null; problemInstancesView.Content = null; seedingStrategyView.Content = null; seedingSolutionsView.Content = null; } else { maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); problemViewHost.Content = Content.Problem; runsView.Content = Content.InstanceRuns; kbViewHost.ViewType = typeof(RunCollectionRLDView); kbViewHost.Content = Content.KnowledgeBase; problemInstancesView.Content = Content.ProblemInstances; solverResultsView.Content = null; seedingStrategyView.Content = Content.SeedingStrategy; seedingSolutionsView.Content = Content.SolutionSeedingPool; } } finally { SuppressEvents = false; } UpdateSuggestedInstancesCombobox(); UpdateSimilarityCalculators(); UpdateNamesComboboxes(); } 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; algorithmCloneButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0; 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; } #region Event Handlers #region Content events private void ContentOnProblemChanged(object sender, EventArgs eventArgs) { UpdateSuggestedInstancesCombobox(); UpdateSimilarityCalculators(); SetEnabledStateOfControls(); } private void SuggestedInstancesOnChanged(object sender, EventArgs e) { UpdateSuggestedInstancesCombobox(); } private void SeedingPoolOnChanged(object sender, EventArgs e) { UpdateSolutionVisualization(); } private void SolutionsOnChanged(object sender, EventArgs e) { UpdateNamesComboboxes(); UpdateSolutionVisualization(); } #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 = value; e.Cancel = false; //errorProvider.SetError(maxEvaluationsTextBox, null); } } private void RefreshMapButtonOnClick(object sender, EventArgs e) { UpdateProjectionComboBox(); } 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 e) { if (InvokeRequired) { Invoke((Action)OkbDownloadProgressOnStateChanged, sender, e); return; } var progress = (IProgress)sender; okbDownloadInProgress = progress.ProgressState == ProgressState.Started; SetEnabledStateOfControls(); if (!okbDownloadInProgress) { progressPanel.Visible = false; progress.ProgressStateChanged -= OkbDownloadProgressOnStateChanged; } } private async void AlgorithmStartButtonOnClick(object sender, EventArgs e) { if (suggestedInstancesComboBox.SelectedIndex >= 0) { solverResultsView.Content = await Content.StartAlgorithmAsync(suggestedInstancesComboBox.SelectedIndex); } } private void AlgorithmCloneButtonOnClick(object sender, EventArgs e) { if (suggestedInstancesComboBox.SelectedIndex >= 0) MainFormManager.MainForm.ShowContent((IAlgorithm)Content.AlgorithmInstances[suggestedInstancesComboBox.SelectedIndex].Clone()); } private void ProjectionComboBoxOnSelectedIndexChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke((Action)ProjectionComboBoxOnSelectedIndexChanged, sender, e); return; } 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 }; if (!Content.IsCurrentInstance(run)) instancesSeries.Points.Add(dataPoint); else currentInstanceSeries.Points.Add(dataPoint); } } private void SuggestedInstancesComboBoxOnSelectedIndexChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke((Action)SuggestedInstancesComboBoxOnSelectedIndexChanged, sender, e); return; } if (suggestedInstancesComboBox.SelectedIndex >= 0) { var alg = Content.AlgorithmInstances[suggestedInstancesComboBox.SelectedIndex]; solverParametersView.Content = alg.Parameters; var engineAlg = alg as EngineAlgorithm; if (engineAlg != null) operatorGraphViewHost.Content = engineAlg.OperatorGraph; else operatorGraphViewHost.Content = new Data.StringValue("Algorithm is not modeled as an operator graph."); } else { solverParametersView.Content = null; operatorGraphViewHost.Content = null; } SetEnabledStateOfControls(); } private void SimilarityComboBoxOnSelectedIndexChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke((Action)SimilarityComboBoxOnSelectedIndexChanged, sender, e); return; } var calculator = (ISolutionSimilarityCalculator)similarityComboBox.SelectedItem; if (calculator != null) { calculator.SolutionVariableName = (string)solutionNameComboBox.SelectedItem; calculator.QualityVariableName = Content.Problem.Problem.Evaluator.QualityParameter.ActualName; } UpdateSolutionDiversityAnalysis(calculator); UpdateSolutionFdcAnalysis(calculator, fdcBetweenBestCheckBox.Checked); UpdateSolutionLengthScaleAnalysis(calculator); UpdateSolutionNetworkAnalysis(calculator); } private void FdcBetweenBestCheckBoxOnCheckedChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke((Action)FdcBetweenBestCheckBoxOnCheckedChanged, sender, e); return; } UpdateSolutionFdcAnalysis((ISolutionSimilarityCalculator)similarityComboBox.SelectedItem, fdcBetweenBestCheckBox.Checked); } private void SolutionsNetworkChartOnMouseClick(object sender, MouseEventArgs e) { var result = solutionsNetworkChart.HitTest(e.X, e.Y); if (result.ChartElementType == ChartElementType.DataPoint) { var point = (DataPoint)result.Object; var solutionScope = point.Tag as IScope; if (solutionScope == null || !Content.SolutionSeedingPool.Contains(solutionScope)) return; Content.SolutionSeedingPool.SetItemCheckedState(solutionScope, !Content.SolutionSeedingPool.ItemChecked(solutionScope)); } } #endregion #endregion #region Control Configuration private void UpdateSuggestedInstancesCombobox() { var prevSelection = (IAlgorithm)suggestedInstancesComboBox.SelectedItem; var prevNewIndex = -1; suggestedInstancesComboBox.Items.Clear(); if (Content == null) return; for (var i = 0; i < Content.AlgorithmInstances.Count; i++) { suggestedInstancesComboBox.Items.Add(Content.AlgorithmInstances[i]); if (prevSelection == null || Content.AlgorithmInstances[i].Name == prevSelection.Name) prevNewIndex = prevSelection == null ? 0 : i; } if (prevNewIndex >= 0) { suggestedInstancesComboBox.SelectedIndex = prevNewIndex; } } 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; } private void UpdateNamesComboboxes() { var selectedSolutionName = solutionNameComboBox.SelectedIndex >= 0 ? (string)solutionNameComboBox.SelectedItem : string.Empty; solutionNameComboBox.Items.Clear(); if (Content == null) return; var solutionNames = Content.Problem.Solutions.Select(x => x.Solution).OfType().SelectMany(x => x.Variables); 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; } } 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); } } #endregion #region Visualization #region Solution Visualization public void UpdateSolutionVisualization() { if (InvokeRequired) { Invoke((Action)UpdateSolutionVisualization); return; } var qualityName = Content.Problem.Problem.Evaluator.QualityParameter.ActualName; UpdateSolutionQualityAnalysis(qualityName); if (similarityComboBox.SelectedIndex >= 0) { var calculator = (ISolutionSimilarityCalculator)similarityComboBox.SelectedItem; UpdateSolutionDiversityAnalysis(calculator); UpdateSolutionFdcAnalysis(calculator, fdcBetweenBestCheckBox.Checked); UpdateSolutionLengthScaleAnalysis(calculator); UpdateSolutionNetworkAnalysis(calculator); } else { solutionsDiversityViewHost.Content = null; solutionsFdcViewHost.Content = null; solutionsLengthScaleViewHost.Content = null; solutionsNetworkChart.Series.First().Points.Clear(); } } private void UpdateSolutionQualityAnalysis(string qualityName) { var dt = solutionsQualityViewHost.Content as DataTable; if (dt == null) { dt = QualityDistributionAnalyzer.PrepareTable(qualityName); dt.VisualProperties.Title = "Quality Distribution"; solutionsQualityViewHost.Content = dt; } QualityDistributionAnalyzer.UpdateTable(dt, GetSolutionScopes().Select(x => GetQuality(x, qualityName) ?? double.NaN).Where(x => !double.IsNaN(x))); } private void UpdateSolutionDiversityAnalysis(ISolutionSimilarityCalculator calculator) { try { solutionsDiversityViewHost.Content = null; var solutionScopes = GetSolutionScopes(); var similarities = new double[solutionScopes.Count, solutionScopes.Count]; for (var i = 0; i < solutionScopes.Count; i++) { for (var j = 0; j < solutionScopes.Count; j++) similarities[i, j] = calculator.CalculateSolutionSimilarity(solutionScopes[i], solutionScopes[j]); } var hm = new HeatMap(similarities, "Solution Similarities", 0.0, 1.0); solutionsDiversityViewHost.Content = hm; } catch { } } private void UpdateSolutionFdcAnalysis(ISolutionSimilarityCalculator calculator, bool distanceToBest) { try { solutionsFdcViewHost.Content = null; var solutionScopes = GetSolutionScopes(); var points = new List>(); if (distanceToBest) { var maximization = ((IValueParameter)Content.Problem.MaximizationParameter).Value.Value; var bestSolutions = (maximization ? solutionScopes.MaxItems(x => GetQuality(x, calculator.QualityVariableName) ?? double.NegativeInfinity) : solutionScopes.MinItems(x => GetQuality(x, calculator.QualityVariableName) ?? double.PositiveInfinity)).ToList(); foreach (var solScope in solutionScopes.Except(bestSolutions)) { var maxSimilarity = bestSolutions.Max(x => calculator.CalculateSolutionSimilarity(solScope, x)); var qDiff = (GetQuality(solScope, calculator.QualityVariableName) ?? double.NaN) - (GetQuality(bestSolutions[0], calculator.QualityVariableName) ?? double.NaN); points.Add(new Point2D(Math.Abs(qDiff), 1.0 - maxSimilarity)); } } else { for (int i = 0; i < solutionScopes.Count; i++) { for (int j = 0; j < solutionScopes.Count; j++) { if (i == j) continue; var qDiff = (GetQuality(solutionScopes[i], calculator.QualityVariableName) ?? double.NaN) - (GetQuality(solutionScopes[j], calculator.QualityVariableName) ?? double.NaN); if (double.IsNaN(qDiff)) continue; points.Add(new Point2D(Math.Abs(qDiff), 1.0 - calculator.CalculateSolutionSimilarity(solutionScopes[i], solutionScopes[j]))); } } } var splot = new ScatterPlot("Fitness-Distance", ""); splot.VisualProperties.XAxisTitle = "Absolute Fitness Difference"; splot.VisualProperties.XAxisMinimumFixedValue = 0.0; splot.VisualProperties.XAxisMinimumAuto = false; splot.VisualProperties.YAxisTitle = "Solution Distance"; splot.VisualProperties.YAxisMinimumFixedValue = 0.0; splot.VisualProperties.YAxisMinimumAuto = false; splot.VisualProperties.YAxisMaximumFixedValue = 1.0; splot.VisualProperties.YAxisMaximumAuto = false; 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; var dt = solutionsLengthScaleViewHost.Content as DataTable; if (dt == null) { dt = QualityDistributionAnalyzer.PrepareTable("Length Scale"); solutionsLengthScaleViewHost.Content = dt; } QualityDistributionAnalyzer.UpdateTable(dt, CalculateLengthScale(calculator)); } catch { solutionsLengthScaleViewHost.Content = null; } } private IEnumerable CalculateLengthScale(ISolutionSimilarityCalculator calculator) { var solutionScopes = GetSolutionScopes(); for (var i = 0; i < solutionScopes.Count; i++) { for (var j = 0; j < solutionScopes.Count; j++) { if (i == j) continue; var sim = calculator.CalculateSolutionSimilarity(solutionScopes[i], solutionScopes[j]); if (sim.IsAlmost(0)) continue; var qDiff = (GetQuality(solutionScopes[i], calculator.QualityVariableName) ?? double.NaN) - (GetQuality(solutionScopes[j], calculator.QualityVariableName) ?? double.NaN); if (!double.IsNaN(qDiff)) yield return Math.Abs(qDiff) / sim; } } } private void UpdateSolutionNetworkAnalysis(ISolutionSimilarityCalculator calculator) { var series = solutionsNetworkChart.Series["SolutionSeries"]; var seedingSeries = solutionsNetworkChart.Series["SeedingSolutionSeries"]; try { series.Points.Clear(); seedingSeries.Points.Clear(); var solutionScopes = GetSolutionScopes(); var dissimilarities = new DoubleMatrix(solutionScopes.Count, solutionScopes.Count); for (var i = 0; i < solutionScopes.Count; i++) { for (var j = 0; j < solutionScopes.Count; j++) { if (i == j) continue; dissimilarities[i, j] = 1.0 - calculator.CalculateSolutionSimilarity(solutionScopes[i], solutionScopes[j]); } } var coords = MultidimensionalScaling.KruskalShepard(dissimilarities); for (var i = 0; i < coords.Rows; i++) { var quality = GetQuality(solutionScopes[i], calculator.QualityVariableName) ?? double.NaN; var dataPoint = new DataPoint() { Name = (i + 1).ToString(), XValue = coords[i, 0], YValues = new[] {coords[i, 1], quality}, Label = i + ": " + quality, Tag = solutionScopes[i] }; if (Content.SolutionSeedingPool.Contains(solutionScopes[i]) && Content.SolutionSeedingPool.ItemChecked(solutionScopes[i])) seedingSeries.Points.Add(dataPoint); else series.Points.Add(dataPoint); } } catch { // problems in calculating the similarity series.Points.Clear(); seedingSeries.Points.Clear(); } } #endregion #endregion private List GetSolutionScopes() { return Content.Problem.Solutions.Select(x => x.Solution).OfType().ToList(); } private double? GetQuality(IScope scope, string qualityName) { IVariable v; if (!scope.Variables.TryGetValue(qualityName, out v)) return null; var dval = v.Value as Data.DoubleValue; if (dval == null) return null; return dval.Value; } } }