Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added color palette (#1177)

File size: 6.9 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    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
44      Pen flowPen = new Pen(Brushes.Red, 3);
45      flowPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
46      flowPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
47
48      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
49        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
50        for (int i = 0; i < coordinates.Rows; i++) {
51          if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
52          if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
53          if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
54          if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
55        }
56
57        int border = 20;
58        double xStep = xMax != xMin ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
59        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
60
61        using (Graphics graphics = Graphics.FromImage(bitmap)) {
62          if (Solution != null) {
63            int currentTour = 0;
64
65            List<Tour> tours = Solution.GetTours();
66            List<Pen> pens = GetColors(tours.Count);
67
68            foreach (Tour tour in tours) {
69              double t = 0.0;
70              Point[] tourPoints = new Point[tour.Stops.Count + 2];
71              Brush[] customerBrushes = new Brush[tour.Stops.Count];
72              Pen[] customerPens = new Pen[tour.Stops.Count];
73              bool[] validPickupDelivery = new bool[tour.Stops.Count];
74              int lastCustomer = 0;
75
76              Dictionary<int, bool> stops = new Dictionary<int, bool>();
77              for (int i = -1; i <= tour.Stops.Count; i++) {
78                int location = 0;
79
80                if (i == -1 || i == tour.Stops.Count)
81                  location = 0; //depot
82                else
83                  location = tour.Stops[i];
84
85                Point locationPoint = new Point(border + ((int)((coordinates[location, 0] - xMin) * xStep)),
86                                bitmap.Height - (border + ((int)((coordinates[location, 1] - yMin) * yStep))));
87                tourPoints[i + 1] = locationPoint;
88
89                if (i != -1 && i != tour.Stops.Count) {
90                  Brush customerBrush = Brushes.Black;
91                  Pen customerPen = Pens.Black;
92
93                  t += Content.GetDistance(
94                    lastCustomer, location, Solution);
95
96                  if (t < readyTime[location]) {
97                    t = readyTime[location];
98                    customerBrush = Brushes.Orange;
99                    customerPen = Pens.Orange;
100
101                  } else if (t > dueTime[location]) {
102                    customerBrush = Brushes.Red;
103                    customerPen = Pens.Red;
104                  }
105
106                  t += serviceTime[location];
107
108                  validPickupDelivery[i] =
109                    ((demand[location] >= 0) ||
110                     (stops.ContainsKey(pickupDeliveryLocation[location])));
111
112                  customerBrushes[i] = customerBrush;
113                  customerPens[i] = customerPen;
114
115                  stops.Add(location, true);
116                }
117                lastCustomer = location;
118              }
119
120              if (!drawFlow)
121                graphics.DrawPolygon(pens[currentTour], tourPoints);
122
123              for (int i = 0; i < tour.Stops.Count; i++) {
124                if (validPickupDelivery[i]) {
125                  graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
126
127                  if (demand[tour.Stops[i]] < 0 && drawFlow) {
128                    int location = pickupDeliveryLocation[tour.Stops[i]];
129                    int source = tour.Stops.IndexOf(location);
130
131                    graphics.DrawLine(flowPen, tourPoints[source + 1], tourPoints[i + 1]);
132                  }
133                } else
134                  graphics.DrawRectangle(customerPens[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
135              }
136
137              graphics.FillEllipse(Brushes.Blue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
138
139              currentTour++;
140            }
141
142            for (int i = 0; i < pens.Count; i++)
143              pens[i].Dispose();
144          } else {
145            {
146              Point[] locationPoints = new Point[coordinates.Rows];
147              //just draw customers
148              for (int i = 1; i < coordinates.Rows; i++) {
149                locationPoints[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
150                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
151
152                graphics.FillRectangle(Brushes.Black, locationPoints[i].X - 3, locationPoints[i].Y - 3, 6, 6);
153              }
154
155              if (drawFlow) {
156                for (int i = 1; i < coordinates.Rows; i++) {
157                  if (demand[i] < 0) {
158
159                    graphics.DrawLine(flowPen, locationPoints[pickupDeliveryLocation[i]], locationPoints[i]);
160                  }
161                }
162              }
163
164              Point locationPoint = new Point(border + ((int)((coordinates[0, 0] - xMin) * xStep)),
165                                bitmap.Height - (border + ((int)((coordinates[0, 1] - yMin) * yStep))));
166              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
167            }
168          }
169        }
170      }
171
172      flowPen.Dispose();
173    }
174  }
175}
Note: See TracBrowser for help on using the repository browser.