[5563] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[5641] | 22 | using System;
|
---|
[6342] | 23 | using System.Drawing;
|
---|
[6660] | 24 | using System.Linq;
|
---|
[6733] | 25 | using System.Threading.Tasks;
|
---|
[5563] | 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Common.Resources;
|
---|
[6660] | 28 | using HeuristicLab.Core;
|
---|
[5563] | 29 | using HeuristicLab.Core.Views;
|
---|
[6659] | 30 | using HeuristicLab.Data;
|
---|
[6660] | 31 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[5563] | 32 | using HeuristicLab.MainForm;
|
---|
| 33 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Problems.QuadraticAssignment.Views {
|
---|
| 36 | [View("Quadratic Assignment Problem View")]
|
---|
| 37 | [Content(typeof(QuadraticAssignmentProblem), IsDefaultView = true)]
|
---|
| 38 | public sealed partial class QuadraticAssignmentProblemView : ParameterizedNamedItemView {
|
---|
| 39 | public new QuadraticAssignmentProblem Content {
|
---|
| 40 | get { return (QuadraticAssignmentProblem)base.Content; }
|
---|
| 41 | set { base.Content = value; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public QuadraticAssignmentProblemView() {
|
---|
| 45 | InitializeComponent();
|
---|
| 46 | importInstanceButton.Image = VSImageLibrary.Open;
|
---|
[6659] | 47 | reloadInstancesButton.Text = String.Empty;
|
---|
| 48 | reloadInstancesButton.Image = VSImageLibrary.Refresh;
|
---|
| 49 | loadInstanceButton.Image = VSImageLibrary.Internet;
|
---|
[5641] | 50 | Controls.Remove(parameterCollectionView);
|
---|
| 51 | parameterCollectionView.Dock = DockStyle.Fill;
|
---|
| 52 | problemTabPage.Controls.Add(parameterCollectionView);
|
---|
[5563] | 53 | }
|
---|
| 54 |
|
---|
[5641] | 55 | protected override void RegisterContentEvents() {
|
---|
| 56 | base.RegisterContentEvents();
|
---|
[5838] | 57 | Content.DistancesParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
|
---|
[5641] | 58 | Content.WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
|
---|
| 59 | Content.BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | protected override void DeregisterContentEvents() {
|
---|
[5838] | 63 | Content.DistancesParameter.ValueChanged -= new EventHandler(DistanceMatrixParameter_ValueChanged);
|
---|
[5641] | 64 | Content.WeightsParameter.ValueChanged -= new EventHandler(WeightsParameter_ValueChanged);
|
---|
| 65 | Content.BestKnownSolutionParameter.ValueChanged -= new EventHandler(BestKnownSolutionParameter_ValueChanged);
|
---|
| 66 | base.DeregisterContentEvents();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | private void DistanceMatrixParameter_ValueChanged(object sender, System.EventArgs e) {
|
---|
[5838] | 70 | qapView.Distances = Content.Distances;
|
---|
[5641] | 71 | }
|
---|
| 72 |
|
---|
| 73 | private void WeightsParameter_ValueChanged(object sender, System.EventArgs e) {
|
---|
| 74 | qapView.Weights = Content.Weights;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private void BestKnownSolutionParameter_ValueChanged(object sender, System.EventArgs e) {
|
---|
| 78 | qapView.Assignment = Content.BestKnownSolution;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[5563] | 81 | protected override void OnContentChanged() {
|
---|
| 82 | base.OnContentChanged();
|
---|
| 83 | if (Content != null) {
|
---|
[5838] | 84 | qapView.Distances = Content.Distances;
|
---|
[5641] | 85 | qapView.Weights = Content.Weights;
|
---|
| 86 | qapView.Assignment = Content.BestKnownSolution;
|
---|
| 87 | } else {
|
---|
| 88 | qapView.Distances = null;
|
---|
| 89 | qapView.Weights = null;
|
---|
| 90 | qapView.Assignment = null;
|
---|
[5563] | 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | protected override void SetEnabledStateOfControls() {
|
---|
| 95 | base.SetEnabledStateOfControls();
|
---|
[6733] | 96 | reloadInstancesButton.Enabled = !ReadOnly && !Locked && Content != null;
|
---|
[5563] | 97 | instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null;
|
---|
[6733] | 98 | loadInstanceButton.Enabled = !ReadOnly && !Locked && Content != null && !String.IsNullOrEmpty((string)instancesComboBox.SelectedItem);
|
---|
[5563] | 99 | importInstanceButton.Enabled = !ReadOnly && !Locked && Content != null;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | private void instancesComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
|
---|
[6733] | 103 | SetEnabledStateOfControls();
|
---|
[5563] | 104 | }
|
---|
| 105 |
|
---|
| 106 | private void loadInstanceButton_Click(object sender, System.EventArgs e) {
|
---|
[6733] | 107 | ReadOnly = true;
|
---|
| 108 | string instanceStr = instancesComboBox.SelectedItem as string;
|
---|
| 109 | Task t = new Task((object name) => {
|
---|
| 110 | string instance = (string)name;
|
---|
| 111 | if (String.IsNullOrEmpty(instance)) return;
|
---|
| 112 | try {
|
---|
| 113 | using (QAPServiceReference.QAPClient client = new QAPServiceReference.QAPClient()) {
|
---|
| 114 | var data = client.GetProblemInstanceData(instance);
|
---|
| 115 | DoubleMatrix weights = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
|
---|
| 116 | DoubleMatrix distances = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
|
---|
| 117 | try {
|
---|
| 118 | for (int i = 0; i < data.Weights.Length; i++)
|
---|
| 119 | for (int j = 0; j < data.Weights.Length; j++) {
|
---|
| 120 | weights[i, j] = data.Weights[i][j];
|
---|
| 121 | distances[i, j] = data.Distances[i][j];
|
---|
| 122 | }
|
---|
| 123 | } catch (IndexOutOfRangeException) {
|
---|
| 124 | MessageBox.Show("The problem data is malformed, the problem could not be loaded.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 125 | }
|
---|
| 126 | Content.Name = data.Name;
|
---|
| 127 | Content.Description = data.Description;
|
---|
| 128 | Content.Maximization.Value = data.Maximization;
|
---|
| 129 | Content.Weights = weights;
|
---|
| 130 | Content.Distances = distances;
|
---|
| 131 |
|
---|
| 132 | Content.BestKnownQualityParameter.Value = null;
|
---|
| 133 | Content.BestKnownSolution = null;
|
---|
| 134 | Content.BestKnownSolutions = new ItemSet<Permutation>();
|
---|
| 135 | var solutions = client.GetBestSolutionsData(instance);
|
---|
| 136 | if (solutions.Any()) {
|
---|
| 137 | Content.BestKnownQualityParameter.Value = new DoubleValue(solutions.First().Quality);
|
---|
| 138 | Content.BestKnownSolution = new Permutation(PermutationTypes.Absolute, solutions.First().Assignment);
|
---|
| 139 | foreach (var solution in solutions) {
|
---|
| 140 | Content.BestKnownSolutions.Add(new Permutation(PermutationTypes.Absolute, solution.Assignment));
|
---|
[6659] | 141 | }
|
---|
[6660] | 142 | }
|
---|
| 143 | }
|
---|
[6733] | 144 | } catch (Exception ex) {
|
---|
| 145 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
|
---|
| 146 | } finally {
|
---|
| 147 | ReadOnly = false;
|
---|
[6659] | 148 | }
|
---|
[6733] | 149 | }, instanceStr);
|
---|
| 150 | t.Start();
|
---|
[5563] | 151 | }
|
---|
| 152 |
|
---|
| 153 | private void importInstanceButton_Click(object sender, System.EventArgs e) {
|
---|
| 154 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
[5641] | 155 | try {
|
---|
| 156 | Content.ImportFileInstance(openFileDialog.FileName);
|
---|
| 157 | } catch (Exception ex) {
|
---|
| 158 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
|
---|
| 159 | }
|
---|
[5563] | 160 | }
|
---|
| 161 | }
|
---|
[5583] | 162 |
|
---|
| 163 | private void QAPLIBInstancesLabel_Click(object sender, System.EventArgs e) {
|
---|
| 164 | System.Diagnostics.Process.Start("http://www.seas.upenn.edu/qaplib/");
|
---|
| 165 | }
|
---|
[6342] | 166 |
|
---|
| 167 | private void QAPLIBInstancesLabel_MouseEnter(object sender, EventArgs e) {
|
---|
| 168 | Cursor = Cursors.Hand;
|
---|
| 169 | QAPLIBInstancesLabel.ForeColor = Color.Red;
|
---|
| 170 | toolTip.SetToolTip(QAPLIBInstancesLabel, "Browse to http://www.seas.upenn.edu/qaplib/");
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | private void QAPLIBInstancesLabel_MouseLeave(object sender, EventArgs e) {
|
---|
| 174 | Cursor = Cursors.Default;
|
---|
| 175 | QAPLIBInstancesLabel.ForeColor = Color.Blue;
|
---|
| 176 | toolTip.SetToolTip(QAPLIBInstancesLabel, String.Empty);
|
---|
| 177 | }
|
---|
[6659] | 178 |
|
---|
| 179 | private void reloadInstancesButton_Click(object sender, EventArgs e) {
|
---|
[6733] | 180 | ReadOnly = true;
|
---|
| 181 | System.Threading.Tasks.Task t = new System.Threading.Tasks.Task(() => {
|
---|
| 182 | try {
|
---|
| 183 | using (QAPServiceReference.QAPClient client = new QAPServiceReference.QAPClient()) {
|
---|
| 184 | instancesComboBox.Items.Clear();
|
---|
| 185 | foreach (string name in client.GetProblemInstances())
|
---|
| 186 | instancesComboBox.Items.Add(name);
|
---|
| 187 | if (instancesComboBox.Items.Count > 0)
|
---|
| 188 | instancesComboBox.SelectedIndex = 0;
|
---|
| 189 | }
|
---|
| 190 | } catch (Exception ex) {
|
---|
| 191 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
|
---|
| 192 | } finally {
|
---|
| 193 | ReadOnly = false;
|
---|
[6659] | 194 | }
|
---|
[6733] | 195 | });
|
---|
| 196 | t.Start();
|
---|
[6659] | 197 | }
|
---|
[5563] | 198 | }
|
---|
| 199 | }
|
---|