Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting.Views/3.4/CVRPTWView.cs @ 10514

Last change on this file since 10514 was 7858, checked in by svonolfe, 12 years ago

Added color palette (#1177)

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