Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/17/19 21:40:30 (5 years ago)
Author:
abeham
Message:

#2521: Refactored pTSP to use compositional pattern

Location:
branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP.Views/3.3
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP.Views/3.3/HeuristicLab.Problems.PTSP.Views-3.3.csproj

    r17320 r17335  
    172172  </ItemGroup>
    173173  <ItemGroup>
    174     <Compile Include="CoordinatesPTSPDataView.cs">
     174    <Compile Include="ProbabilisticTSPDataView.cs">
    175175      <SubType>UserControl</SubType>
    176176    </Compile>
    177     <Compile Include="CoordinatesPTSPDataView.Designer.cs">
    178       <DependentUpon>CoordinatesPTSPDataView.cs</DependentUpon>
    179     </Compile>
    180     <Compile Include="EuclideanPTSPDataView.cs">
    181       <SubType>UserControl</SubType>
    182     </Compile>
    183     <Compile Include="EuclideanPTSPDataView.Designer.cs">
    184       <DependentUpon>EuclideanPTSPDataView.cs</DependentUpon>
    185     </Compile>
    186     <Compile Include="MatrixPTSPDataView.cs">
    187       <SubType>UserControl</SubType>
    188     </Compile>
    189     <Compile Include="MatrixPTSPDataView.Designer.cs">
    190       <DependentUpon>MatrixPTSPDataView.cs</DependentUpon>
     177    <Compile Include="ProbabilisticTSPDataView.Designer.cs">
     178      <DependentUpon>ProbabilisticTSPDataView.cs</DependentUpon>
    191179    </Compile>
    192180    <Compile Include="ProbabilisticTSPSolutionView.cs">
     
    198186    <Compile Include="Plugin.cs" />
    199187    <Compile Include="Properties\AssemblyInfo.cs" />
     188    <Compile Include="ProbabilisticTSPVisualizer.cs" />
    200189  </ItemGroup>
    201190  <ItemGroup>
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP.Views/3.3/ProbabilisticTSPSolutionView.cs

    r17260 r17335  
    2222using System;
    2323using System.Drawing;
     24using HeuristicLab.Core.Views;
    2425using HeuristicLab.Data;
    2526using HeuristicLab.Encodings.PermutationEncoding;
     
    3132  /// The base class for visual representations of a path tour for a PTSP.
    3233  /// </summary>
    33   [View("PathPTSPTour View")]
     34  [View("pTSP Solution View")]
    3435  [Content(typeof(IProbabilisticTSPSolution), true)]
    35   public sealed partial class ProbabilisticTSPSolutionView : TSPSolutionView {
     36  public sealed partial class ProbabilisticTSPSolutionView : ItemView {
     37    public TSPVisualizer Visualizer { get; set; }
     38
    3639    public new IProbabilisticTSPSolution Content {
    3740      get { return (IProbabilisticTSPSolution)base.Content; }
     
    4447    public ProbabilisticTSPSolutionView() {
    4548      InitializeComponent();
    46     }
    47 
    48     protected override void GenerateImage() {
    49       if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
    50         if (Content == null) {
    51           pictureBox.Image = null;
    52         } else {
    53           DoubleMatrix coordinates = Content.Coordinates;
    54           Permutation permutation = Content.Tour;
    55           DoubleArray probabilities = Content.Probabilities;
    56           Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
    57 
    58           if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2) && (probabilities.Length == coordinates.Rows)) {
    59             double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
    60             for (int i = 0; i < coordinates.Rows; i++) {
    61               if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
    62               if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
    63               if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
    64               if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
    65             }
    66 
    67             int border = 20;
    68             double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
    69             double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
    70 
    71             Point[] points = new Point[coordinates.Rows];
    72             for (int i = 0; i < coordinates.Rows; i++)
    73               points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
    74                                     bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
    75 
    76             using (Graphics graphics = Graphics.FromImage(bitmap)) {
    77               if (permutation != null && permutation.Length > 1) {
    78                 Point[] tour = new Point[permutation.Length];
    79                 for (int i = 0; i < permutation.Length; i++) {
    80                   if (permutation[i] >= 0 && permutation[i] < points.Length)
    81                     tour[i] = points[permutation[i]];
    82                 }
    83                 graphics.DrawPolygon(Pens.Black, tour);
    84               }
    85               for (int i = 0; i < points.Length; i++)
    86                 graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, Convert.ToInt32(probabilities[i] * 20), Convert.ToInt32(probabilities[i] * 20));
    87             }
    88           } else {
    89             using (Graphics graphics = Graphics.FromImage(bitmap)) {
    90               graphics.Clear(Color.White);
    91               Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
    92               string text = "No coordinates defined or in wrong format.";
    93               SizeF strSize = graphics.MeasureString(text, font);
    94               graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
    95             }
    96           }
    97           pictureBox.Image = bitmap;
    98         }
    99       }
     49      Visualizer = new ProbabilisticTSPVisualizer();
    10050    }
    10151  }
Note: See TracChangeset for help on using the changeset viewer.