Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation.Views/3.3/DynPDPView.cs @ 8670

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

Added first version of the dynamic vehicle routing addon (#1955)

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