Free cookie consent management tool by TermsFeed Policy Generator

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

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

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