Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering.Views/3.3/OrienteeringSolutionView.cs @ 11327

Last change on this file since 11327 was 11327, checked in by pfleck, 10 years ago

#2208

  • Added distance and penalty visualization for orienteering solution.
  • Added Evaluate method in IOrienteeringEvaluator for evaluation of new best solutions (still need some design improvements).
File size: 9.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Problems.Orienteering.Views {
30
31  [View("OrienteeringSolution View")]
32  [Content(typeof(OrienteeringSolution), true)]
33  public partial class OrienteeringSolutionView : ItemView {
34    public new OrienteeringSolution Content {
35      get { return (OrienteeringSolution)base.Content; }
36      set { base.Content = value; }
37    }
38    public OrienteeringSolutionView() {
39      InitializeComponent();
40    }
41
42    protected override void DeregisterContentEvents() {
43      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
44      Content.PenaltyChanged -= new EventHandler(Content_PenaltyChanged);
45      Content.DistanceChanged -= new EventHandler(Content_DistanceChanged);
46      Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
47      Content.StartingPointChanged -= new EventHandler(Content_StartingPointChanged);
48      Content.TerminalPointChanged -= new EventHandler(Content_TerminalPointChanged);
49      Content.ScoresChanged -= new EventHandler(Content_ScoresChanged);
50      Content.IntegerVectorChanged -= new EventHandler(Content_IntegerVectorChanged);
51      base.DeregisterContentEvents();
52    }
53    protected override void RegisterContentEvents() {
54      base.RegisterContentEvents();
55      Content.QualityChanged += new EventHandler(Content_QualityChanged);
56      Content.PenaltyChanged += new EventHandler(Content_PenaltyChanged);
57      Content.DistanceChanged += new EventHandler(Content_DistanceChanged);
58      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
59      Content.StartingPointChanged += new EventHandler(Content_StartingPointChanged);
60      Content.TerminalPointChanged += new EventHandler(Content_TerminalPointChanged);
61      Content.ScoresChanged += new EventHandler(Content_ScoresChanged);
62      Content.IntegerVectorChanged += new EventHandler(Content_IntegerVectorChanged);
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      if (Content == null) {
68        qualityViewHost.Content = null;
69        penaltyViewHost.Content = null;
70        distanceViewHost.Content = null;
71        pictureBox.Image = null;
72        tourViewHost.Content = null;
73      } else {
74        qualityViewHost.Content = Content.Quality;
75        penaltyViewHost.Content = Content.Penalty;
76        distanceViewHost.Content = Content.Distance;
77        GenerateImage();
78        tourViewHost.Content = Content.IntegerVector;
79      }
80    }
81
82    protected override void SetEnabledStateOfControls() {
83      base.SetEnabledStateOfControls();
84      qualityGroupBox.Enabled = Content != null;
85      penaltyGroupBox.Enabled = Content != null;
86      distanceGroupBox.Enabled = Content != null;
87      pictureBox.Enabled = Content != null;
88      tourGroupBox.Enabled = Content != null;
89    }
90
91    private void GenerateImage() {
92      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
93        if (Content == null) {
94          pictureBox.Image = null;
95        } else {
96          var coordinates = Content.Coordinates;
97          var scores = Content.Scores;
98          var integerVector = Content.IntegerVector;
99          var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
100
101          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
102            && (scores != null) && (coordinates.Rows == scores.Length)) {
103            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
104            for (int i = 0; i < coordinates.Rows; i++) {
105              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
106              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
107              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
108              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
109            }
110
111            int border = 20;
112            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
113            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
114
115            Point[] points = new Point[coordinates.Rows];
116            for (int i = 0; i < coordinates.Rows; i++)
117              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
118                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
119
120            using (Graphics graphics = Graphics.FromImage(bitmap)) {
121              if (integerVector != null && integerVector.Length > 1) {
122                Point[] tour = new Point[integerVector.Length];
123                for (int i = 0; i < integerVector.Length; i++) {
124                  tour[i] = points[integerVector[i]];
125                }
126                bool visualizePenalty = Content.Penalty != null && Content.Penalty.Value > 0;
127                graphics.DrawLines(visualizePenalty ? Pens.Red : Pens.Black, tour);
128              }
129
130              double scoreMin = scores.Min();
131              double scoreMax = scores.Max();
132              double scoreRange = scoreMax - scoreMin;
133              for (int i = 0; i < points.Length; i++) {
134                double score = scores[i];
135                int size = scoreRange.IsAlmost(0.0)
136                  ? 6
137                  : (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
138                graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
139              }
140              int startingPoint = Content.StartingPoint.Value;
141              int terminalPoint = Content.TerminalPoint.Value;
142              Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
143              var beginSize = graphics.MeasureString("Begin", font);
144              if (startingPoint >= 0 && startingPoint < points.Length)
145                graphics.DrawString("Begin", font, Brushes.Black, points[startingPoint].X - beginSize.Width, points[startingPoint].Y - beginSize.Height);
146              if (terminalPoint >= 0 && terminalPoint < points.Length)
147                graphics.DrawString("End", font, Brushes.Black, points[terminalPoint].X, points[terminalPoint].Y);
148            }
149          } else {
150            using (Graphics graphics = Graphics.FromImage(bitmap)) {
151              graphics.Clear(Color.White);
152              Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
153              string text = "No coordinates defined or in wrong format.";
154              SizeF strSize = graphics.MeasureString(text, font);
155              graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
156            }
157          }
158          pictureBox.Image = bitmap;
159        }
160      }
161    }
162
163    private void Content_QualityChanged(object sender, EventArgs e) {
164      if (InvokeRequired)
165        Invoke(new EventHandler(Content_QualityChanged), sender, e);
166      else
167        qualityViewHost.Content = Content.Quality;
168    }
169    private void Content_PenaltyChanged(object sender, EventArgs e) {
170      if (InvokeRequired)
171        Invoke(new EventHandler(Content_PenaltyChanged), sender, e);
172      else {
173        penaltyViewHost.Content = Content.Penalty;
174        GenerateImage();
175      }
176    }
177
178    private void Content_DistanceChanged(object sender, EventArgs e) {
179      if (InvokeRequired)
180        Invoke(new EventHandler(Content_DistanceChanged), sender, e);
181      else
182        distanceViewHost.Content = Content.Distance;
183    }
184    private void Content_CoordinatesChanged(object sender, EventArgs e) {
185      if (InvokeRequired)
186        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
187      else
188        GenerateImage();
189    }
190    private void Content_StartingPointChanged(object sender, EventArgs e) {
191      if (InvokeRequired)
192        Invoke(new EventHandler(Content_StartingPointChanged), sender, e);
193      else
194        GenerateImage();
195    }
196    private void Content_TerminalPointChanged(object sender, EventArgs e) {
197      if (InvokeRequired)
198        Invoke(new EventHandler(Content_TerminalPointChanged), sender, e);
199      else
200        GenerateImage();
201    }
202    private void Content_ScoresChanged(object sender, EventArgs e) {
203      if (InvokeRequired)
204        Invoke(new EventHandler(Content_ScoresChanged), sender, e);
205      else
206        GenerateImage();
207    }
208    private void Content_IntegerVectorChanged(object sender, EventArgs e) {
209      if (InvokeRequired)
210        Invoke(new EventHandler(Content_IntegerVectorChanged), sender, e);
211      else {
212        GenerateImage();
213        tourViewHost.Content = Content.IntegerVector;
214      }
215    }
216
217    private void pictureBox_SizeChanged(object sender, EventArgs e) {
218      GenerateImage();
219    }
220  }
221}
Note: See TracBrowser for help on using the repository browser.