#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 System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HeuristicLab.Analysis;
using HeuristicLab.Common.Resources;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
using HeuristicLab.OptimizationExpertSystem.Common;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.OptimizationExpertSystem {
[View("Performance Modeling View")]
[Content(typeof(KnowledgeCenter), IsDefaultView = false)]
public partial class PerformanceModelingView : KnowledgeCenterViewBase {
private readonly CheckedItemList characteristics;
public PerformanceModelingView() {
InitializeComponent();
characteristics = new CheckedItemList();
characteristics.CheckedItemsChanged += CharacteristicsOnCheckedItemsChanged;
characteristicsViewHost.Content = characteristics;
recommendStartButton.Text = string.Empty;
recommendStartButton.Image = VSImageLibrary.Play;
refreshCharacteristicsButton.Text = string.Empty;
refreshCharacteristicsButton.Image = VSImageLibrary.Refresh;
UpdateRecommenderCombobox();
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
minTargetView.Content = null;
} else {
minTargetView.Content = Content.MinimumTarget;
}
UpdateCharacteristics();
UpdateTopNCombobox();
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
// TODO: up/download state
recommendStartButton.Enabled = Content != null && recommenderComboBox.SelectedIndex >= 0 && characteristics.CheckedItems.Any();
characteristicsViewHost.Enabled = Content != null;
xValidateButton.Enabled = Content != null && recommenderComboBox.SelectedIndex >= 0 && characteristics.CheckedItems.Any() && topNComboBox.SelectedIndex >= 0;
}
#region Update Controls
private void UpdateRecommenderCombobox() {
if (InvokeRequired) { Invoke((Action)UpdateRecommenderCombobox); return; }
var prevSelection = (IAlgorithmInstanceRecommender)recommenderComboBox.SelectedItem;
var prevNewIndex = -1;
recommenderComboBox.Items.Clear();
var i = 0;
foreach (var rcmd in ApplicationManager.Manager.GetInstances()) {
recommenderComboBox.Items.Add(rcmd);
if (prevSelection == null || rcmd.GetType() == prevSelection.GetType())
prevNewIndex = prevSelection == null ? 0 : i;
i++;
}
recommenderComboBox.SelectedIndex = prevNewIndex;
}
private void UpdateCharacteristics() {
if (InvokeRequired) { Invoke((Action)UpdateCharacteristics); return; }
var @checked = new HashSet(characteristics.CheckedItems.Select(x => x.Value.Value));
if (@checked.Count == 0 && Content.ProblemInstances.ResultNames.Any(x => x.StartsWith("Characteristic.")))
@checked = new HashSet(Content.ProblemInstances.ResultNames.Where(x => x.StartsWith("Characteristic.")));
characteristics.Clear();
if (Content == null || Content.ProblemInstances.Count == 0) return;
foreach (var c in Content.ProblemInstances.ResultNames) {
characteristics.Add(new StringValue(c), @checked.Contains(c));
}
}
private void UpdateTopNCombobox() {
if (InvokeRequired) { Invoke((Action)UpdateTopNCombobox); return; }
int selected = 3;
if (topNComboBox.SelectedIndex >= 0) selected = (int)topNComboBox.SelectedItem;
topNComboBox.Items.Clear();
if (Content == null) return;
var algInstances = Content.AlgorithmInstances.Count;
for (var i = 1; i <= algInstances; i++) {
topNComboBox.Items.Add(i);
}
topNComboBox.SelectedIndex = Math.Min(selected, topNComboBox.Items.Count) - 1;
}
#endregion
#region Content Event Handlers
protected override void OnProblemInstancesChanged() {
base.OnProblemInstancesChanged();
UpdateCharacteristics();
SetEnabledStateOfControls();
}
protected override void OnAlgorithmInstancesChanged() {
base.OnAlgorithmInstancesChanged();
UpdateTopNCombobox();
}
#endregion
#region Control Event Handlers
private void RecommenderComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
if (InvokeRequired) { Invoke((Action