Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting.Views/3.4/CVRPPDTWView.cs @ 6710

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

Added support for pickups and deliveries (#1177)

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