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.Globalization;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Core.Views;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.QuadraticAssignment.Views {
|
---|
33 | /// <summary>
|
---|
34 | /// The base class for visual representations of a path tour for a TSP.
|
---|
35 | /// </summary>
|
---|
36 | [View("QAPAssignment View")]
|
---|
37 | [Content(typeof(QAPAssignment), true)]
|
---|
38 | public sealed partial class QAPAssignmentView : ItemView {
|
---|
39 | public new QAPAssignment Content {
|
---|
40 | get { return (QAPAssignment)base.Content; }
|
---|
41 | set { base.Content = value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Initializes a new instance of <see cref="PathTSPTourView"/>.
|
---|
46 | /// </summary>
|
---|
47 | public QAPAssignmentView() {
|
---|
48 | InitializeComponent();
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected override void DeregisterContentEvents() {
|
---|
52 | Content.PropertyChanged -= new PropertyChangedEventHandler(Content_PropertyChanged);
|
---|
53 | base.DeregisterContentEvents();
|
---|
54 | }
|
---|
55 | protected override void RegisterContentEvents() {
|
---|
56 | base.RegisterContentEvents();
|
---|
57 | Content.PropertyChanged += new PropertyChangedEventHandler(Content_PropertyChanged);
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected override void OnContentChanged() {
|
---|
61 | base.OnContentChanged();
|
---|
62 | if (Content == null) {
|
---|
63 | qualityViewHost.Content = null;
|
---|
64 | pictureBox.Image = null;
|
---|
65 | assignmentViewHost.Content = null;
|
---|
66 | } else {
|
---|
67 | qualityViewHost.Content = Content.Quality;
|
---|
68 | GenerateImage();
|
---|
69 | assignmentViewHost.Content = Content.Assignment;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected override void SetEnabledStateOfControls() {
|
---|
74 | base.SetEnabledStateOfControls();
|
---|
75 | qualityGroupBox.Enabled = Content != null;
|
---|
76 | pictureBox.Enabled = Content != null;
|
---|
77 | assignmentGroupBox.Enabled = Content != null;
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void GenerateImage() {
|
---|
81 | if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
|
---|
82 | if (Content == null) {
|
---|
83 | pictureBox.Image = null;
|
---|
84 | } else {
|
---|
85 | bool drawDistances = false;
|
---|
86 | DoubleMatrix coordinates = Content.Coordinates;
|
---|
87 | DoubleMatrix distances = Content.Distances;
|
---|
88 | if ((coordinates == null || coordinates.Rows == 0)
|
---|
89 | && (distances == null || distances.Rows == 0)) return;
|
---|
90 | else if ((coordinates == null || coordinates.Rows == 0)
|
---|
91 | && Content.ViewCoordinates == null) {
|
---|
92 | coordinates = new DoubleMatrix(distances.Rows, 2);
|
---|
93 | int columns = (int)Math.Ceiling(Math.Sqrt(coordinates.Rows));
|
---|
94 | int x = 0, y = 0;
|
---|
95 | for (int i = 0; i < coordinates.Rows; i++) {
|
---|
96 | coordinates[i, 0] = 10 * x;
|
---|
97 | coordinates[i, 1] = 10 * y;
|
---|
98 | if (++x > columns) {
|
---|
99 | y++;
|
---|
100 | x = 0;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | Content.ViewCoordinates = coordinates;
|
---|
104 | drawDistances = true;
|
---|
105 | } else if ((coordinates == null || coordinates.Rows == 0)
|
---|
106 | && Content.ViewCoordinates != null) {
|
---|
107 | coordinates = Content.ViewCoordinates;
|
---|
108 | drawDistances = true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | DoubleMatrix weights = Content.Weights;
|
---|
112 | Permutation permutation = Content.Assignment;
|
---|
113 | Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
114 |
|
---|
115 | if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
|
---|
116 | double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
|
---|
117 | for (int i = 0; i < coordinates.Rows; i++) {
|
---|
118 | if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
|
---|
119 | if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
|
---|
120 | if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
|
---|
121 | if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
|
---|
122 | }
|
---|
123 |
|
---|
124 | int border = 20;
|
---|
125 | double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
|
---|
126 | double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
|
---|
127 |
|
---|
128 | Point[] points = new Point[coordinates.Rows];
|
---|
129 | for (int i = 0; i < coordinates.Rows; i++)
|
---|
130 | points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
|
---|
131 | bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
|
---|
132 |
|
---|
133 | using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
---|
134 | if ((permutation != null) && (permutation.Length == coordinates.Rows) && (permutation.Validate())) {
|
---|
135 | for (int i = 0; i < permutation.Length; i++) {
|
---|
136 | for (int j = 0; j < permutation.Length; j++) {
|
---|
137 | if (i != j && weights[i, j] > 0) {
|
---|
138 | graphics.DrawLine(Pens.Black, points[permutation[i]], points[permutation[j]]);
|
---|
139 | float midX = ((points[permutation[i]].X + points[permutation[j]].X) / 2.0f) + 5;
|
---|
140 | float midY = ((points[permutation[i]].Y + points[permutation[j]].Y) / 2.0f) - 5;
|
---|
141 | graphics.DrawString(weights[i, j].ToString(CultureInfo.InvariantCulture.NumberFormat), Font, Brushes.Black, midX, midY);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | for (int i = 0; i < points.Length; i++)
|
---|
147 | graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | pictureBox.Image = bitmap;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
156 | if (InvokeRequired)
|
---|
157 | Invoke(new PropertyChangedEventHandler(Content_PropertyChanged), sender, e);
|
---|
158 | else {
|
---|
159 | switch (e.PropertyName) {
|
---|
160 | case "Coordinates":
|
---|
161 | case "Distances":
|
---|
162 | case "Weights":
|
---|
163 | GenerateImage();
|
---|
164 | break;
|
---|
165 | case "Permutation":
|
---|
166 | GenerateImage();
|
---|
167 | assignmentViewHost.Content = Content.Assignment;
|
---|
168 | break;
|
---|
169 | case "Quality":
|
---|
170 | break;
|
---|
171 | default:
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | private void pictureBox_SizeChanged(object sender, EventArgs e) {
|
---|
178 | GenerateImage();
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|