Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/07/20 17:41:18 (4 years ago)
Author:
abeham
Message:

#2521: working on porting orienteering problem
Open points include:

  • Visualization of OrienteeringProblemData
  • Fix visualization of solution
  • Cleanup unused classes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Orienteering.Views/3.3/OrienteeringSolutionView.cs

    r17226 r17525  
    2020#endregion
    2121
    22 using System;
    23 using System.Drawing;
    24 using System.Linq;
    25 using HeuristicLab.Common;
    26 using HeuristicLab.Core.Views;
     22using System.ComponentModel;
    2723using HeuristicLab.MainForm;
     24using HeuristicLab.Problems.TravelingSalesman.Views;
    2825
    2926namespace HeuristicLab.Problems.Orienteering.Views {
    3027  [View("OrienteeringSolution View")]
    3128  [Content(typeof(OrienteeringSolution), true)]
    32   public partial class OrienteeringSolutionView : ItemView {
     29  public partial class OrienteeringSolutionView : TSPSolutionView {
     30    public new OrienteeringVisualizer Visualizer {
     31      get { return (OrienteeringVisualizer)base.Visualizer; }
     32      set { base.Visualizer = value; }
     33    }
    3334    public new OrienteeringSolution Content {
    3435      get { return (OrienteeringSolution)base.Content; }
     
    3738    public OrienteeringSolutionView() {
    3839      InitializeComponent();
     40      Visualizer = new OrienteeringVisualizer();
    3941    }
    4042
    4143    protected override void DeregisterContentEvents() {
    42       Content.QualityChanged -= Content_QualityChanged;
    43       Content.PenaltyChanged -= Content_PenaltyChanged;
    44       Content.DistanceChanged -= Content_DistanceChanged;
    45       Content.CoordinatesChanged -= Content_CoordinatesChanged;
    46       Content.StartingPointChanged -= Content_StartingPointChanged;
    47       Content.TerminalPointChanged -= Content_TerminalPointChanged;
    48       Content.ScoresChanged -= Content_ScoresChanged;
    49       Content.IntegerVectorChanged -= Content_IntegerVectorChanged;
     44      Content.PropertyChanged -= ContentOnPropertyChanged;
    5045      base.DeregisterContentEvents();
    5146    }
    5247    protected override void RegisterContentEvents() {
    5348      base.RegisterContentEvents();
    54       Content.QualityChanged += Content_QualityChanged;
    55       Content.PenaltyChanged += Content_PenaltyChanged;
    56       Content.DistanceChanged += Content_DistanceChanged;
    57       Content.CoordinatesChanged += Content_CoordinatesChanged;
    58       Content.StartingPointChanged += Content_StartingPointChanged;
    59       Content.TerminalPointChanged += Content_TerminalPointChanged;
    60       Content.ScoresChanged += Content_ScoresChanged;
    61       Content.IntegerVectorChanged += Content_IntegerVectorChanged;
     49      Content.PropertyChanged += ContentOnPropertyChanged;
     50    }
     51
     52    protected override void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
     53      base.ContentOnPropertyChanged(sender, e);
     54      switch (e.PropertyName) {
     55        case nameof(Content.Quality):
     56          qualityValueView.Content = Content.Quality;
     57          break;
     58        case nameof(Content.Score):
     59          scoreValueView.Content = Content.Score;
     60          break;
     61      }
    6262    }
    6363
    6464    protected override void OnContentChanged() {
    65       base.OnContentChanged();
    6665      if (Content == null) {
    6766        qualityValueView.Content = null;
    68         penaltyValueView.Content = null;
    69         distanceValueView.Content = null;
     67        scoreValueView.Content = null;
    7068        splitContainer.Panel1Collapsed = true;
    71         pictureBox.Image = null;
    72         tourViewHost.Content = null;
     69        Visualizer.Data = null;
     70        Visualizer.IsFeasible = false;
    7371      } else {
    7472        qualityValueView.Content = Content.Quality;
    75         penaltyValueView.Content = Content.Penalty;
    76         distanceValueView.Content = Content.Distance;
    77         SetPanelCollapsing();
    78         GenerateImage();
    79         tourViewHost.Content = Content.IntegerVector;
     73        scoreValueView.Content = Content.Score;
     74        Visualizer.Data = Content.OPData;
     75        Visualizer.IsFeasible = Content.TravelCosts.Value <= Content.OPData.MaximumTravelCosts;
    8076      }
     77      base.OnContentChanged();
    8178    }
    8279
    8380    protected override void SetEnabledStateOfControls() {
    8481      base.SetEnabledStateOfControls();
    85       qualityValueView.Enabled = Content != null;
    86       penaltyValueView.Enabled = Content != null;
    87       distanceValueView.Enabled = Content != null;
    88       pictureBox.Enabled = Content != null;
    89       tourGroupBox.Enabled = Content != null;
    90     }
    91 
    92     private void GenerateImage() {
    93       if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
    94         if (Content == null) {
    95           pictureBox.Image = null;
    96         } else {
    97           var coordinates = Content.Coordinates;
    98           var scores = Content.Scores;
    99           var integerVector = Content.IntegerVector;
    100           var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
    101 
    102           if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
    103             && (scores != null) && (coordinates.Rows == scores.Length)) {
    104             double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
    105             for (int i = 0; i < coordinates.Rows; i++) {
    106               if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
    107               if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
    108               if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
    109               if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
    110             }
    111 
    112             int border = 20;
    113             double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
    114             double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
    115 
    116             Point[] points = new Point[coordinates.Rows];
    117             for (int i = 0; i < coordinates.Rows; i++)
    118               points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
    119                                     bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
    120 
    121             using (Graphics graphics = Graphics.FromImage(bitmap)) {
    122               if (integerVector != null && integerVector.Length > 1) {
    123                 Point[] tour = new Point[integerVector.Length];
    124                 for (int i = 0; i < integerVector.Length; i++) {
    125                   tour[i] = points[integerVector[i]];
    126                 }
    127                 bool visualizePenalty = Content.Penalty != null && Content.Penalty.Value > 0;
    128                 graphics.DrawLines(visualizePenalty ? Pens.Red : Pens.Black, tour);
    129               }
    130 
    131               double scoreMin = scores.Min();
    132               double scoreMax = scores.Max();
    133               double scoreRange = scoreMax - scoreMin;
    134               for (int i = 0; i < points.Length; i++) {
    135                 double score = scores[i];
    136                 int size = scoreRange.IsAlmost(0.0)
    137                   ? 6
    138                   : (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
    139                 graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
    140               }
    141               int startingPoint = Content.StartingPoint.Value;
    142               int terminalPoint = Content.TerminalPoint.Value;
    143               Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
    144               var beginSize = graphics.MeasureString("Begin", font);
    145               if (startingPoint >= 0 && startingPoint < points.Length)
    146                 graphics.DrawString("Begin", font, Brushes.Black, points[startingPoint].X - beginSize.Width, points[startingPoint].Y - beginSize.Height);
    147               if (terminalPoint >= 0 && terminalPoint < points.Length)
    148                 graphics.DrawString("End", font, Brushes.Black, points[terminalPoint].X, points[terminalPoint].Y);
    149             }
    150           } else {
    151             using (Graphics graphics = Graphics.FromImage(bitmap)) {
    152               graphics.Clear(Color.White);
    153               Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
    154               string text = "No coordinates defined or in wrong format.";
    155               SizeF strSize = graphics.MeasureString(text, font);
    156               graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
    157             }
    158           }
    159           pictureBox.Image = bitmap;
    160         }
    161       }
    162     }
    163 
    164     private void SetPanelCollapsing() {
    165       splitContainer.Panel1Collapsed = qualityValueView.Content == null && penaltyValueView.Content == null && distanceValueView.Content == null;
    166     }
    167 
    168     private void Content_QualityChanged(object sender, EventArgs e) {
    169       if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_QualityChanged, sender, e);
    170       else {
    171         qualityValueView.Content = Content.Quality;
    172         SetPanelCollapsing();
    173       }
    174     }
    175     private void Content_PenaltyChanged(object sender, EventArgs e) {
    176       if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_PenaltyChanged, sender, e);
    177       else {
    178         penaltyValueView.Content = Content.Penalty;
    179         GenerateImage();
    180         SetPanelCollapsing();
    181       }
    182     }
    183 
    184     private void Content_DistanceChanged(object sender, EventArgs e) {
    185       if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_DistanceChanged, sender, e);
    186       else {
    187         distanceValueView.Content = Content.Distance;
    188         SetPanelCollapsing();
    189       }
    190     }
    191 
    192     private void Content_CoordinatesChanged(object sender, EventArgs e) {
    193       if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_CoordinatesChanged, sender, e);
    194       else GenerateImage();
    195     }
    196 
    197     private void Content_StartingPointChanged(object sender, EventArgs e) {
    198       if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_StartingPointChanged, sender, e);
    199       else GenerateImage();
    200     }
    201 
    202     private void Content_TerminalPointChanged(object sender, EventArgs e) {
    203       if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_TerminalPointChanged, sender, e);
    204       else GenerateImage();
    205     }
    206 
    207     private void Content_ScoresChanged(object sender, EventArgs e) {
    208       if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ScoresChanged, sender, e);
    209       else GenerateImage();
    210     }
    211 
    212     private void Content_IntegerVectorChanged(object sender, EventArgs e) {
    213       if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_IntegerVectorChanged, sender, e);
    214       else {
    215         GenerateImage();
    216         tourViewHost.Content = Content.IntegerVector;
    217       }
    218     }
    219 
    220     private void pictureBox_SizeChanged(object sender, EventArgs e) {
    221       GenerateImage();
     82      qualityValueView.Enabled = Content != null && !ReadOnly && !Locked;
     83      scoreValueView.Enabled = Content != null && !ReadOnly && !Locked;
    22284    }
    22385  }
Note: See TracChangeset for help on using the changeset viewer.