#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.Common.Resources;
using HeuristicLab.Core;
using HeuristicLab.Core.Views;
using HeuristicLab.Data.Views;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
using HeuristicLab.OptimizationExpertSystem.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace HeuristicLab.OptimizationExpertSystem {
[View("Solver View")]
[Content(typeof(KnowledgeCenter), IsDefaultView = false)]
public partial class SolverView : KnowledgeCenterViewBase {
private EnumValueView seedingStrategyView;
private CheckedItemListView seedingSolutionsView;
protected virtual bool SuppressEvents { get; set; }
public SolverView() {
InitializeComponent();
algorithmStartButton.Text = string.Empty;
algorithmStartButton.Image = VSImageLibrary.Play;
algorithmCloneButton.Text = string.Empty;
algorithmCloneButton.Image = VSImageLibrary.Clone;
seedingStrategyView = new EnumValueView() {
Dock = DockStyle.Fill
};
seedingSolutionsView = new CheckedItemListView() {
Dock = DockStyle.Fill
};
seedingStrategyPanel.Controls.Add(seedingStrategyView);
solutionSeedingTabPage.Controls.Add(seedingSolutionsView);
}
protected override void OnContentChanged() {
base.OnContentChanged();
SuppressEvents = true;
try {
if (Content == null) {
maxEvaluationsView.Content = null;
solverParametersView.Content = null;
runsView.Content = null;
seededRunsView.Content = null;
seedingStrategyView.Content = null;
seedingSolutionsView.Content = null;
} else {
maxEvaluationsView.Content = Content.MaximumEvaluations;
runsView.Content = Content.InstanceRuns;
seededRunsView.Content = Content.SeededRuns;
seedingStrategyView.Content = Content.SeedingStrategy;
seedingSolutionsView.Content = Content.SolutionSeedingPool;
}
} finally { SuppressEvents = false; }
UpdateSuggestedInstancesCombobox();
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
suggestedInstancesComboBox.Enabled = Content != null && !ReadOnly && !Locked;
algorithmStartButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0;
algorithmCloneButton.Enabled = Content != null && !ReadOnly && !Locked && suggestedInstancesComboBox.SelectedIndex >= 0;
runsView.Enabled = Content != null;
}
#region Update Controls
private void UpdateSuggestedInstancesCombobox() {
var prevSelection = (AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem;
var prevNewIndex = -1;
suggestedInstancesComboBox.Items.Clear();
if (Content == null) return;
var ranking = Content.GetAlgorithmInstanceRanking().ToList();
for (var i = 0; i < ranking.Count; i++) {
suggestedInstancesComboBox.Items.Add(new AlgorithmInstanceItem(ranking[i]));
if (prevSelection == null || ranking[i].Key.Name == prevSelection.Item.Key.Name)
prevNewIndex = prevSelection == null ? 0 : i;
}
suggestedInstancesComboBox.SelectedIndex = prevNewIndex;
}
#endregion
#region Content Event Handlers
protected override void OnAlgorithmInstanceStarted(IAlgorithm algorithm) {
base.OnAlgorithmInstanceStarted(algorithm);
var form = new AlgorithmControlForm(algorithm, showOnlyFinalResultCheckBox.Checked);
form.Show(resultsDockPanel);
}
protected override void OnRecommendationModelChanged() {
base.OnRecommendationModelChanged();
UpdateSuggestedInstancesCombobox();
}
#endregion
#region Control Event Handlers
private void AlgorithmStartButtonOnClick(object sender, EventArgs e) {
if (suggestedInstancesComboBox.SelectedIndex >= 0)
Content.StartAlgorithmAsync(Content.AlgorithmInstances.Select((a, i) => new { Alg = a, Index = i}).Single(x => x.Alg.Name == ((AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem).Item.Key.Name).Index);
}
private void AlgorithmCloneButtonOnClick(object sender, EventArgs e) {
if (suggestedInstancesComboBox.SelectedIndex >= 0)
MainForm.ShowContent((IAlgorithm)Content.AlgorithmInstances[Content.AlgorithmInstances.Select((a, i) => new { Alg = a, Index = i }).Single(x => x.Alg.Name == ((AlgorithmInstanceItem)suggestedInstancesComboBox.SelectedItem).Item.Key.Name).Index].Clone());
}
private void SuggestedInstancesComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
if (InvokeRequired) { Invoke((Action