#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.MainForm;
namespace HeuristicLab.Problems.QuadraticAssignment.Views {
///
/// The base class for visual representations of a path tour for a TSP.
///
[View("QAPAssignment View")]
[Content(typeof(QAPAssignment), true)]
public sealed partial class QAPAssignmentView : ItemView {
public new QAPAssignment Content {
get { return (QAPAssignment)base.Content; }
set { base.Content = value; }
}
///
/// Initializes a new instance of .
///
public QAPAssignmentView() {
InitializeComponent();
}
protected override void DeregisterContentEvents() {
Content.PropertyChanged -= new PropertyChangedEventHandler(Content_PropertyChanged);
base.DeregisterContentEvents();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.PropertyChanged += new PropertyChangedEventHandler(Content_PropertyChanged);
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
qualityViewHost.Content = null;
pictureBox.Image = null;
assignmentViewHost.Content = null;
} else {
qualityViewHost.Content = Content.Quality;
GenerateImage();
assignmentViewHost.Content = Content.Assignment;
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
qualityGroupBox.Enabled = Content != null;
pictureBox.Enabled = Content != null;
assignmentGroupBox.Enabled = Content != null;
}
private void GenerateImage() {
if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
if (Content == null) {
pictureBox.Image = null;
} else {
bool drawDistances = false;
DoubleMatrix coordinates = Content.Coordinates;
DoubleMatrix distances = Content.Distances;
if ((coordinates == null || coordinates.Rows == 0)
&& (distances == null || distances.Rows == 0)) return;
else if ((coordinates == null || coordinates.Rows == 0)
&& Content.ViewCoordinates == null) {
coordinates = new DoubleMatrix(distances.Rows, 2);
int columns = (int)Math.Ceiling(Math.Sqrt(coordinates.Rows));
int x = 0, y = 0;
for (int i = 0; i < coordinates.Rows; i++) {
coordinates[i, 0] = 10 * x;
coordinates[i, 1] = 10 * y;
if (++x > columns) {
y++;
x = 0;
}
}
Content.ViewCoordinates = coordinates;
drawDistances = true;
} else if ((coordinates == null || coordinates.Rows == 0)
&& Content.ViewCoordinates != null) {
coordinates = Content.ViewCoordinates;
drawDistances = true;
}
DoubleMatrix weights = Content.Weights;
Permutation permutation = Content.Assignment;
Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
for (int i = 0; i < coordinates.Rows; i++) {
if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
}
int border = 20;
double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
Point[] points = new Point[coordinates.Rows];
for (int i = 0; i < coordinates.Rows; i++)
points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
using (Graphics graphics = Graphics.FromImage(bitmap)) {
if ((permutation != null) && (permutation.Length == coordinates.Rows) && (permutation.Validate())) {
for (int i = 0; i < permutation.Length; i++) {
for (int j = 0; j < permutation.Length; j++) {
if (i != j && weights[i, j] > 0) {
graphics.DrawLine(Pens.Black, points[permutation[i]], points[permutation[j]]);
float midX = ((points[permutation[i]].X + points[permutation[j]].X) / 2.0f) + 5;
float midY = ((points[permutation[i]].Y + points[permutation[j]].Y) / 2.0f) - 5;
graphics.DrawString(weights[i, j].ToString(CultureInfo.InvariantCulture.NumberFormat), Font, Brushes.Black, midX, midY);
}
}
}
}
for (int i = 0; i < points.Length; i++)
graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
}
}
pictureBox.Image = bitmap;
}
}
}
private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (InvokeRequired)
Invoke(new PropertyChangedEventHandler(Content_PropertyChanged), sender, e);
else {
switch (e.PropertyName) {
case "Coordinates":
case "Distances":
case "Weights":
GenerateImage();
break;
case "Permutation":
GenerateImage();
assignmentViewHost.Content = Content.Assignment;
break;
case "Quality":
break;
default:
break;
}
}
}
private void pictureBox_SizeChanged(object sender, EventArgs e) {
GenerateImage();
}
}
}