Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.VehicleRouting.Views/3.4/MDCVRPTWView.cs @ 17401

Last change on this file since 17401 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.9 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("MDCVRPTWProblemInstance View")]
31  [Content(typeof(MDCVRPTWProblemInstance), true)]
32  public partial class MDCVRPTWView : VRPProblemInstanceView {
33    public new MDCVRPTWProblemInstance Content {
34      get { return (MDCVRPTWProblemInstance)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public MDCVRPTWView() {
39      InitializeComponent();
40    }
41
42    protected override void DrawVisualization(Bitmap bitmap) {
43      DoubleMatrix coordinates = Content.Coordinates;
44      DoubleMatrix distanceMatrix = Content.DistanceMatrix;
45      BoolValue useDistanceMatrix = Content.UseDistanceMatrix;
46      DoubleArray dueTime = Content.DueTime;
47      DoubleArray serviceTime = Content.ServiceTime;
48      DoubleArray readyTime = Content.ReadyTime;
49      IntArray vehicleAssignment = Content.VehicleDepotAssignment;
50
51      int depots = Content.Depots.Value;
52
53      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
54        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
55        for (int i = 0; i < coordinates.Rows; i++) {
56          if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
57          if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
58          if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
59          if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
60        }
61
62        int border = 20;
63        double xStep = xMax != xMin ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
64        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
65
66        using (Graphics graphics = Graphics.FromImage(bitmap)) {
67          if (Solution != null) {
68            for (int i = 0; i < Content.Depots.Value; i++) {
69              Point locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
70                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
71              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
72            }
73
74            int currentTour = 0;
75
76            List<Tour> tours = Solution.GetTours();
77            List<Pen> pens = GetColors(tours.Count);
78
79            foreach (Tour tour in tours) {
80              int tourIndex = Solution.GetTourIndex(tour);
81              int vehicle = Solution.GetVehicleAssignment(tourIndex);
82              int depot = vehicleAssignment[vehicle];
83
84              double t = 0.0;
85              Point[] tourPoints = new Point[tour.Stops.Count + 2];
86              Brush[] customerBrushes = new Brush[tour.Stops.Count];
87              int lastCustomer = 0;
88
89              for (int i = -1; i <= tour.Stops.Count; i++) {
90                int location = 0;
91
92                if (i == -1 || i == tour.Stops.Count)
93                  location = 0; //depot
94                else
95                  location = tour.Stops[i];
96
97                Point locationPoint;
98                if (location == 0) {
99                  locationPoint = new Point(border + ((int)((Content.Coordinates[depot, 0] - xMin) * xStep)),
100                                  bitmap.Height - (border + ((int)((Content.Coordinates[depot, 1] - yMin) * yStep))));
101                } else {
102                  locationPoint = new Point(border + ((int)((Content.GetCoordinates(location)[0] - xMin) * xStep)),
103                                  bitmap.Height - (border + ((int)((Content.GetCoordinates(location)[1] - yMin) * yStep))));
104                }
105                tourPoints[i + 1] = locationPoint;
106
107                if (i != -1 && i != tour.Stops.Count) {
108                  Brush customerBrush = Brushes.Black;
109
110                  t += Content.GetDistance(
111                    lastCustomer, location, Solution);
112
113                  int locationIndex;
114                  if (location == 0)
115                    locationIndex = depot;
116                  else
117                    locationIndex = location + depots - 1;
118
119                  if (t < readyTime[locationIndex]) {
120                    t = readyTime[locationIndex];
121                    customerBrush = Brushes.Orange;
122                  } else if (t > dueTime[locationIndex]) {
123                    customerBrush = Brushes.Red;
124                  }
125
126                  t += serviceTime[location - 1];
127                  customerBrushes[i] = customerBrush;
128                }
129                lastCustomer = location;
130              }
131
132              graphics.DrawPolygon(pens[currentTour], tourPoints);
133
134              for (int i = 0; i < tour.Stops.Count; i++) {
135                graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
136              }
137
138              graphics.FillEllipse(Brushes.DarkBlue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
139
140              currentTour++;
141            }
142
143            for (int i = 0; i < pens.Count; i++)
144              pens[i].Dispose();
145          } else {
146            Point locationPoint;
147            //just draw customers
148            for (int i = 1; i < coordinates.Rows; i++) {
149              locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
150                              bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
151
152              graphics.FillRectangle(Brushes.Black, locationPoint.X - 3, locationPoint.Y - 3, 6, 6);
153            }
154
155            for (int i = 0; i < Content.Depots.Value; i++) {
156              locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
157                                bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
158              graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
159            }
160          }
161        }
162      }
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.