Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.VehicleRouting.Views/3.4/CVRPTWView.cs @ 17787

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

#2875: Removed years in copyrights

File size: 5.5 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("CVRPTWProblemInstance View")]
31  [Content(typeof(CVRPTWProblemInstance), true)]
32  public partial class CVRPTWView : VRPProblemInstanceView {
33    public new CVRPTWProblemInstance Content {
34      get { return (CVRPTWProblemInstance)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public CVRPTWView() {
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
50      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
51        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
52        for (int i = 0; i < coordinates.Rows; i++) {
53          if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
54          if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
55          if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
56          if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
57        }
58
59        int border = 20;
60        double xStep = xMax != xMin ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
61        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
62
63        using (Graphics graphics = Graphics.FromImage(bitmap)) {
64          if (Solution != null) {
65            int currentTour = 0;
66
67            List<Tour> tours = Solution.GetTours();
68            List<Pen> pens = GetColors(tours.Count);
69
70            foreach (Tour tour in tours) {
71              double t = 0.0;
72              Point[] tourPoints = new Point[tour.Stops.Count + 2];
73              Brush[] customerBrushes = new Brush[tour.Stops.Count];
74              int lastCustomer = 0;
75
76              for (int i = -1; i <= tour.Stops.Count; i++) {
77                int location = 0;
78
79                if (i == -1 || i == tour.Stops.Count)
80                  location = 0; //depot
81                else
82                  location = tour.Stops[i];
83
84                Point locationPoint = new Point(border + ((int)((coordinates[location, 0] - xMin) * xStep)),
85                                bitmap.Height - (border + ((int)((coordinates[location, 1] - yMin) * yStep))));
86                tourPoints[i + 1] = locationPoint;
87
88                if (i != -1 && i != tour.Stops.Count) {
89                  Brush customerBrush = Brushes.Black;
90
91                  t += Content.GetDistance(
92                    lastCustomer, location, Solution);
93
94                  if (t < readyTime[location]) {
95                    t = readyTime[location];
96                    customerBrush = Brushes.Orange;
97                  } else if (t > dueTime[location]) {
98                    customerBrush = Brushes.Red;
99                  }
100
101                  t += serviceTime[location];
102                  customerBrushes[i] = customerBrush;
103                }
104                lastCustomer = location;
105              }
106
107              graphics.DrawPolygon(pens[currentTour], tourPoints);
108
109              for (int i = 0; i < tour.Stops.Count; i++) {
110                graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
111              }
112
113              graphics.FillEllipse(Brushes.Blue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
114
115              currentTour++;
116            }
117
118            for (int i = 0; i < pens.Count; i++)
119              pens[i].Dispose();
120          } else {
121            Point locationPoint;
122            //just draw customers
123            for (int i = 1; i < coordinates.Rows; i++) {
124              locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
125                              bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
126
127              graphics.FillRectangle(Brushes.Black, locationPoint.X - 3, locationPoint.Y - 3, 6, 6);
128            }
129
130            locationPoint = new Point(border + ((int)((coordinates[0, 0] - xMin) * xStep)),
131                              bitmap.Height - (border + ((int)((coordinates[0, 1] - yMin) * yStep))));
132            graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
133          }
134        }
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.