Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7317 was 6947, checked in by svonolfe, 13 years ago

Fixed small issues in view and crossover (#1177)

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