Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting.Views/3.4/MultiDepotVRPView.cs @ 6851

Last change on this file since 6851 was 6851, checked in by svonolfe, 13 years ago

Added support for multi depot CVRP instances (#1177)

File size: 6.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("MultiDepotVRPProblemInstance View")]
14  [Content(typeof(MultiDepotVRPProblemInstance), true)]
15  public partial class MultiDepotVRPView : VRPProblemInstanceView {
16    public new MultiDepotVRPProblemInstance Content {
17      get { return (MultiDepotVRPProblemInstance)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public MultiDepotVRPView() {
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            for (int i = 0; i < Content.Depots.Value; i++) {
54              Point locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
55                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
56              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
57            }
58
59            int currentTour = 0;
60            foreach (Tour tour in Solution.GetTours()) {
61              Point[] tourPoints = new Point[tour.Stops.Count + 2];
62              Brush[] customerBrushes = new Brush[tour.Stops.Count];
63              int lastCustomer = 0;
64
65              for (int i = -1; i <= tour.Stops.Count; i++) {
66                int location = 0;
67
68                if (i == -1 || i == tour.Stops.Count)
69                  location = 0; //depot
70                else
71                  location = tour.Stops[i];
72
73                Point locationPoint;
74
75                if (location == 0) {
76                  int tourIndex = Solution.GetTourIndex(tour);
77                  int vehicle = Solution.GetVehicleAssignment(tourIndex);
78                  int depot = Content.VehicleDepotAssignment[vehicle];
79
80                  locationPoint = new Point(border + ((int)((Content.Coordinates[depot, 0] - xMin) * xStep)),
81                                  bitmap.Height - (border + ((int)((Content.Coordinates[depot, 1] - yMin) * yStep))));
82                } else {
83                  locationPoint = new Point(border + ((int)((Content.GetCoordinates(location)[0] - xMin) * xStep)),
84                                  bitmap.Height - (border + ((int)((Content.GetCoordinates(location)[1] - yMin) * yStep))));
85                }
86                tourPoints[i + 1] = locationPoint;
87
88                if (i != -1 && i != tour.Stops.Count) {
89                  Brush customerBrush = Brushes.Black;
90                  customerBrushes[i] = customerBrush;
91                }
92                lastCustomer = location;
93              }
94
95              graphics.DrawPolygon(pens[((currentTour >= pens.Length) ? (pens.Length - 1) : (currentTour))], tourPoints);
96
97              for (int i = 0; i < tour.Stops.Count; i++) {
98                graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
99              }
100
101              graphics.FillEllipse(Brushes.DarkBlue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
102
103              currentTour++;
104            }
105          } else {
106            Point locationPoint;
107            //just draw customers
108            for (int i = 1; i <= Content.Cities.Value; i++) {
109              locationPoint = new Point(border + ((int)((Content.GetCoordinates(i)[0] - xMin) * xStep)),
110                              bitmap.Height - (border + ((int)((Content.GetCoordinates(i)[1] - yMin) * yStep))));
111
112              graphics.FillRectangle(Brushes.Black, locationPoint.X - 3, locationPoint.Y - 3, 6, 6);
113            }
114
115            for (int i = 0; i < Content.Depots.Value; i++) {
116              locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
117                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
118              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
119            }
120          }
121        }
122      }
123
124      for (int i = 0; i < pens.Length; i++)
125        pens[i].Dispose();
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.