Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation.Views/3.3/PickupDeliveryVisualizationView.cs @ 10879

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

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

File size: 10.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.Core.Views;
10using HeuristicLab.MainForm;
11using HeuristicLab.PDPSimulation.DomainModel;
12using HeuristicLab.MainForm.WindowsForms;
13
14namespace HeuristicLab.PDPSimulation.Views {
15  [View("PickupDeliveryVisualization View")]
16  [Content(typeof(PickupDeliveryVisualization), true)]
17  public partial class PickupDeliveryVisualizationView : ItemView {   
18    public new PickupDeliveryVisualization Content {
19      get { return (PickupDeliveryVisualization)base.Content; }
20      set { base.Content = value; }
21    }
22   
23    public PickupDeliveryVisualizationView() {
24      InitializeComponent();
25    }
26
27    protected override void DeregisterContentEvents() {
28      Content.ContentChanged -= new EventHandler(Content_ContentChanged);
29      base.DeregisterContentEvents();
30    }
31
32    protected override void RegisterContentEvents() {
33      base.RegisterContentEvents();
34      Content.ContentChanged += new EventHandler(Content_ContentChanged);
35    }
36
37    void Content_ContentChanged(object sender, EventArgs e) {
38      GenerateImage();
39    }
40
41    private bool drawRoutes = false;
42
43    void pictureBox_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
44      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
45        drawRoutes = !drawRoutes;
46        GenerateImage();
47      }
48    }
49
50    private void GenerateImage() {
51      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
52        PickupDeliveryVisualization content = Content;
53
54        if (content == null) {
55          pictureBox.Image = null;
56        } else {         
57          int customerSize = Resources.source.Width;
58          int depotSize = Resources.depot.Width;
59          int truckSize = Resources.truck.Width;
60
61          int border = customerSize * 2;
62
63          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
64          double width = bitmap.Width - border * 2;
65          double height = bitmap.Height - border * 2;
66
67          double rangeX = content.OrderRangeX;
68          double rangeY = content.OrderRangeY;
69
70          using (Graphics graphics = Graphics.FromImage(bitmap)) {
71            if (drawRoutes) {
72              float completedWidth = 1;
73              Pen[] pastPens = {new Pen(Color.FromArgb(92,20,237), completedWidth), new Pen(Color.FromArgb(237,183,20), completedWidth), new Pen(Color.FromArgb(237,20,219), completedWidth), new Pen(Color.FromArgb(20,237,76), completedWidth),
74                    new Pen(Color.FromArgb(237,61,20), completedWidth), new Pen(Color.FromArgb(115,78,26), completedWidth), new Pen(Color.FromArgb(20,237,229), completedWidth), new Pen(Color.FromArgb(39,101,19), completedWidth),
75                    new Pen(Color.FromArgb(230,170,229), completedWidth), new Pen(Color.FromArgb(142,136,89), completedWidth), new Pen(Color.FromArgb(157,217,166), completedWidth), new Pen(Color.FromArgb(31,19,101), completedWidth),
76                    new Pen(Color.FromArgb(173,237,20), completedWidth), new Pen(Color.FromArgb(230,231,161), completedWidth), new Pen(Color.FromArgb(142,89,89), completedWidth), new Pen(Color.FromArgb(93,89,142), completedWidth),
77                    new Pen(Color.FromArgb(146,203,217), completedWidth), new Pen(Color.FromArgb(101,19,75), completedWidth), new Pen(Color.FromArgb(198,20,237), completedWidth), new Pen(Color.FromArgb(185,185,185), completedWidth),
78                    new Pen(Color.FromArgb(179,32,32), completedWidth), new Pen(Color.FromArgb(18,119,115), completedWidth), new Pen(Color.FromArgb(104,158,239), completedWidth), new Pen(Color.Black, completedWidth)};
79
80              float toBeServedWidth = 2;
81              Pen[] futurePens = {new Pen(Color.FromArgb(92,20,237), toBeServedWidth), new Pen(Color.FromArgb(237,183,20), toBeServedWidth), new Pen(Color.FromArgb(237,20,219), toBeServedWidth), new Pen(Color.FromArgb(20,237,76), toBeServedWidth),
82                    new Pen(Color.FromArgb(237,61,20), toBeServedWidth), new Pen(Color.FromArgb(115,78,26), toBeServedWidth), new Pen(Color.FromArgb(20,237,229), toBeServedWidth), new Pen(Color.FromArgb(39,101,19), toBeServedWidth),
83                    new Pen(Color.FromArgb(230,170,229), toBeServedWidth), new Pen(Color.FromArgb(142,136,89), toBeServedWidth), new Pen(Color.FromArgb(157,217,166), toBeServedWidth), new Pen(Color.FromArgb(31,19,101), toBeServedWidth),
84                    new Pen(Color.FromArgb(173,237,20), toBeServedWidth), new Pen(Color.FromArgb(230,231,161), toBeServedWidth), new Pen(Color.FromArgb(142,89,89), toBeServedWidth), new Pen(Color.FromArgb(93,89,142), toBeServedWidth),
85                    new Pen(Color.FromArgb(146,203,217), toBeServedWidth), new Pen(Color.FromArgb(101,19,75), toBeServedWidth), new Pen(Color.FromArgb(198,20,237), toBeServedWidth), new Pen(Color.FromArgb(185,185,185), toBeServedWidth),
86                    new Pen(Color.FromArgb(179,32,32), toBeServedWidth), new Pen(Color.FromArgb(18,119,115), toBeServedWidth), new Pen(Color.FromArgb(104,158,239), toBeServedWidth), new Pen(Color.Black, toBeServedWidth)};
87
88              int currentTour = 0;
89              foreach (Vehicle vehicle in content.Vehicles) {
90                PDAction action = content.GetAction(vehicle);
91                List<Point> tourPoints = new List<Point>();
92
93                while (action != null && action.ActionCompleted()) {
94                  if (action is MoveVehicleAction) {
95                    MoveVehicleAction moveAction = action as MoveVehicleAction;
96
97                    if (tourPoints.Count == 0) {
98                      tourPoints.Add(new Point((int)(border + moveAction.SourceX / rangeX * width),
99                        (int)(height + border - moveAction.SourceY / rangeY * height)));
100                    }
101
102                    Point locationPoint = new Point((int)(border + moveAction.TargetX / rangeX * width),
103                      (int)(height + border - moveAction.TargetY / rangeY * height));
104
105                    tourPoints.Add(locationPoint);
106                  }
107
108                  action = action.Successor;
109                }
110
111                if (tourPoints.Count > 1) {
112                  graphics.DrawLines(pastPens[((currentTour >= pastPens.Length) ? (pastPens.Length - 1) : (currentTour))], tourPoints.ToArray());
113
114                  Point last = tourPoints.Last();
115                  tourPoints.Clear();
116                  tourPoints.Add(last);
117                }
118
119                while (action != null) {
120                  if (action is MoveVehicleAction) {
121                    MoveVehicleAction moveAction = action as MoveVehicleAction;
122
123                    if (tourPoints.Count > 0 || (moveAction.SourceX >= 0 && moveAction.SourceY >= 0)) {
124                      if (tourPoints.Count == 0) {
125                        tourPoints.Add(new Point((int)(border + moveAction.SourceX / rangeX * width),
126                          (int)(height + border - moveAction.SourceY / rangeY * height)));
127                      }
128
129                      Point locationPoint = new Point((int)(border + moveAction.TargetX / rangeX * width),
130                        (int)(height + border - moveAction.TargetY / rangeY * height));
131
132                      tourPoints.Add(locationPoint);
133                    }
134                  }
135
136                  action = action.Successor;
137                }
138
139                if (tourPoints.Count > 1) {
140                  graphics.DrawLines(futurePens[((currentTour >= futurePens.Length) ? (futurePens.Length - 1) : (currentTour))], tourPoints.ToArray());
141                  currentTour++;
142                }
143              }
144
145              for (int i = 0; i < pastPens.Length; i++)
146                pastPens[i].Dispose();
147              for (int i = 0; i < futurePens.Length; i++)
148                futurePens[i].Dispose();
149            } else {
150              using (Pen flowPen = new Pen(Brushes.Green)) {
151                flowPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
152                //flowPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
153
154                foreach (Order order in content.Orders) {
155                  if (order.OrderState != OrderState.Cancelled &&
156                    order.OrderState != OrderState.Delivered) {
157                    Point pickup = new Point(
158                      (int)(border + order.PickupXCoord / rangeX * width),
159                      (int)(height + border - order.PickupYCoord / rangeY * height));
160
161                    graphics.DrawImage(Resources.source, pickup.X - customerSize / 2, pickup.Y - customerSize / 2);
162
163                    if (!order.ServiceRequest) {
164                      Point delivery = new Point(
165                        (int)(border + order.DeliveryXCoord / rangeX * width),
166                        (int)(height + border - order.DeliveryYCoord / rangeY * height));
167
168                      graphics.DrawImage(Resources.target, delivery.X - customerSize / 2, delivery.Y - customerSize / 2);
169
170                      graphics.DrawLine(flowPen, pickup, delivery);
171                    }
172                  }
173                }
174              }
175            }
176
177            foreach (Vehicle vehicle in content.Vehicles) {
178              Point depotPoint = new Point(
179                (int)(border + vehicle.DepotX / rangeX * width),
180                (int)(height + border - vehicle.DepotY / rangeY * height));
181
182              graphics.DrawImage(Resources.depot, depotPoint.X - depotSize / 2,
183                depotPoint.Y - depotSize / 2);
184            }
185
186            foreach (Vehicle vehicle in content.Vehicles) {
187              Point vehiclePoint = new Point(
188                (int)(border + vehicle.PositionX / rangeX * width),
189                (int)(height + border - vehicle.PositionY / rangeY * height));
190
191              if (vehicle.VehicleState == VehicleState.Waiting ||
192                vehicle.VehicleState == VehicleState.Parked ||
193                vehicle.VehicleState == VehicleState.InvalidAction) {
194                graphics.DrawImage(Resources.truck_idle, vehiclePoint.X - truckSize / 2,
195                  vehiclePoint.Y - truckSize / 2);
196              } else {
197                graphics.DrawImage(Resources.truck, vehiclePoint.X - truckSize / 2,
198                  vehiclePoint.Y - truckSize / 2);
199              }
200            }
201          }
202          pictureBox.Image = bitmap;
203        }
204      }
205    }
206
207    private void PickupDeliveryVisualizationView_SizeChanged(object sender, EventArgs e) {
208      GenerateImage();
209    }
210
211    protected override void OnContentChanged() {
212      base.OnContentChanged();
213
214      GenerateImage();
215    }
216  }
217}
Note: See TracBrowser for help on using the repository browser.