Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting.Views/3.4/MDCVRPPDTWView.cs @ 13304

Last change on this file since 13304 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

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