Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting.Views/3.4/SingleDepotVRPView.cs @ 6049

Last change on this file since 6049 was 4374, checked in by svonolfe, 14 years ago

Added analyzers and views (#1177)

File size: 5.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using HeuristicLab.MainForm;
9using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
10using HeuristicLab.Data;
11
12namespace HeuristicLab.Problems.VehicleRouting.Views {
13  [View("SingleDepotVRPProblemInstance View")]
14  [Content(typeof(SingleDepotVRPProblemInstance), true)]
15  public partial class SingleDepotVRPView : VRPProblemInstanceView {
16    public new SingleDepotVRPProblemInstance Content {
17      get { return (SingleDepotVRPProblemInstance)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public SingleDepotVRPView() {
22      InitializeComponent();
23    }
24
25    protected override void DrawVisualization(Bitmap bitmap) {
26      DoubleMatrix coordinates = Content.Coordinates;
27      DoubleMatrix distanceMatrix = Content.DistanceMatrix;
28      BoolValue useDistanceMatrix = Content.UseDistanceMatrix;
29
30      Pen[] pens = {new Pen(Color.FromArgb(92,20,237)), new Pen(Color.FromArgb(237,183,20)), new Pen(Color.FromArgb(237,20,219)), new Pen(Color.FromArgb(20,237,76)),
31                    new Pen(Color.FromArgb(237,61,20)), new Pen(Color.FromArgb(115,78,26)), new Pen(Color.FromArgb(20,237,229)), new Pen(Color.FromArgb(39,101,19)),
32                    new Pen(Color.FromArgb(230,170,229)), new Pen(Color.FromArgb(142,136,89)), new Pen(Color.FromArgb(157,217,166)), new Pen(Color.FromArgb(31,19,101)),
33                    new Pen(Color.FromArgb(173,237,20)), new Pen(Color.FromArgb(230,231,161)), new Pen(Color.FromArgb(142,89,89)), new Pen(Color.FromArgb(93,89,142)),
34                    new Pen(Color.FromArgb(146,203,217)), new Pen(Color.FromArgb(101,19,75)), new Pen(Color.FromArgb(198,20,237)), new Pen(Color.FromArgb(185,185,185)),
35                    new Pen(Color.FromArgb(179,32,32)), new Pen(Color.FromArgb(18,119,115)), new Pen(Color.FromArgb(104,158,239)), new Pen(Color.Black)};
36
37
38      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
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 ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
49        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
50
51        using (Graphics graphics = Graphics.FromImage(bitmap)) {
52          if (Solution != null) {
53            int currentTour = 0;
54            foreach (Tour tour in Solution.GetTours()) {
55              Point[] tourPoints = new Point[tour.Stops.Count + 2];
56              Brush[] customerBrushes = new Brush[tour.Stops.Count];
57              int lastCustomer = 0;
58
59              for (int i = -1; i <= tour.Stops.Count; i++) {
60                int location = 0;
61
62                if (i == -1 || i == tour.Stops.Count)
63                  location = 0; //depot
64                else
65                  location = tour.Stops[i];
66
67                Point locationPoint = new Point(border + ((int)((coordinates[location, 0] - xMin) * xStep)),
68                                bitmap.Height - (border + ((int)((coordinates[location, 1] - yMin) * yStep))));
69                tourPoints[i + 1] = locationPoint;
70
71                if (i != -1 && i != tour.Stops.Count) {
72                  Brush customerBrush = Brushes.Black;
73                  customerBrushes[i] = customerBrush;
74                }
75                lastCustomer = location;
76              }
77
78              graphics.DrawPolygon(pens[((currentTour >= pens.Length) ? (pens.Length - 1) : (currentTour))], tourPoints);
79
80              for (int i = 0; i < tour.Stops.Count; i++) {
81                graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
82              }
83
84              graphics.FillEllipse(Brushes.Blue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
85
86              currentTour++;
87            }
88          } else {
89            Point locationPoint;
90            //just draw customers
91            for (int i = 1; i < coordinates.Rows; i++) {
92              locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
93                              bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
94
95              graphics.FillRectangle(Brushes.Black, locationPoint.X - 3, locationPoint.Y - 3, 6, 6);
96            }
97
98            locationPoint = new Point(border + ((int)((coordinates[0, 0] - xMin) * xStep)),
99                              bitmap.Height - (border + ((int)((coordinates[0, 1] - yMin) * yStep))));
100            graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
101          }
102        }
103      }
104
105      for (int i = 0; i < pens.Length; i++)
106        pens[i].Dispose();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.