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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Common.Resources;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Core.Views;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
32 | using HeuristicLab.MainForm;
|
---|
33 | using HeuristicLab.MainForm.WindowsForms;
|
---|
34 | using HeuristicLab.Problems.QuadraticAssignment.QAPInstanceService;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.QuadraticAssignment.Views {
|
---|
37 | [View("Quadratic Assignment Problem View")]
|
---|
38 | [Content(typeof(QuadraticAssignmentProblem), IsDefaultView = true)]
|
---|
39 | public sealed partial class QuadraticAssignmentProblemView : ParameterizedNamedItemView {
|
---|
40 | public new QuadraticAssignmentProblem Content {
|
---|
41 | get { return (QuadraticAssignmentProblem)base.Content; }
|
---|
42 | set { base.Content = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public QuadraticAssignmentProblemView() {
|
---|
46 | InitializeComponent();
|
---|
47 | importInstanceButton.Image = VSImageLibrary.Open;
|
---|
48 | reloadInstancesButton.Text = String.Empty;
|
---|
49 | reloadInstancesButton.Image = VSImageLibrary.Refresh;
|
---|
50 | loadInstanceButton.Image = VSImageLibrary.Internet;
|
---|
51 | Controls.Remove(parameterCollectionView);
|
---|
52 | parameterCollectionView.Dock = DockStyle.Fill;
|
---|
53 | problemTabPage.Controls.Add(parameterCollectionView);
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected override void RegisterContentEvents() {
|
---|
57 | base.RegisterContentEvents();
|
---|
58 | Content.DistancesParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
|
---|
59 | Content.WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
|
---|
60 | Content.BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
|
---|
61 | Content.Instances.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
62 | Content.Instances.ItemsMoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
63 | Content.Instances.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
64 | Content.Instances.ItemsReplaced += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
65 | Content.Instances.CollectionReset += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void DeregisterContentEvents() {
|
---|
69 | Content.DistancesParameter.ValueChanged -= new EventHandler(DistanceMatrixParameter_ValueChanged);
|
---|
70 | Content.WeightsParameter.ValueChanged -= new EventHandler(WeightsParameter_ValueChanged);
|
---|
71 | Content.BestKnownSolutionParameter.ValueChanged -= new EventHandler(BestKnownSolutionParameter_ValueChanged);
|
---|
72 | Content.Instances.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
73 | Content.Instances.ItemsMoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
74 | Content.Instances.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
75 | Content.Instances.ItemsReplaced -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
76 | Content.Instances.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
|
---|
77 | base.DeregisterContentEvents();
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void Content_Instances_Changed(object sender, EventArgs e) {
|
---|
81 | instancesComboBox.Items.Clear();
|
---|
82 | foreach (string name in Content.Instances)
|
---|
83 | instancesComboBox.Items.Add(name);
|
---|
84 | if (instancesComboBox.Items.Count > 0)
|
---|
85 | instancesComboBox.SelectedIndex = 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void DistanceMatrixParameter_ValueChanged(object sender, System.EventArgs e) {
|
---|
89 | qapView.Distances = Content.Distances;
|
---|
90 | }
|
---|
91 |
|
---|
92 | private void WeightsParameter_ValueChanged(object sender, System.EventArgs e) {
|
---|
93 | qapView.Weights = Content.Weights;
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void BestKnownSolutionParameter_ValueChanged(object sender, System.EventArgs e) {
|
---|
97 | qapView.Assignment = Content.BestKnownSolution;
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected override void OnContentChanged() {
|
---|
101 | base.OnContentChanged();
|
---|
102 | if (Content != null) {
|
---|
103 | qapView.Distances = Content.Distances;
|
---|
104 | qapView.Weights = Content.Weights;
|
---|
105 | qapView.Assignment = Content.BestKnownSolution;
|
---|
106 | } else {
|
---|
107 | qapView.Distances = null;
|
---|
108 | qapView.Weights = null;
|
---|
109 | qapView.Assignment = null;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected override void SetEnabledStateOfControls() {
|
---|
114 | base.SetEnabledStateOfControls();
|
---|
115 | reloadInstancesButton.Enabled = !ReadOnly && !Locked && Content != null;
|
---|
116 | instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null;
|
---|
117 | loadInstanceButton.Enabled = !ReadOnly && !Locked && Content != null && !String.IsNullOrEmpty((string)instancesComboBox.SelectedItem);
|
---|
118 | importInstanceButton.Enabled = !ReadOnly && !Locked && Content != null;
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void instancesComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
|
---|
122 | SetEnabledStateOfControls();
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void loadInstanceButton_Click(object sender, System.EventArgs e) {
|
---|
126 | ReadOnly = true;
|
---|
127 | string instanceStr = instancesComboBox.SelectedItem as string;
|
---|
128 | progressBar.Visible = true;
|
---|
129 | loadInstanceWorker.RunWorkerAsync(instanceStr);
|
---|
130 | }
|
---|
131 |
|
---|
132 | private void importFileInstanceButton_Click(object sender, System.EventArgs e) {
|
---|
133 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
134 | try {
|
---|
135 | Content.LoadInstanceFromFile(openFileDialog.FileName);
|
---|
136 | } catch (Exception ex) {
|
---|
137 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | private void reloadInstancesButton_Click(object sender, EventArgs e) {
|
---|
143 | ReadOnly = true;
|
---|
144 | progressBar.Visible = true;
|
---|
145 | getInstancesWorker.RunWorkerAsync();
|
---|
146 | }
|
---|
147 |
|
---|
148 | private void loadInstanceWorker_DoWork(object sender, DoWorkEventArgs e) {
|
---|
149 | string instance = (string)e.Argument;
|
---|
150 | if (String.IsNullOrEmpty(instance)) return;
|
---|
151 | using (var client = new QAPClient()) {
|
---|
152 | loadInstanceWorker.ReportProgress(10);
|
---|
153 | var data = client.GetProblemInstanceData(instance);
|
---|
154 | loadInstanceWorker.ReportProgress(60);
|
---|
155 | DoubleMatrix weights = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
|
---|
156 | DoubleMatrix distances = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
|
---|
157 | try {
|
---|
158 | for (int i = 0; i < data.Weights.Length; i++)
|
---|
159 | for (int j = 0; j < data.Weights.Length; j++) {
|
---|
160 | weights[i, j] = data.Weights[i][j];
|
---|
161 | distances[i, j] = data.Distances[i][j];
|
---|
162 | }
|
---|
163 | } catch (IndexOutOfRangeException) {
|
---|
164 | MessageBox.Show("The problem data is malformed, the problem could not be loaded.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
165 | }
|
---|
166 | loadInstanceWorker.ReportProgress(65);
|
---|
167 | Content.Name = data.Name;
|
---|
168 | Content.Description = data.Description;
|
---|
169 | Content.Maximization.Value = data.Maximization;
|
---|
170 | Content.Weights = weights;
|
---|
171 | Content.Distances = distances;
|
---|
172 |
|
---|
173 | Content.BestKnownQualityParameter.Value = null;
|
---|
174 | Content.BestKnownSolution = null;
|
---|
175 | Content.BestKnownSolutions = new ItemSet<Permutation>();
|
---|
176 | var solutions = client.GetBestSolutionsData(instance);
|
---|
177 | loadInstanceWorker.ReportProgress(90);
|
---|
178 | if (solutions.Any()) {
|
---|
179 | Content.BestKnownQualityParameter.Value = new DoubleValue(solutions.First().Quality);
|
---|
180 | Content.BestKnownSolution = new Permutation(PermutationTypes.Absolute, solutions.First().Assignment);
|
---|
181 | foreach (var solution in solutions) {
|
---|
182 | Content.BestKnownSolutions.Add(new Permutation(PermutationTypes.Absolute, solution.Assignment));
|
---|
183 | }
|
---|
184 | }
|
---|
185 | loadInstanceWorker.ReportProgress(100);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | private void getInstancesWorker_DoWork(object sender, DoWorkEventArgs e) {
|
---|
190 | using (var client = new QAPClient()) {
|
---|
191 | getInstancesWorker.ReportProgress(10);
|
---|
192 | var instances = client.GetProblemInstances();
|
---|
193 | getInstancesWorker.ReportProgress(85);
|
---|
194 | Content.Instances.Replace(instances);
|
---|
195 | getInstancesWorker.ReportProgress(100);
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
|
---|
200 | progressBar.Value = e.ProgressPercentage;
|
---|
201 | }
|
---|
202 |
|
---|
203 | private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
204 | progressBar.Visible = false;
|
---|
205 | ReadOnly = false;
|
---|
206 | if (e.Error != null)
|
---|
207 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(e.Error);
|
---|
208 | }
|
---|
209 |
|
---|
210 | private void QAPLIBInstancesLabel_Click(object sender, System.EventArgs e) {
|
---|
211 | System.Diagnostics.Process.Start("http://www.seas.upenn.edu/qaplib/");
|
---|
212 | }
|
---|
213 |
|
---|
214 | private void QAPLIBInstancesLabel_MouseEnter(object sender, EventArgs e) {
|
---|
215 | Cursor = Cursors.Hand;
|
---|
216 | QAPLIBInstancesLabel.ForeColor = Color.Red;
|
---|
217 | toolTip.SetToolTip(QAPLIBInstancesLabel, "Browse to http://www.seas.upenn.edu/qaplib/");
|
---|
218 | }
|
---|
219 |
|
---|
220 | private void QAPLIBInstancesLabel_MouseLeave(object sender, EventArgs e) {
|
---|
221 | Cursor = Cursors.Default;
|
---|
222 | QAPLIBInstancesLabel.ForeColor = Color.Blue;
|
---|
223 | toolTip.SetToolTip(QAPLIBInstancesLabel, String.Empty);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | } |
---|