Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting.Views/3.4/CVRPPDTWView.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 7.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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("CVRPPDTWProblemInstance View")]
31  [Content(typeof(CVRPPDTWProblemInstance), true)]
32  public partial class CVRPPDTWView : VRPProblemInstanceView {
33    public new CVRPPDTWProblemInstance Content {
34      get { return (CVRPPDTWProblemInstance)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public CVRPPDTWView() {
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
61      Pen flowPen = new Pen(Brushes.Red, 3);
62      flowPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
63      flowPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
64
65      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
66        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
67        for (int i = 0; i < coordinates.Rows; i++) {
68          if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
69          if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
70          if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
71          if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
72        }
73
74        int border = 20;
75        double xStep = xMax != xMin ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
76        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
77
78        using (Graphics graphics = Graphics.FromImage(bitmap)) {
79          if (Solution != null) {
80            int currentTour = 0;
81
82            List<Tour> tours = Solution.GetTours();
83            List<Pen> pens = GetColors(tours.Count);
84
85            foreach (Tour tour in tours) {
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 = new Point(border + ((int)((coordinates[location, 0] - xMin) * xStep)),
103                                bitmap.Height - (border + ((int)((coordinates[location, 1] - yMin) * yStep))));
104                tourPoints[i + 1] = locationPoint;
105
106                if (i != -1 && i != tour.Stops.Count) {
107                  Brush customerBrush = Brushes.Black;
108                  Pen customerPen = Pens.Black;
109
110                  t += Content.GetDistance(
111                    lastCustomer, location, Solution);
112
113                  if (t < readyTime[location]) {
114                    t = readyTime[location];
115                    customerBrush = Brushes.Orange;
116                    customerPen = Pens.Orange;
117
118                  } else if (t > dueTime[location]) {
119                    customerBrush = Brushes.Red;
120                    customerPen = Pens.Red;
121                  }
122
123                  t += serviceTime[location];
124
125                  validPickupDelivery[i] =
126                    ((demand[location] >= 0) ||
127                     (stops.ContainsKey(pickupDeliveryLocation[location])));
128
129                  customerBrushes[i] = customerBrush;
130                  customerPens[i] = customerPen;
131
132                  stops.Add(location, true);
133                }
134                lastCustomer = location;
135              }
136
137              if (!drawFlow)
138                graphics.DrawPolygon(pens[currentTour], tourPoints);
139
140              for (int i = 0; i < tour.Stops.Count; i++) {
141                if (validPickupDelivery[i]) {
142                  graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
143
144                  if (demand[tour.Stops[i]] < 0 && drawFlow) {
145                    int location = pickupDeliveryLocation[tour.Stops[i]];
146                    int source = tour.Stops.IndexOf(location);
147
148                    graphics.DrawLine(flowPen, tourPoints[source + 1], tourPoints[i + 1]);
149                  }
150                } else
151                  graphics.DrawRectangle(customerPens[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
152              }
153
154              graphics.FillEllipse(Brushes.Blue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
155
156              currentTour++;
157            }
158
159            for (int i = 0; i < pens.Count; i++)
160              pens[i].Dispose();
161          } else {
162            {
163              Point[] locationPoints = new Point[coordinates.Rows];
164              //just draw customers
165              for (int i = 1; i < coordinates.Rows; i++) {
166                locationPoints[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
167                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
168
169                graphics.FillRectangle(Brushes.Black, locationPoints[i].X - 3, locationPoints[i].Y - 3, 6, 6);
170              }
171
172              if (drawFlow) {
173                for (int i = 1; i < coordinates.Rows; i++) {
174                  if (demand[i] < 0) {
175
176                    graphics.DrawLine(flowPen, locationPoints[pickupDeliveryLocation[i]], locationPoints[i]);
177                  }
178                }
179              }
180
181              Point locationPoint = new Point(border + ((int)((coordinates[0, 0] - xMin) * xStep)),
182                                bitmap.Height - (border + ((int)((coordinates[0, 1] - yMin) * yStep))));
183              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
184            }
185          }
186        }
187      }
188
189      flowPen.Dispose();
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.