Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1619

  • experimented with a first locally working version
File size: 7.1 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.Windows.Forms;
25using HeuristicLab.Common.Resources;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30
31namespace HeuristicLab.Problems.QuadraticAssignment.Views {
32  [View("Quadratic Assignment Problem View")]
33  [Content(typeof(QuadraticAssignmentProblem), IsDefaultView = true)]
34  public sealed partial class QuadraticAssignmentProblemView : ParameterizedNamedItemView {
35    public new QuadraticAssignmentProblem Content {
36      get { return (QuadraticAssignmentProblem)base.Content; }
37      set { base.Content = value; }
38    }
39
40    public QuadraticAssignmentProblemView() {
41      InitializeComponent();
42      importInstanceButton.Image = VSImageLibrary.Open;
43      reloadInstancesButton.Text = String.Empty;
44      reloadInstancesButton.Image = VSImageLibrary.Refresh;
45      loadInstanceButton.Image = VSImageLibrary.Internet;
46      Controls.Remove(parameterCollectionView);
47      parameterCollectionView.Dock = DockStyle.Fill;
48      problemTabPage.Controls.Add(parameterCollectionView);
49    }
50
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.DistancesParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
54      Content.WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
55      Content.BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
56    }
57
58    protected override void DeregisterContentEvents() {
59      Content.DistancesParameter.ValueChanged -= new EventHandler(DistanceMatrixParameter_ValueChanged);
60      Content.WeightsParameter.ValueChanged -= new EventHandler(WeightsParameter_ValueChanged);
61      Content.BestKnownSolutionParameter.ValueChanged -= new EventHandler(BestKnownSolutionParameter_ValueChanged);
62      base.DeregisterContentEvents();
63    }
64
65    private void DistanceMatrixParameter_ValueChanged(object sender, System.EventArgs e) {
66      qapView.Distances = Content.Distances;
67    }
68
69    private void WeightsParameter_ValueChanged(object sender, System.EventArgs e) {
70      qapView.Weights = Content.Weights;
71    }
72
73    private void BestKnownSolutionParameter_ValueChanged(object sender, System.EventArgs e) {
74      qapView.Assignment = Content.BestKnownSolution;
75    }
76
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      instancesComboBox.Items.Clear();
80      if (Content != null) {
81        foreach (string instance in Content.EmbeddedInstances) {
82          instancesComboBox.Items.Add(instance);
83        }
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      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null;
97      loadInstanceButton.Enabled = !ReadOnly && !Locked && Content != null && instancesComboBox.SelectedItem != null;
98      importInstanceButton.Enabled = !ReadOnly && !Locked && Content != null;
99    }
100
101    private void instancesComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
102      loadInstanceButton.Enabled = instancesComboBox.SelectedItem != null;
103    }
104
105    private void loadInstanceButton_Click(object sender, System.EventArgs e) {
106      string instance = instancesComboBox.SelectedItem as string;
107      try {
108        //Content.LoadEmbeddedInstance(instance);
109        using (QAPServiceReference.QAPClient client = new QAPServiceReference.QAPClient()) {
110          var data = client.GetProblemInstanceData(instance);
111          DoubleMatrix weights = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
112          DoubleMatrix distances = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
113          try {
114            for (int i = 0; i < data.Weights.Length; i++)
115              for (int j = 0; j < data.Weights.Length; j++) {
116                weights[i, j] = data.Weights[i][j];
117                distances[i, j] = data.Distances[i][j];
118              }
119          } catch (IndexOutOfRangeException) {
120            MessageBox.Show("The problem data is malformed, the problem could not be loaded.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
121          }
122          Content.Name = data.Name;
123          Content.Description = data.Description;
124          Content.Maximization.Value = data.Maximization;
125          Content.Weights = weights;
126          Content.Distances = distances;
127        }
128      } catch (Exception ex) {
129        PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
130      }
131    }
132
133    private void importInstanceButton_Click(object sender, System.EventArgs e) {
134      if (openFileDialog.ShowDialog() == DialogResult.OK) {
135        try {
136          Content.ImportFileInstance(openFileDialog.FileName);
137        } catch (Exception ex) {
138          PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
139        }
140      }
141    }
142
143    private void QAPLIBInstancesLabel_Click(object sender, System.EventArgs e) {
144      System.Diagnostics.Process.Start("http://www.seas.upenn.edu/qaplib/");
145    }
146
147    private void QAPLIBInstancesLabel_MouseEnter(object sender, EventArgs e) {
148      Cursor = Cursors.Hand;
149      QAPLIBInstancesLabel.ForeColor = Color.Red;
150      toolTip.SetToolTip(QAPLIBInstancesLabel, "Browse to http://www.seas.upenn.edu/qaplib/");
151    }
152
153    private void QAPLIBInstancesLabel_MouseLeave(object sender, EventArgs e) {
154      Cursor = Cursors.Default;
155      QAPLIBInstancesLabel.ForeColor = Color.Blue;
156      toolTip.SetToolTip(QAPLIBInstancesLabel, String.Empty);
157    }
158
159    private void reloadInstancesButton_Click(object sender, EventArgs e) {
160      try {
161        using (QAPServiceReference.QAPClient client = new QAPServiceReference.QAPClient()) {
162          instancesComboBox.Items.Clear();
163          foreach (string name in client.GetProblemInstances())
164            instancesComboBox.Items.Add(name);
165        }
166      } catch (Exception ex) {
167        PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
168      }
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.