Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2707_HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting.Views/3.4/MDCVRPPDTWView.cs @ 17010

Last change on this file since 17010 was 17010, checked in by pfleck, 5 years ago

#2707 Merged trunk into branch

File size: 9.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.MainForm;
26using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Problems.VehicleRouting.Views {
30  [View("MDCVRPPDTWProblemInstance View")]
31  [Content(typeof(MDCVRPPDTWProblemInstance), true)]
32  public partial class MDCVRPPDTWView : VRPProblemInstanceView {
33    public new MDCVRPPDTWProblemInstance Content {
34      get { return (MDCVRPPDTWProblemInstance)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public MDCVRPPDTWView() {
39      InitializeComponent();
40    }
41
42    private bool drawFlow = false;
43
44    protected override void pictureBox_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
45      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
46        drawFlow = !drawFlow;
47        GenerateImage();
48      }     
49    }
50
51    protected override void DrawVisualization(Bitmap bitmap) {
52      DoubleMatrix coordinates = Content.Coordinates;
53      DoubleMatrix distanceMatrix = Content.DistanceMatrix;
54      BoolValue useDistanceMatrix = Content.UseDistanceMatrix;
55      DoubleArray dueTime = Content.DueTime;
56      DoubleArray serviceTime = Content.ServiceTime;
57      DoubleArray readyTime = Content.ReadyTime;
58      DoubleArray demand = Content.Demand;
59      IntArray pickupDeliveryLocation = Content.PickupDeliveryLocation;
60      IntArray vehicleAssignment = Content.VehicleDepotAssignment;
61
62      int depots = Content.Depots.Value;
63      int cities = Content.Cities.Value;
64
65      Pen flowPen = new Pen(Brushes.Red, 3);
66      flowPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
67      flowPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
68
69      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
70        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
71        for (int i = 0; i < coordinates.Rows; i++) {
72          if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
73          if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
74          if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
75          if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
76        }
77
78        int border = 20;
79        double xStep = xMax != xMin ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
80        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
81
82        using (Graphics graphics = Graphics.FromImage(bitmap)) {
83          if (Solution != null) {
84            for (int i = 0; i < Content.Depots.Value; i++) {
85              Point locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
86                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
87              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
88            }
89
90            int currentTour = 0;
91
92            List<Tour> tours = Solution.GetTours();
93            List<Pen> pens = GetColors(tours.Count);
94
95            foreach (Tour tour in tours) {
96              int tourIndex = Solution.GetTourIndex(tour);
97              int vehicle = Solution.GetVehicleAssignment(tourIndex);
98              int depot = vehicleAssignment[vehicle];
99
100              double t = 0.0;
101              Point[] tourPoints = new Point[tour.Stops.Count + 2];
102              Brush[] customerBrushes = new Brush[tour.Stops.Count];
103              Pen[] customerPens = new Pen[tour.Stops.Count];
104              bool[] validPickupDelivery = new bool[tour.Stops.Count];
105              int lastCustomer = 0;
106
107              Dictionary<int, bool> stops = new Dictionary<int, bool>();
108              for (int i = -1; i <= tour.Stops.Count; i++) {
109                int location = 0;
110
111                if (i == -1 || i == tour.Stops.Count)
112                  location = 0; //depot
113                else
114                  location = tour.Stops[i];
115
116                Point locationPoint;
117                if (location == 0) {
118                  locationPoint = new Point(border + ((int)((Content.Coordinates[depot, 0] - xMin) * xStep)),
119                                  bitmap.Height - (border + ((int)((Content.Coordinates[depot, 1] - yMin) * yStep))));
120                } else {
121                  locationPoint = new Point(border + ((int)((Content.GetCoordinates(location)[0] - xMin) * xStep)),
122                                  bitmap.Height - (border + ((int)((Content.GetCoordinates(location)[1] - yMin) * yStep))));
123                }
124                tourPoints[i + 1] = locationPoint;
125
126                if (i != -1 && i != tour.Stops.Count) {
127                  Brush customerBrush = Brushes.Black;
128                  Pen customerPen = Pens.Black;
129
130                  t += Content.GetDistance(
131                    lastCustomer, location, Solution);
132
133                  int locationIndex;
134                  if (location == 0)
135                    locationIndex = depot;
136                  else
137                    locationIndex = location + depots - 1;
138
139                  if (t < readyTime[locationIndex]) {
140                    t = readyTime[locationIndex];
141                    customerBrush = Brushes.Orange;
142                    customerPen = Pens.Orange;
143
144                  } else if (t > dueTime[locationIndex]) {
145                    customerBrush = Brushes.Red;
146                    customerPen = Pens.Red;
147                  }
148
149                  t += serviceTime[location - 1];
150
151                  validPickupDelivery[i] =
152                    ((demand[location - 1] >= 0) ||
153                     (stops.ContainsKey(pickupDeliveryLocation[location - 1])));
154
155                  customerBrushes[i] = customerBrush;
156                  customerPens[i] = customerPen;
157
158                  stops.Add(location, true);
159                }
160                lastCustomer = location;
161              }
162
163              if (!drawFlow)
164                graphics.DrawPolygon(pens[currentTour], tourPoints);
165
166              for (int i = 0; i < tour.Stops.Count; i++) {
167                if (validPickupDelivery[i]) {
168                  graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
169
170                  if (demand[tour.Stops[i] - 1] < 0 && drawFlow) {
171                    int location = pickupDeliveryLocation[tour.Stops[i] - 1];
172                    int source = tour.Stops.IndexOf(location);
173
174                    graphics.DrawLine(flowPen, tourPoints[source + 1], tourPoints[i + 1]);
175                  }
176                } else
177                  graphics.DrawRectangle(customerPens[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
178              }
179
180              graphics.FillEllipse(Brushes.DarkBlue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
181
182              currentTour++;
183            }
184
185            for (int i = 0; i < pens.Count; i++)
186              pens[i].Dispose();
187          } else {
188            {
189              Point[] locationPoints = new Point[cities];
190              //just draw customers
191              for (int i = 0; i < cities; i++) {
192                locationPoints[i] = new Point(border + ((int)((Content.GetCoordinates(i + 1)[0] - xMin) * xStep)),
193                                bitmap.Height - (border + ((int)((Content.GetCoordinates(i + 1)[1] - yMin) * yStep))));
194
195                graphics.FillRectangle(Brushes.Black, locationPoints[i].X - 3, locationPoints[i].Y - 3, 6, 6);
196              }
197
198              if (drawFlow) {
199                for (int i = 0; i < cities; i++) {
200                  if (demand[i] < 0) {
201                    graphics.DrawLine(flowPen, locationPoints[pickupDeliveryLocation[i] - 1], locationPoints[i]);
202                  }
203                }
204              }
205
206              for (int i = 0; i < Content.Depots.Value; i++) {
207                Point locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
208                                  bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
209                graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
210              }
211            }
212          }
213        }
214      }
215
216      flowPen.Dispose();
217    }
218  }
219}
Note: See TracBrowser for help on using the repository browser.