Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1955_DynamicVehicleRouting/HeuristicLab.PDPSimulation.Views/3.3/VehicleHistoryView.cs @ 17526

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

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

File size: 7.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Linq;
6using System.Text;
7using System.Windows.Forms;
8using HeuristicLab.Core.Views;
9using HeuristicLab.MainForm;
10using HeuristicLab.PDPSimulation.DomainModel;
11using HeuristicLab.MainForm.WindowsForms;
12using HeuristicLab.Analysis;
13
14namespace HeuristicLab.PDPSimulation.Views {
15  [View("VehicleHistoryView")]
16  [Content(typeof(VehicleHistoryVisualization), true)]
17  public partial class VehicleHistoryView : ItemView {
18    public new VehicleHistoryVisualization Content {
19      get { return (VehicleHistoryVisualization)base.Content; }
20      set { base.Content = value; }
21    }
22
23    public VehicleHistoryView() {
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    void pictureBox_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
42      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
43        GenerateImage();
44      }
45    }
46
47    private List<Order> GetServicedOrders(VehicleHistory vehicle) {
48      List<Order> result = new List<Order>();
49
50      Order current = null;
51      for (int i = 0; i < vehicle.VehicleState.Count; i++) {
52        current = vehicle.ActiveOrders[i];
53        if (vehicle.VehicleState[i] == VehicleState.Servicing && !result.Contains(current))
54          result.Add(current);
55      }
56
57      return result;
58    }
59
60    private void GenerateImage() {
61      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
62        VehicleHistoryVisualization content = Content;
63
64        if (content == null) {
65          pictureBox.Image = null;
66        } else {
67          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
68          using (Graphics graphics = Graphics.FromImage(bitmap)) {
69            IEnumerable<VehicleHistory> vehicles = content.VehicleHistory.Where(v => v.VehicleState.Any(s => s != VehicleState.Parked && s != VehicleState.Waiting));
70            int vehicleCount = vehicles.Count();
71
72            if (vehicleCount > 0) {
73              float width = pictureBox.Width - 1;
74              float height = pictureBox.Height - 1;
75
76              int orderCount = 0;
77              foreach (VehicleHistory vehicle in vehicles)
78                orderCount += GetServicedOrders(vehicle).Count;
79
80              int totalRows = vehicleCount * 3 + orderCount;
81              float rowHeight = height / (float)totalRows;
82
83              int totalColumns = vehicles.First().VehicleState.Count;
84              float columnWidth = width / (float)totalColumns;
85              float timeWidth = columnWidth / (float)content.ResultCollectionInterval;
86
87              float curY = 0;
88              foreach (VehicleHistory vehicle in vehicles) {
89                float curX = 0;
90
91                int curAvailability = 1;
92                VehicleState curState = VehicleState.Parked;
93                int counter = 0;
94                for (int i = 0; i <= vehicle.VehicleState.Count; i++) {
95                  counter++;
96
97                  int availability = 1;
98                  VehicleState state = VehicleState.Parked;
99                  if (i < vehicle.VehicleState.Count) {
100                    state = vehicle.VehicleState[i];
101                    availability = vehicle.VehicleAvailability[i];
102                  }
103
104                  if (state != curState || availability != curAvailability || (i == vehicle.VehicleState.Count && i != 0)) {
105                    float stateWidth = columnWidth * (float)counter;
106
107                    Brush brush = Brushes.White;
108                    if (curAvailability <= 0)
109                      brush = Brushes.DarkGray;
110                    if (curState == VehicleState.Moving)
111                      brush = Brushes.Green;
112                    else if (curState == VehicleState.Servicing)
113                      brush = Brushes.Red;
114                    else if (curState == VehicleState.InvalidAction)
115                      brush = Brushes.Gray;
116
117                    graphics.FillRectangle(brush, curX, curY, stateWidth, rowHeight);
118
119                    curState = state;
120                    curAvailability = availability;
121                    counter = 0;
122                    curX += stateWidth;
123                  }
124                }
125
126                graphics.DrawRectangle(Pens.Black, 0, curY, width, rowHeight);
127                           
128                curY += rowHeight;
129                curY += rowHeight;
130
131                foreach (Order order in GetServicedOrders(vehicle)) {
132                  float begin = Math.Min(pictureBox.Width, (float)order.PickupReadyTime * timeWidth);
133                  float end = Math.Min(pictureBox.Width, (float)order.PickupDueTime * timeWidth);
134                  graphics.DrawLine(Pens.Blue, begin, curY + rowHeight / 2, end, curY + rowHeight / 2);
135                  graphics.DrawLine(Pens.Blue, begin, curY, begin, curY + rowHeight);
136                  graphics.DrawLine(Pens.Blue, end, curY, end, curY + rowHeight);
137
138                  if (order.OrderState != OrderState.Waiting) {
139                    float pickupTime = Math.Min(width, (float)order.PickupTime * timeWidth);
140                    graphics.FillEllipse(Brushes.Blue, pickupTime - rowHeight / 4, curY + rowHeight / 4, rowHeight / 2, rowHeight / 2);
141                  }
142
143                  if (!order.ServiceRequest) {
144                    begin = Math.Min(pictureBox.Width, (float)order.DeliveryReadyTime * timeWidth);
145                    end = Math.Min(pictureBox.Width, (float)order.DeliveryDueTime * timeWidth);
146                    graphics.DrawLine(Pens.Green, begin, curY + rowHeight / 2, end, curY + rowHeight / 2);
147                    graphics.DrawLine(Pens.Green, begin, curY, begin, curY + rowHeight);
148                    graphics.DrawLine(Pens.Green, end, curY, end, curY + rowHeight);
149
150                    if (order.OrderState != OrderState.Waiting && order.OrderState != OrderState.PickingUp && order.OrderState != OrderState.PickedUp) {
151                      float deliveryTime = Math.Min(width, (float)order.DeliveryTime * timeWidth);
152                      graphics.FillEllipse(Brushes.Green, deliveryTime - rowHeight / 4, curY + rowHeight / 4, rowHeight / 2, rowHeight / 2);
153                    }
154                  }
155                 
156                  curY += rowHeight;
157                }
158
159                curY += rowHeight;
160              }
161            }
162            pictureBox.Image = bitmap;
163          }
164        }
165      }
166    }
167
168    protected override void OnContentChanged() {
169      base.OnContentChanged();
170
171      GenerateImage();
172    }
173
174    private void VehicleHistoryView_SizeChanged(object sender, EventArgs e) {
175      GenerateImage();
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.