#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