Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting.Views/3.4/MDCVRPTWView.cs @ 10743

Last change on this file since 10743 was 7858, checked in by svonolfe, 13 years ago

Added color palette (#1177)

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