Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5723 was 4374, checked in by svonolfe, 14 years ago

Added analyzers and views (#1177)

File size: 5.7 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      Pen[] pens = {new Pen(Color.FromArgb(92,20,237)), new Pen(Color.FromArgb(237,183,20)), new Pen(Color.FromArgb(237,20,219)), new Pen(Color.FromArgb(20,237,76)),
34                    new Pen(Color.FromArgb(237,61,20)), new Pen(Color.FromArgb(115,78,26)), new Pen(Color.FromArgb(20,237,229)), new Pen(Color.FromArgb(39,101,19)),
35                    new Pen(Color.FromArgb(230,170,229)), new Pen(Color.FromArgb(142,136,89)), new Pen(Color.FromArgb(157,217,166)), new Pen(Color.FromArgb(31,19,101)),
36                    new Pen(Color.FromArgb(173,237,20)), new Pen(Color.FromArgb(230,231,161)), new Pen(Color.FromArgb(142,89,89)), new Pen(Color.FromArgb(93,89,142)),
37                    new Pen(Color.FromArgb(146,203,217)), new Pen(Color.FromArgb(101,19,75)), new Pen(Color.FromArgb(198,20,237)), new Pen(Color.FromArgb(185,185,185)),
38                    new Pen(Color.FromArgb(179,32,32)), new Pen(Color.FromArgb(18,119,115)), new Pen(Color.FromArgb(104,158,239)), new Pen(Color.Black)};
39
40      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
41        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
42        for (int i = 0; i < coordinates.Rows; i++) {
43          if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
44          if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
45          if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
46          if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
47        }
48
49        int border = 20;
50        double xStep = xMax != xMin ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
51        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
52
53        using (Graphics graphics = Graphics.FromImage(bitmap)) {
54          if (Solution != null) {
55            int currentTour = 0;
56            foreach (Tour tour in Solution.GetTours()) {
57              double t = 0.0;
58              Point[] tourPoints = new Point[tour.Stops.Count + 2];
59              Brush[] customerBrushes = new Brush[tour.Stops.Count];
60              int lastCustomer = 0;
61
62              for (int i = -1; i <= tour.Stops.Count; i++) {
63                int location = 0;
64
65                if (i == -1 || i == tour.Stops.Count)
66                  location = 0; //depot
67                else
68                  location = tour.Stops[i];
69
70                Point locationPoint = new Point(border + ((int)((coordinates[location, 0] - xMin) * xStep)),
71                                bitmap.Height - (border + ((int)((coordinates[location, 1] - yMin) * yStep))));
72                tourPoints[i + 1] = locationPoint;
73
74                if (i != -1 && i != tour.Stops.Count) {
75                  Brush customerBrush = Brushes.Black;
76
77                  t += Content.GetDistance(
78                    lastCustomer, location);
79
80                  if (t < readyTime[location]) {
81                    t = readyTime[location];
82                    customerBrush = Brushes.Orange;
83                  } else if (t > dueTime[location]) {
84                    customerBrush = Brushes.Red;
85                  }
86
87                  t += serviceTime[location];
88                  customerBrushes[i] = customerBrush;
89                }
90                lastCustomer = location;
91              }
92
93              graphics.DrawPolygon(pens[((currentTour >= pens.Length) ? (pens.Length - 1) : (currentTour))], tourPoints);
94
95              for (int i = 0; i < tour.Stops.Count; i++) {
96                graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
97              }
98
99              graphics.FillEllipse(Brushes.Blue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
100
101              currentTour++;
102            }
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      for (int i = 0; i < pens.Length; i++)
121        pens[i].Dispose();
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.