[17525] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 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.Drawing;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Problems.TravelingSalesman.Views;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.Orienteering.Views {
|
---|
| 28 | public class OrienteeringVisualizer : TSPVisualizer {
|
---|
| 29 | public IOrienteeringProblemData Data { get; set; }
|
---|
| 30 | public bool IsFeasible { get; set; }
|
---|
| 31 |
|
---|
| 32 | public override Bitmap Draw(int width, int height) {
|
---|
| 33 | var coordinates = Coordinates;
|
---|
| 34 | var integerVector = Tour;
|
---|
| 35 | var bitmap = new Bitmap(width, height);
|
---|
| 36 |
|
---|
| 37 | if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
|
---|
[17533] | 38 | && (coordinates.Rows == Data.Cities)) {
|
---|
[17525] | 39 | double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
|
---|
| 40 | for (int i = 0; i < coordinates.Rows; i++) {
|
---|
| 41 | if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
|
---|
| 42 | if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
|
---|
| 43 | if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
|
---|
| 44 | if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | int border = 20;
|
---|
| 48 | double xStep = xMax != xMin ? (width - 2 * border) / (xMax - xMin) : 1;
|
---|
| 49 | double yStep = yMax != yMin ? (height - 2 * border) / (yMax - yMin) : 1;
|
---|
| 50 |
|
---|
| 51 | Point[] points = new Point[coordinates.Rows];
|
---|
| 52 | for (int i = 0; i < coordinates.Rows; i++)
|
---|
| 53 | points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
|
---|
| 54 | bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
|
---|
| 55 |
|
---|
| 56 | using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
---|
| 57 | if (integerVector != null && integerVector.Length > 1) {
|
---|
| 58 | Point[] tour = new Point[integerVector.Length];
|
---|
| 59 | for (int i = 0; i < integerVector.Length; i++) {
|
---|
| 60 | tour[i] = points[integerVector[i]];
|
---|
| 61 | }
|
---|
| 62 | bool visualizePenalty = !IsFeasible;
|
---|
| 63 | graphics.DrawLines(visualizePenalty ? Pens.Red : Pens.Black, tour);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | double scoreMin = double.MaxValue;
|
---|
| 67 | double scoreMax = double.MinValue;
|
---|
| 68 | for (int i = 0; i < points.Length; i++) {
|
---|
| 69 | var score = Data.GetScore(i);
|
---|
| 70 | if (score < scoreMin) scoreMin = score;
|
---|
| 71 | if (score > scoreMax) scoreMax = score;
|
---|
| 72 | }
|
---|
| 73 | double scoreRange = scoreMax - scoreMin;
|
---|
| 74 | for (int i = 0; i < points.Length; i++) {
|
---|
| 75 | double score = Data.GetScore(i);
|
---|
| 76 | int size = scoreRange.IsAlmost(0.0)
|
---|
| 77 | ? 6
|
---|
| 78 | : (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
|
---|
| 79 | graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
|
---|
| 80 | }
|
---|
| 81 | int startingPoint = Data.StartingPoint;
|
---|
| 82 | int terminalPoint = Data.TerminalPoint;
|
---|
| 83 | Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
|
---|
| 84 | var beginSize = graphics.MeasureString("Begin", font);
|
---|
| 85 | if (startingPoint >= 0 && startingPoint < points.Length)
|
---|
| 86 | graphics.DrawString("Begin", font, Brushes.Black, points[startingPoint].X - beginSize.Width, points[startingPoint].Y - beginSize.Height);
|
---|
| 87 | if (terminalPoint >= 0 && terminalPoint < points.Length)
|
---|
| 88 | graphics.DrawString("End", font, Brushes.Black, points[terminalPoint].X, points[terminalPoint].Y);
|
---|
| 89 | }
|
---|
| 90 | } else {
|
---|
| 91 | using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
---|
| 92 | graphics.Clear(Color.White);
|
---|
| 93 | Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
|
---|
| 94 | string text = "No coordinates defined or in wrong format.";
|
---|
| 95 | SizeF strSize = graphics.MeasureString(text, font);
|
---|
| 96 | graphics.DrawString(text, font, Brushes.Black, (float)(width - strSize.Width) / 2.0f, (float)(height - strSize.Height) / 2.0f);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | return bitmap;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|