#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 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.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using HeuristicLab.Common.Resources;
using HeuristicLab.Core;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.Problems.QuadraticAssignment.Views {
[View("Quadratic Assignment Problem View")]
[Content(typeof(QuadraticAssignmentProblem), IsDefaultView = true)]
public sealed partial class QuadraticAssignmentProblemView : ParameterizedNamedItemView {
public new QuadraticAssignmentProblem Content {
get { return (QuadraticAssignmentProblem)base.Content; }
set { base.Content = value; }
}
public QuadraticAssignmentProblemView() {
InitializeComponent();
importInstanceButton.Image = VSImageLibrary.Open;
reloadInstancesButton.Text = String.Empty;
reloadInstancesButton.Image = VSImageLibrary.Refresh;
loadInstanceButton.Image = VSImageLibrary.Internet;
Controls.Remove(parameterCollectionView);
parameterCollectionView.Dock = DockStyle.Fill;
problemTabPage.Controls.Add(parameterCollectionView);
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.DistancesParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
Content.WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
Content.BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
}
protected override void DeregisterContentEvents() {
Content.DistancesParameter.ValueChanged -= new EventHandler(DistanceMatrixParameter_ValueChanged);
Content.WeightsParameter.ValueChanged -= new EventHandler(WeightsParameter_ValueChanged);
Content.BestKnownSolutionParameter.ValueChanged -= new EventHandler(BestKnownSolutionParameter_ValueChanged);
base.DeregisterContentEvents();
}
private void DistanceMatrixParameter_ValueChanged(object sender, System.EventArgs e) {
qapView.Distances = Content.Distances;
}
private void WeightsParameter_ValueChanged(object sender, System.EventArgs e) {
qapView.Weights = Content.Weights;
}
private void BestKnownSolutionParameter_ValueChanged(object sender, System.EventArgs e) {
qapView.Assignment = Content.BestKnownSolution;
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content != null) {
qapView.Distances = Content.Distances;
qapView.Weights = Content.Weights;
qapView.Assignment = Content.BestKnownSolution;
} else {
qapView.Distances = null;
qapView.Weights = null;
qapView.Assignment = null;
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
reloadInstancesButton.Enabled = !ReadOnly && !Locked && Content != null;
instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null;
loadInstanceButton.Enabled = !ReadOnly && !Locked && Content != null && !String.IsNullOrEmpty((string)instancesComboBox.SelectedItem);
importInstanceButton.Enabled = !ReadOnly && !Locked && Content != null;
}
private void instancesComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
SetEnabledStateOfControls();
}
private void loadInstanceButton_Click(object sender, System.EventArgs e) {
ReadOnly = true;
string instanceStr = instancesComboBox.SelectedItem as string;
Task t = new Task((object name) => {
string instance = (string)name;
if (String.IsNullOrEmpty(instance)) return;
try {
using (QAPServiceReference.QAPClient client = new QAPServiceReference.QAPClient()) {
var data = client.GetProblemInstanceData(instance);
DoubleMatrix weights = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
DoubleMatrix distances = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
try {
for (int i = 0; i < data.Weights.Length; i++)
for (int j = 0; j < data.Weights.Length; j++) {
weights[i, j] = data.Weights[i][j];
distances[i, j] = data.Distances[i][j];
}
} catch (IndexOutOfRangeException) {
MessageBox.Show("The problem data is malformed, the problem could not be loaded.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Content.Name = data.Name;
Content.Description = data.Description;
Content.Maximization.Value = data.Maximization;
Content.Weights = weights;
Content.Distances = distances;
Content.BestKnownQualityParameter.Value = null;
Content.BestKnownSolution = null;
Content.BestKnownSolutions = new ItemSet();
var solutions = client.GetBestSolutionsData(instance);
if (solutions.Any()) {
Content.BestKnownQualityParameter.Value = new DoubleValue(solutions.First().Quality);
Content.BestKnownSolution = new Permutation(PermutationTypes.Absolute, solutions.First().Assignment);
foreach (var solution in solutions) {
Content.BestKnownSolutions.Add(new Permutation(PermutationTypes.Absolute, solution.Assignment));
}
}
}
} catch (Exception ex) {
PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
} finally {
ReadOnly = false;
}
}, instanceStr);
t.Start();
}
private void importInstanceButton_Click(object sender, System.EventArgs e) {
if (openFileDialog.ShowDialog() == DialogResult.OK) {
try {
Content.ImportFileInstance(openFileDialog.FileName);
} catch (Exception ex) {
PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
}
}
}
private void QAPLIBInstancesLabel_Click(object sender, System.EventArgs e) {
System.Diagnostics.Process.Start("http://www.seas.upenn.edu/qaplib/");
}
private void QAPLIBInstancesLabel_MouseEnter(object sender, EventArgs e) {
Cursor = Cursors.Hand;
QAPLIBInstancesLabel.ForeColor = Color.Red;
toolTip.SetToolTip(QAPLIBInstancesLabel, "Browse to http://www.seas.upenn.edu/qaplib/");
}
private void QAPLIBInstancesLabel_MouseLeave(object sender, EventArgs e) {
Cursor = Cursors.Default;
QAPLIBInstancesLabel.ForeColor = Color.Blue;
toolTip.SetToolTip(QAPLIBInstancesLabel, String.Empty);
}
private void reloadInstancesButton_Click(object sender, EventArgs e) {
ReadOnly = true;
System.Threading.Tasks.Task t = new System.Threading.Tasks.Task(() => {
try {
using (QAPServiceReference.QAPClient client = new QAPServiceReference.QAPClient()) {
instancesComboBox.Items.Clear();
foreach (string name in client.GetProblemInstances())
instancesComboBox.Items.Add(name);
if (instancesComboBox.Items.Count > 0)
instancesComboBox.SelectedIndex = 0;
}
} catch (Exception ex) {
PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
} finally {
ReadOnly = false;
}
});
t.Start();
}
}
}