Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting.Views/3.4/MultiDepotVRPView.cs @ 8583

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

Added color palette (#1177)

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