Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

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