Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2645_ContextAlgorithms/HeuristicLab.Problems.VehicleRouting.Views/3.4/CVRPPDTWView.cs @ 17230

Last change on this file since 17230 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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("CVRPPDTWProblemInstance View")]
35  [Content(typeof(CVRPPDTWProblemInstance), true)]
36  public partial class CVRPPDTWView : VRPProblemInstanceView {
37    public new CVRPPDTWProblemInstance Content {
38      get { return (CVRPPDTWProblemInstance)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public CVRPPDTWView() {
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
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            int currentTour = 0;
85
86            List<Tour> tours = Solution.GetTours();
87            List<Pen> pens = GetColors(tours.Count);
88
89            foreach (Tour tour in tours) {
90              double t = 0.0;
91              Point[] tourPoints = new Point[tour.Stops.Count + 2];
92              Brush[] customerBrushes = new Brush[tour.Stops.Count];
93              Pen[] customerPens = new Pen[tour.Stops.Count];
94              bool[] validPickupDelivery = new bool[tour.Stops.Count];
95              int lastCustomer = 0;
96
97              Dictionary<int, bool> stops = new Dictionary<int, bool>();
98              for (int i = -1; i <= tour.Stops.Count; i++) {
99                int location = 0;
100
101                if (i == -1 || i == tour.Stops.Count)
102                  location = 0; //depot
103                else
104                  location = tour.Stops[i];
105
106                Point locationPoint = new Point(border + ((int)((coordinates[location, 0] - xMin) * xStep)),
107                                bitmap.Height - (border + ((int)((coordinates[location, 1] - yMin) * yStep))));
108                tourPoints[i + 1] = locationPoint;
109
110                if (i != -1 && i != tour.Stops.Count) {
111                  Brush customerBrush = Brushes.Black;
112                  Pen customerPen = Pens.Black;
113
114                  t += Content.GetDistance(
115                    lastCustomer, location, Solution);
116
117                  if (t < readyTime[location]) {
118                    t = readyTime[location];
119                    customerBrush = Brushes.Orange;
120                    customerPen = Pens.Orange;
121
122                  } else if (t > dueTime[location]) {
123                    customerBrush = Brushes.Red;
124                    customerPen = Pens.Red;
125                  }
126
127                  t += serviceTime[location];
128
129                  validPickupDelivery[i] =
130                    ((demand[location] >= 0) ||
131                     (stops.ContainsKey(pickupDeliveryLocation[location])));
132
133                  customerBrushes[i] = customerBrush;
134                  customerPens[i] = customerPen;
135
136                  stops.Add(location, true);
137                }
138                lastCustomer = location;
139              }
140
141              if (!drawFlow)
142                graphics.DrawPolygon(pens[currentTour], tourPoints);
143
144              for (int i = 0; i < tour.Stops.Count; i++) {
145                if (validPickupDelivery[i]) {
146                  graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
147
148                  if (demand[tour.Stops[i]] < 0 && drawFlow) {
149                    int location = pickupDeliveryLocation[tour.Stops[i]];
150                    int source = tour.Stops.IndexOf(location);
151
152                    graphics.DrawLine(flowPen, tourPoints[source + 1], tourPoints[i + 1]);
153                  }
154                } else
155                  graphics.DrawRectangle(customerPens[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
156              }
157
158              graphics.FillEllipse(Brushes.Blue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
159
160              currentTour++;
161            }
162
163            for (int i = 0; i < pens.Count; i++)
164              pens[i].Dispose();
165          } else {
166            {
167              Point[] locationPoints = new Point[coordinates.Rows];
168              //just draw customers
169              for (int i = 1; i < coordinates.Rows; i++) {
170                locationPoints[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
171                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
172
173                graphics.FillRectangle(Brushes.Black, locationPoints[i].X - 3, locationPoints[i].Y - 3, 6, 6);
174              }
175
176              if (drawFlow) {
177                for (int i = 1; i < coordinates.Rows; i++) {
178                  if (demand[i] < 0) {
179
180                    graphics.DrawLine(flowPen, locationPoints[pickupDeliveryLocation[i]], locationPoints[i]);
181                  }
182                }
183              }
184
185              Point locationPoint = new Point(border + ((int)((coordinates[0, 0] - xMin) * xStep)),
186                                bitmap.Height - (border + ((int)((coordinates[0, 1] - yMin) * yStep))));
187              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
188            }
189          }
190        }
191      }
192
193      flowPen.Dispose();
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.