Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting.Views/3.4/MDCVRPTWView.cs @ 7781

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

Added support for the multi depot CVRP with time windows (#1177)

File size: 7.0 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("MDCVRPTWProblemInstance View")]
14  [Content(typeof(MDCVRPTWProblemInstance), true)]
15  public partial class MDCVRPTWView : VRPProblemInstanceView {
16    public new MDCVRPTWProblemInstance Content {
17      get { return (MDCVRPTWProblemInstance)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public MDCVRPTWView() {
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      DoubleArray dueTime = Content.DueTime;
30      DoubleArray serviceTime = Content.ServiceTime;
31      DoubleArray readyTime = Content.ReadyTime;
32      IntArray vehicleAssignment = Content.VehicleDepotAssignment;
33
34      int depots = Content.Depots.Value;
35
36      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)),
37                    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)),
38                    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)),
39                    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)),
40                    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)),
41                    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)};
42
43      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
44        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
45        for (int i = 0; i < coordinates.Rows; i++) {
46          if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
47          if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
48          if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
49          if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
50        }
51
52        int border = 20;
53        double xStep = xMax != xMin ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
54        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
55
56        using (Graphics graphics = Graphics.FromImage(bitmap)) {
57          if (Solution != null) {
58            for (int i = 0; i < Content.Depots.Value; i++) {
59              Point locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
60                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
61              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
62            }
63
64            int currentTour = 0;
65            foreach (Tour tour in Solution.GetTours()) {
66              int tourIndex = Solution.GetTourIndex(tour);
67              int vehicle = Solution.GetVehicleAssignment(tourIndex);
68              int depot = vehicleAssignment[vehicle];
69
70              double t = 0.0;
71              Point[] tourPoints = new Point[tour.Stops.Count + 2];
72              Brush[] customerBrushes = new Brush[tour.Stops.Count];
73              int lastCustomer = 0;
74
75              for (int i = -1; i <= tour.Stops.Count; i++) {
76                int location = 0;
77
78                if (i == -1 || i == tour.Stops.Count)
79                  location = 0; //depot
80                else
81                  location = tour.Stops[i];
82
83                Point locationPoint;
84                if (location == 0) {
85                  locationPoint = new Point(border + ((int)((Content.Coordinates[depot, 0] - xMin) * xStep)),
86                                  bitmap.Height - (border + ((int)((Content.Coordinates[depot, 1] - yMin) * yStep))));
87                } else {
88                  locationPoint = new Point(border + ((int)((Content.GetCoordinates(location)[0] - xMin) * xStep)),
89                                  bitmap.Height - (border + ((int)((Content.GetCoordinates(location)[1] - yMin) * yStep))));
90                }
91                tourPoints[i + 1] = locationPoint;
92
93                if (i != -1 && i != tour.Stops.Count) {
94                  Brush customerBrush = Brushes.Black;
95
96                  t += Content.GetDistance(
97                    lastCustomer, location, Solution);
98
99                  int locationIndex;
100                  if (location == 0)
101                    locationIndex = depot;
102                  else
103                    locationIndex = location + depots - 1;
104
105                  if (t < readyTime[locationIndex]) {
106                    t = readyTime[locationIndex];
107                    customerBrush = Brushes.Orange;
108                  } else if (t > dueTime[locationIndex]) {
109                    customerBrush = Brushes.Red;
110                  }
111
112                  t += serviceTime[location - 1];
113                  customerBrushes[i] = customerBrush;
114                }
115                lastCustomer = location;
116              }
117
118              graphics.DrawPolygon(pens[((currentTour >= pens.Length) ? (pens.Length - 1) : (currentTour))], tourPoints);
119
120              for (int i = 0; i < tour.Stops.Count; i++) {
121                graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
122              }
123
124              graphics.FillEllipse(Brushes.DarkBlue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
125
126              currentTour++;
127            }
128          } else {
129            Point locationPoint;
130            //just draw customers
131            for (int i = 1; i < coordinates.Rows; i++) {
132              locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
133                              bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
134
135              graphics.FillRectangle(Brushes.Black, locationPoint.X - 3, locationPoint.Y - 3, 6, 6);
136            }
137
138            for (int i = 0; i < Content.Depots.Value; i++) {
139              locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
140                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
141              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
142            }
143          }
144        }
145      }
146
147      for (int i = 0; i < pens.Length; i++)
148        pens[i].Dispose();
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.