Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting.Views/3.3/VRPSolutionView.cs @ 4174

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

Refactored VRP, added some Potvin operators (WIP) (#1039)

File size: 10.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.Data;
27using HeuristicLab.MainForm;
28using HeuristicLab.Problems.VehicleRouting.Encodings;
29
30namespace HeuristicLab.Problems.VehicleRouting.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
33  /// </summary>
34  [View("VRPSolution View")]
35  [Content(typeof(VRPSolution), true)]
36  public sealed partial class VRPSolutionView : ItemView {
37    public new VRPSolution Content {
38      get { return (VRPSolution)base.Content; }
39      set { base.Content = value; }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="ItemBaseView"/>.
44    /// </summary>
45    public VRPSolutionView() {
46      InitializeComponent();
47    }
48
49    protected override void DeregisterContentEvents() {
50      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
51      Content.DistanceChanged -= new EventHandler(Content_DistanceChanged);
52      Content.OverloadChanged -= new EventHandler(Content_OverloadChanged);
53      Content.TardinessChanged -= new EventHandler(Content_TardinessChanged);
54      Content.TravelTimeChanged -= new EventHandler(Content_TravelTimeChanged);
55      Content.VehicleUtilizationChanged -= new EventHandler(Content_VehicleUtilizationChanged);
56      Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
57      Content.SolutionChanged -= new EventHandler(Content_SolutionChanged);
58      base.DeregisterContentEvents();
59    }
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.QualityChanged += new EventHandler(Content_QualityChanged);
63      Content.DistanceChanged += new EventHandler(Content_DistanceChanged);
64      Content.OverloadChanged += new EventHandler(Content_OverloadChanged);
65      Content.TardinessChanged += new EventHandler(Content_TardinessChanged);
66      Content.TravelTimeChanged += new EventHandler(Content_TravelTimeChanged);
67      Content.VehicleUtilizationChanged += new EventHandler(Content_VehicleUtilizationChanged);
68      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
69      Content.SolutionChanged += new EventHandler(Content_SolutionChanged);
70    }
71
72    protected override void OnContentChanged() {
73      base.OnContentChanged();
74      if (Content == null) {
75        pictureBox.Image = null;
76      } else {
77        qualityViewHost.Content = Content.Quality;
78        distanceViewHost.Content = Content.Distance;
79        overloadViewHost.Content = Content.Overload;
80        tardinessViewHost.Content = Content.Tardiness;
81        travelTimeViewHost.Content = Content.TravelTime;
82        vehicleUtilizationViewHost.Content = Content.VehicleUtilization;
83
84        GenerateImage();
85        tourViewHost.Content = Content.Solution;
86      }
87    }
88
89    protected override void OnReadOnlyChanged() {
90      base.OnReadOnlyChanged();
91      SetEnabledStateOfControls();
92    }
93
94    protected override void SetEnabledStateOfControls() {
95      base.SetEnabledStateOfControls();
96      tableLayoutPanel1.Enabled = Content != null;
97      pictureBox.Enabled = Content != null;
98      tourGroupBox.Enabled = Content != null;
99    }
100
101    private void GenerateImage() {
102      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
103        if (Content == null) {
104          pictureBox.Image = null;
105        } else {
106          DoubleMatrix coordinates = Content.Coordinates;
107          DoubleMatrix distanceMatrix = Content.DistanceMatrix;
108          BoolValue useDistanceMatrix = Content.UseDistanceMatrix;
109          DoubleArray dueTime = Content.DueTime;
110          DoubleArray serviceTime = Content.ServiceTime;
111          DoubleArray readyTime = Content.ReadyTime;
112
113          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
114
115          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)),
116                    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)),
117                    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)),
118                    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)),
119                    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)),
120                    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)};
121
122          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
123            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
124            for (int i = 0; i < coordinates.Rows; i++) {
125              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
126              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
127              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
128              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
129            }
130
131            int border = 20;
132            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
133            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
134
135            using (Graphics graphics = Graphics.FromImage(bitmap)) {
136              if (Content.Solution != null) {
137                int currentTour = 0;
138                foreach (Tour tour in Content.Solution.Tours) {
139                  double t = 0.0;
140                  Point[] tourPoints = new Point[tour.Cities.Count + 2];
141                  int lastCustomer = 0;
142
143                  for (int i = -1; i <= tour.Cities.Count; i++) {
144                    int location = 0;
145
146                    if (i == -1 || i == tour.Cities.Count)
147                      location = 0; //depot
148                    else
149                      location = tour.Cities[i];
150
151                    Point locationPoint = new Point(border + ((int)((coordinates[location, 0] - xMin) * xStep)),
152                                    bitmap.Height - (border + ((int)((coordinates[location, 1] - yMin) * yStep))));
153                    tourPoints[i + 1] = locationPoint;
154
155                    if (i != -1 && i != tour.Cities.Count) {
156                      Brush customerBrush = Brushes.Black;
157
158                      t += VRPUtilities.GetDistance(
159                        lastCustomer, location, coordinates, distanceMatrix, useDistanceMatrix);
160
161                      if (t < readyTime[location]) {
162                        t = readyTime[location];
163                        customerBrush = Brushes.Yellow;
164                      } else if (t > dueTime[location]) {
165                        customerBrush = Brushes.Red;
166                      }
167
168                      t += serviceTime[location];
169
170                      graphics.FillRectangle(customerBrush, locationPoint.X - 2, locationPoint.Y - 2, 6, 6);
171                    }
172                    lastCustomer = location;
173                  }
174
175                  graphics.DrawPolygon(pens[((currentTour >= pens.Length) ? (pens.Length - 1) : (currentTour))], tourPoints);
176                  currentTour++;
177                }
178              }
179            }
180          }
181
182          for (int i = 0; i < pens.Length; i++)
183            pens[i].Dispose();
184
185          pictureBox.Image = bitmap;
186        }
187      }
188    }
189
190    private void Content_QualityChanged(object sender, EventArgs e) {
191      if (InvokeRequired)
192        Invoke(new EventHandler(Content_QualityChanged), sender, e);
193      else
194        qualityViewHost.Content = Content.Quality;
195    }
196    void Content_DistanceChanged(object sender, EventArgs e) {
197      if (InvokeRequired)
198        Invoke(new EventHandler(Content_DistanceChanged), sender, e);
199      else
200        distanceViewHost.Content = Content.Distance;
201    }
202    private void Content_CoordinatesChanged(object sender, EventArgs e) {
203      if (InvokeRequired)
204        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
205      else
206        GenerateImage();
207    }
208    void Content_OverloadChanged(object sender, EventArgs e) {
209      if (InvokeRequired)
210        Invoke(new EventHandler(Content_OverloadChanged), sender, e);
211      else
212        overloadViewHost.Content = Content.Overload;
213    }
214    void Content_TardinessChanged(object sender, EventArgs e) {
215      if (InvokeRequired)
216        Invoke(new EventHandler(Content_TardinessChanged), sender, e);
217      else
218        tardinessViewHost.Content = Content.Tardiness;
219    }
220    void Content_TravelTimeChanged(object sender, EventArgs e) {
221      if (InvokeRequired)
222        Invoke(new EventHandler(Content_TravelTimeChanged), sender, e);
223      else
224        travelTimeViewHost.Content = Content.TravelTime;
225    }
226    void Content_VehicleUtilizationChanged(object sender, EventArgs e) {
227      if (InvokeRequired)
228        Invoke(new EventHandler(Content_VehicleUtilizationChanged), sender, e);
229      else
230        vehicleUtilizationViewHost.Content = Content.VehicleUtilization;
231    }
232    private void Content_SolutionChanged(object sender, EventArgs e) {
233      if (InvokeRequired)
234        Invoke(new EventHandler(Content_SolutionChanged), sender, e);
235      else {
236        GenerateImage();
237        tourViewHost.Content = Content.Solution;
238      }
239    }
240    private void pictureBox_SizeChanged(object sender, EventArgs e) {
241      GenerateImage();
242    }
243  }
244}
Note: See TracBrowser for help on using the repository browser.