Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting.Views/3.4/MDCVRPPDTWView.cs @ 11427

Last change on this file since 11427 was 7858, checked in by svonolfe, 12 years ago

Added color palette (#1177)

File size: 8.3 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("MDCVRPPDTWProblemInstance View")]
14  [Content(typeof(MDCVRPPDTWProblemInstance), true)]
15  public partial class MDCVRPPDTWView : VRPProblemInstanceView {
16    public new MDCVRPPDTWProblemInstance Content {
17      get { return (MDCVRPPDTWProblemInstance)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public MDCVRPPDTWView() {
22      InitializeComponent();
23    }
24
25    private bool drawFlow = false;
26
27    protected override void pictureBox_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
28      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
29        drawFlow = !drawFlow;
30        GenerateImage();
31      }     
32    }
33
34    protected override void DrawVisualization(Bitmap bitmap) {
35      DoubleMatrix coordinates = Content.Coordinates;
36      DoubleMatrix distanceMatrix = Content.DistanceMatrix;
37      BoolValue useDistanceMatrix = Content.UseDistanceMatrix;
38      DoubleArray dueTime = Content.DueTime;
39      DoubleArray serviceTime = Content.ServiceTime;
40      DoubleArray readyTime = Content.ReadyTime;
41      DoubleArray demand = Content.Demand;
42      IntArray pickupDeliveryLocation = Content.PickupDeliveryLocation;
43      IntArray vehicleAssignment = Content.VehicleDepotAssignment;
44
45      int depots = Content.Depots.Value;
46      int cities = Content.Cities.Value;
47
48      Pen flowPen = new Pen(Brushes.Red, 3);
49      flowPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
50      flowPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
51
52      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
53        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
54        for (int i = 0; i < coordinates.Rows; i++) {
55          if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
56          if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
57          if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
58          if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
59        }
60
61        int border = 20;
62        double xStep = xMax != xMin ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
63        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
64
65        using (Graphics graphics = Graphics.FromImage(bitmap)) {
66          if (Solution != null) {
67            for (int i = 0; i < Content.Depots.Value; i++) {
68              Point locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
69                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
70              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
71            }
72
73            int currentTour = 0;
74
75            List<Tour> tours = Solution.GetTours();
76            List<Pen> pens = GetColors(tours.Count);
77
78            foreach (Tour tour in tours) {
79              int tourIndex = Solution.GetTourIndex(tour);
80              int vehicle = Solution.GetVehicleAssignment(tourIndex);
81              int depot = vehicleAssignment[vehicle];
82
83              double t = 0.0;
84              Point[] tourPoints = new Point[tour.Stops.Count + 2];
85              Brush[] customerBrushes = new Brush[tour.Stops.Count];
86              Pen[] customerPens = new Pen[tour.Stops.Count];
87              bool[] validPickupDelivery = new bool[tour.Stops.Count];
88              int lastCustomer = 0;
89
90              Dictionary<int, bool> stops = new Dictionary<int, bool>();
91              for (int i = -1; i <= tour.Stops.Count; i++) {
92                int location = 0;
93
94                if (i == -1 || i == tour.Stops.Count)
95                  location = 0; //depot
96                else
97                  location = tour.Stops[i];
98
99                Point locationPoint;
100                if (location == 0) {
101                  locationPoint = new Point(border + ((int)((Content.Coordinates[depot, 0] - xMin) * xStep)),
102                                  bitmap.Height - (border + ((int)((Content.Coordinates[depot, 1] - yMin) * yStep))));
103                } else {
104                  locationPoint = new Point(border + ((int)((Content.GetCoordinates(location)[0] - xMin) * xStep)),
105                                  bitmap.Height - (border + ((int)((Content.GetCoordinates(location)[1] - yMin) * yStep))));
106                }
107                tourPoints[i + 1] = locationPoint;
108
109                if (i != -1 && i != tour.Stops.Count) {
110                  Brush customerBrush = Brushes.Black;
111                  Pen customerPen = Pens.Black;
112
113                  t += Content.GetDistance(
114                    lastCustomer, location, Solution);
115
116                  int locationIndex;
117                  if (location == 0)
118                    locationIndex = depot;
119                  else
120                    locationIndex = location + depots - 1;
121
122                  if (t < readyTime[locationIndex]) {
123                    t = readyTime[locationIndex];
124                    customerBrush = Brushes.Orange;
125                    customerPen = Pens.Orange;
126
127                  } else if (t > dueTime[locationIndex]) {
128                    customerBrush = Brushes.Red;
129                    customerPen = Pens.Red;
130                  }
131
132                  t += serviceTime[location - 1];
133
134                  validPickupDelivery[i] =
135                    ((demand[location - 1] >= 0) ||
136                     (stops.ContainsKey(pickupDeliveryLocation[location - 1])));
137
138                  customerBrushes[i] = customerBrush;
139                  customerPens[i] = customerPen;
140
141                  stops.Add(location, true);
142                }
143                lastCustomer = location;
144              }
145
146              if (!drawFlow)
147                graphics.DrawPolygon(pens[currentTour], tourPoints);
148
149              for (int i = 0; i < tour.Stops.Count; i++) {
150                if (validPickupDelivery[i]) {
151                  graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
152
153                  if (demand[tour.Stops[i] - 1] < 0 && drawFlow) {
154                    int location = pickupDeliveryLocation[tour.Stops[i] - 1];
155                    int source = tour.Stops.IndexOf(location);
156
157                    graphics.DrawLine(flowPen, tourPoints[source + 1], tourPoints[i + 1]);
158                  }
159                } else
160                  graphics.DrawRectangle(customerPens[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
161              }
162
163              graphics.FillEllipse(Brushes.DarkBlue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
164
165              currentTour++;
166            }
167
168            for (int i = 0; i < pens.Count; i++)
169              pens[i].Dispose();
170          } else {
171            {
172              Point[] locationPoints = new Point[cities];
173              //just draw customers
174              for (int i = 0; i < cities; i++) {
175                locationPoints[i] = new Point(border + ((int)((Content.GetCoordinates(i + 1)[0] - xMin) * xStep)),
176                                bitmap.Height - (border + ((int)((Content.GetCoordinates(i + 1)[1] - yMin) * yStep))));
177
178                graphics.FillRectangle(Brushes.Black, locationPoints[i].X - 3, locationPoints[i].Y - 3, 6, 6);
179              }
180
181              if (drawFlow) {
182                for (int i = 0; i < cities; i++) {
183                  if (demand[i] < 0) {
184                    graphics.DrawLine(flowPen, locationPoints[pickupDeliveryLocation[i] - 1], locationPoints[i]);
185                  }
186                }
187              }
188
189              for (int i = 0; i < Content.Depots.Value; i++) {
190                Point locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
191                                  bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
192                graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
193              }
194            }
195          }
196        }
197      }
198
199      flowPen.Dispose();
200    }
201  }
202}
Note: See TracBrowser for help on using the repository browser.