Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed crash in VRP solution view (#1039)

File size: 10.4 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        pictureBox.Image = null;
77      } else {
78        qualityViewHost.Content = Content.Quality;
79        distanceViewHost.Content = Content.Distance;
80        overloadViewHost.Content = Content.Overload;
81        tardinessViewHost.Content = Content.Tardiness;
82        travelTimeViewHost.Content = Content.TravelTime;
83        vehicleUtilizationViewHost.Content = Content.VehicleUtilization;
84
85        GenerateImage();
86        tourViewHost.Content = Content.Solution;
87      }
88    }
89
90    protected override void OnReadOnlyChanged() {
91      base.OnReadOnlyChanged();
92      SetEnabledStateOfControls();
93    }
94
95    protected override void SetEnabledStateOfControls() {
96      base.SetEnabledStateOfControls();
97      tableLayoutPanel1.Enabled = Content != null;
98      pictureBox.Enabled = Content != null;
99      tourGroupBox.Enabled = Content != null;
100    }
101
102    private void GenerateImage() {
103      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
104        if (Content == null) {
105          pictureBox.Image = null;
106        } else {
107          DoubleMatrix coordinates = Content.Coordinates;
108          DoubleMatrix distanceMatrix = Content.DistanceMatrix;
109          BoolValue useDistanceMatrix = Content.UseDistanceMatrix;
110          DoubleArray dueTime = Content.DueTime;
111          DoubleArray serviceTime = Content.ServiceTime;
112          DoubleArray readyTime = Content.ReadyTime;
113
114          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
115
116          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)),
117                    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)),
118                    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)),
119                    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)),
120                    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)),
121                    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)};
122
123          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
124            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
125            for (int i = 0; i < coordinates.Rows; i++) {
126              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
127              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
128              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
129              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
130            }
131
132            int border = 20;
133            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
134            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
135
136            using (Graphics graphics = Graphics.FromImage(bitmap)) {
137              if (Content.Solution != null) {
138                int currentTour = 0;
139                foreach (Tour tour in Content.Solution.Tours) {
140                  double t = 0.0;
141                  Point[] tourPoints = new Point[tour.Count];
142                  int lastCustomer = 0;
143
144                  for (int i = 0; i < tour.Count; i++) {
145                    int customer = tour[i].Value;
146
147                    Point customerPoint = new Point(border + ((int)((coordinates[customer, 0] - xMin) * xStep)),
148                                    bitmap.Height - (border + ((int)((coordinates[customer, 1] - yMin) * yStep))));
149                    tourPoints[i] = customerPoint;
150
151                    if (i > 0) {
152                      Brush customerBrush = Brushes.Black;
153
154                      t += VehicleRoutingProblem.GetDistance(
155                        lastCustomer, customer, coordinates, distanceMatrix, useDistanceMatrix);
156
157                      if (t < readyTime[customer]) {
158                        t = readyTime[customer];
159                        customerBrush = Brushes.Yellow;
160                      } else if (t > dueTime[customer]) {
161                        customerBrush = Brushes.Red;
162                      }
163
164                      t += serviceTime[customer];
165
166                      graphics.FillRectangle(customerBrush, customerPoint.X - 2, customerPoint.Y - 2, 6, 6);
167                    }
168                    lastCustomer = customer;
169                  }
170
171                  graphics.DrawPolygon(pens[((currentTour >= pens.Length) ? (pens.Length - 1) : (currentTour))], tourPoints);
172                  currentTour++;
173                }
174              }               
175            }
176          }
177
178          for (int i = 0; i < pens.Length; i++)
179            pens[i].Dispose();
180
181          pictureBox.Image = bitmap;
182        }
183      }
184    }
185
186    private void Content_QualityChanged(object sender, EventArgs e) {
187      if (InvokeRequired)
188        Invoke(new EventHandler(Content_QualityChanged), sender, e);
189      else
190        qualityViewHost.Content = Content.Quality;
191    }
192    void Content_DistanceChanged(object sender, EventArgs e) {
193      if (InvokeRequired)
194        Invoke(new EventHandler(Content_DistanceChanged), sender, e);
195      else
196        distanceViewHost.Content = Content.Distance;
197    }
198    private void Content_CoordinatesChanged(object sender, EventArgs e) {
199      if (InvokeRequired)
200        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
201      else
202        GenerateImage();
203    }
204    void Content_OverloadChanged(object sender, EventArgs e) {
205      if (InvokeRequired)
206        Invoke(new EventHandler(Content_OverloadChanged), sender, e);
207      else
208        overloadViewHost.Content = Content.Overload;
209    }
210    void Content_TardinessChanged(object sender, EventArgs e) {
211      if (InvokeRequired)
212        Invoke(new EventHandler(Content_TardinessChanged), sender, e);
213      else
214        tardinessViewHost.Content = Content.Tardiness;
215    }
216    void Content_TravelTimeChanged(object sender, EventArgs e) {
217      if (InvokeRequired)
218        Invoke(new EventHandler(Content_TravelTimeChanged), sender, e);
219      else
220        travelTimeViewHost.Content = Content.TravelTime;
221    }
222    void Content_VehicleUtilizationChanged(object sender, EventArgs e) {
223      if (InvokeRequired)
224        Invoke(new EventHandler(Content_VehicleUtilizationChanged), sender, e);
225      else
226        vehicleUtilizationViewHost.Content = Content.VehicleUtilization;
227    }
228    private void Content_SolutionChanged(object sender, EventArgs e) {
229      if (InvokeRequired)
230        Invoke(new EventHandler(Content_SolutionChanged), sender, e);
231      else {
232        GenerateImage();
233        tourViewHost.Content = Content.Solution;
234      }
235    }
236    private void pictureBox_SizeChanged(object sender, EventArgs e) {
237      GenerateImage();
238    }
239  }
240}
Note: See TracBrowser for help on using the repository browser.