Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 10.3 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.Count];
141                  int lastCustomer = 0;
142
143                  for (int i = 0; i < tour.Count; i++) {
144                    int customer = tour[i].Value;
145
146                    Point customerPoint = new Point(border + ((int)((coordinates[customer, 0] - xMin) * xStep)),
147                                    bitmap.Height - (border + ((int)((coordinates[customer, 1] - yMin) * yStep))));
148                    tourPoints[i] = customerPoint;
149
150                    if (i > 0) {
151                      Brush customerBrush = Brushes.Black;
152
153                      t += VehicleRoutingProblem.GetDistance(
154                        lastCustomer, customer, coordinates, distanceMatrix, useDistanceMatrix);
155
156                      if (t < readyTime[customer]) {
157                        t = readyTime[customer];
158                        customerBrush = Brushes.Yellow;
159                      } else if (t > dueTime[customer]) {
160                        customerBrush = Brushes.Red;
161                      }
162
163                      t += serviceTime[customer];
164
165                      graphics.FillRectangle(customerBrush, customerPoint.X - 2, customerPoint.Y - 2, 6, 6);
166                    }
167                    lastCustomer = customer;
168                  }
169
170                  graphics.DrawPolygon(pens[((currentTour >= pens.Length) ? (pens.Length - 1) : (currentTour))], tourPoints);
171                  currentTour++;
172                }
173              }
174            }
175          }
176
177          for (int i = 0; i < pens.Length; i++)
178            pens[i].Dispose();
179
180          pictureBox.Image = bitmap;
181        }
182      }
183    }
184
185    private void Content_QualityChanged(object sender, EventArgs e) {
186      if (InvokeRequired)
187        Invoke(new EventHandler(Content_QualityChanged), sender, e);
188      else
189        qualityViewHost.Content = Content.Quality;
190    }
191    void Content_DistanceChanged(object sender, EventArgs e) {
192      if (InvokeRequired)
193        Invoke(new EventHandler(Content_DistanceChanged), sender, e);
194      else
195        distanceViewHost.Content = Content.Distance;
196    }
197    private void Content_CoordinatesChanged(object sender, EventArgs e) {
198      if (InvokeRequired)
199        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
200      else
201        GenerateImage();
202    }
203    void Content_OverloadChanged(object sender, EventArgs e) {
204      if (InvokeRequired)
205        Invoke(new EventHandler(Content_OverloadChanged), sender, e);
206      else
207        overloadViewHost.Content = Content.Overload;
208    }
209    void Content_TardinessChanged(object sender, EventArgs e) {
210      if (InvokeRequired)
211        Invoke(new EventHandler(Content_TardinessChanged), sender, e);
212      else
213        tardinessViewHost.Content = Content.Tardiness;
214    }
215    void Content_TravelTimeChanged(object sender, EventArgs e) {
216      if (InvokeRequired)
217        Invoke(new EventHandler(Content_TravelTimeChanged), sender, e);
218      else
219        travelTimeViewHost.Content = Content.TravelTime;
220    }
221    void Content_VehicleUtilizationChanged(object sender, EventArgs e) {
222      if (InvokeRequired)
223        Invoke(new EventHandler(Content_VehicleUtilizationChanged), sender, e);
224      else
225        vehicleUtilizationViewHost.Content = Content.VehicleUtilization;
226    }
227    private void Content_SolutionChanged(object sender, EventArgs e) {
228      if (InvokeRequired)
229        Invoke(new EventHandler(Content_SolutionChanged), sender, e);
230      else {
231        GenerateImage();
232        tourViewHost.Content = Content.Solution;
233      }
234    }
235    private void pictureBox_SizeChanged(object sender, EventArgs e) {
236      GenerateImage();
237    }
238  }
239}
Note: See TracBrowser for help on using the repository browser.