Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Problems.VehicleRouting.Views/3.4/MDCVRPTWView.cs @ 11294

Last change on this file since 11294 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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