Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.QuadraticAssignment.Views/3.3/QuadraticAssignmentProblemView.cs @ 6733

Last change on this file since 6733 was 6733, checked in by abeham, 13 years ago

#1619

  • Updated the model slightly
  • Deployed the service
  • Updated the GUI to perform asynchronous calls to the service
File size: 8.4 KB
Line 
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
22using System;
23using System.Drawing;
24using System.Linq;
25using System.Threading.Tasks;
26using System.Windows.Forms;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core;
29using HeuristicLab.Core.Views;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.PermutationEncoding;
32using HeuristicLab.MainForm;
33using HeuristicLab.MainForm.WindowsForms;
34
35namespace 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;
47      reloadInstancesButton.Text = String.Empty;
48      reloadInstancesButton.Image = VSImageLibrary.Refresh;
49      loadInstanceButton.Image = VSImageLibrary.Internet;
50      Controls.Remove(parameterCollectionView);
51      parameterCollectionView.Dock = DockStyle.Fill;
52      problemTabPage.Controls.Add(parameterCollectionView);
53    }
54
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.DistancesParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
58      Content.WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
59      Content.BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
60    }
61
62    protected override void DeregisterContentEvents() {
63      Content.DistancesParameter.ValueChanged -= new EventHandler(DistanceMatrixParameter_ValueChanged);
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) {
70      qapView.Distances = Content.Distances;
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
81    protected override void OnContentChanged() {
82      base.OnContentChanged();
83      if (Content != null) {
84        qapView.Distances = Content.Distances;
85        qapView.Weights = Content.Weights;
86        qapView.Assignment = Content.BestKnownSolution;
87      } else {
88        qapView.Distances = null;
89        qapView.Weights = null;
90        qapView.Assignment = null;
91      }
92    }
93
94    protected override void SetEnabledStateOfControls() {
95      base.SetEnabledStateOfControls();
96      reloadInstancesButton.Enabled = !ReadOnly && !Locked && Content != null;
97      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null;
98      loadInstanceButton.Enabled = !ReadOnly && !Locked && Content != null && !String.IsNullOrEmpty((string)instancesComboBox.SelectedItem);
99      importInstanceButton.Enabled = !ReadOnly && !Locked && Content != null;
100    }
101
102    private void instancesComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
103      SetEnabledStateOfControls();
104    }
105
106    private void loadInstanceButton_Click(object sender, System.EventArgs e) {
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));
141              }
142            }
143          }
144        } catch (Exception ex) {
145          PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
146        } finally {
147          ReadOnly = false;
148        }
149      }, instanceStr);
150      t.Start();
151    }
152
153    private void importInstanceButton_Click(object sender, System.EventArgs e) {
154      if (openFileDialog.ShowDialog() == DialogResult.OK) {
155        try {
156          Content.ImportFileInstance(openFileDialog.FileName);
157        } catch (Exception ex) {
158          PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
159        }
160      }
161    }
162
163    private void QAPLIBInstancesLabel_Click(object sender, System.EventArgs e) {
164      System.Diagnostics.Process.Start("http://www.seas.upenn.edu/qaplib/");
165    }
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    }
178
179    private void reloadInstancesButton_Click(object sender, EventArgs e) {
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;
194        }
195      });
196      t.Start();
197    }
198  }
199}
Note: See TracBrowser for help on using the repository browser.