Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/11/19 21:35:59 (5 years ago)
Author:
abeham
Message:

#2521: working on TSP refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/TSPData.cs

    r17241 r17248  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 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;
    223using System.ComponentModel;
    3 using System.Drawing;
    424using HEAL.Attic;
    525using HeuristicLab.Common;
     
    1535  }
    1636
    17   [StorableType("f08a63d9-0b83-4944-9251-42925baeb872")]
    18   public interface ITSPSolution : IItem {
    19     DoubleMatrix Coordinates { get; }
    20     Permutation Tour { get; }
    21     DoubleValue TourLength { get; }
    22   }
    23 
    2437  [Item("Matrix-based TSP Data", "TSP that is representd by a distance matrix.")]
    2538  [StorableType("4df58a35-679d-4414-b815-9450ae100823")]
    26   public class MatrixTSPData : Item, ITSPData {
    27     [Storable]
    28     private double[,] Matrix { get; set; }
    29 
    30     [Storable]
    31     public DoubleMatrix DisplayCoordinates { get; set; }
    32 
    33     [StorableConstructor]
    34     protected MatrixTSPData(StorableConstructorFlag _) : base(_) { }
    35     protected MatrixTSPData(MatrixTSPData original, Cloner cloner) : base(original, cloner) {
     39  public sealed class MatrixTSPData : Item, ITSPData, INotifyPropertyChanged {
     40
     41    [Storable]
     42    public DoubleMatrix Matrix { get; private set; }
     43
     44    [Storable]
     45    private DoubleMatrix displayCoordinates;
     46    public DoubleMatrix DisplayCoordinates {
     47      get => displayCoordinates;
     48      set {
     49        if (displayCoordinates == value) return;
     50        displayCoordinates = value;
     51        OnPropertyChanged(nameof(DisplayCoordinates));
     52      }
     53    }
     54
     55    [StorableConstructor]
     56    private MatrixTSPData(StorableConstructorFlag _) : base(_) { }
     57    private MatrixTSPData(MatrixTSPData original, Cloner cloner) : base(original, cloner) {
    3658      Matrix = original.Matrix;
    37       DisplayCoordinates = cloner.Clone(original.DisplayCoordinates);
     59      displayCoordinates = cloner.Clone(original.displayCoordinates);
    3860    }
    3961    public MatrixTSPData() {
    40       Matrix = new double[0, 0];
     62      Matrix = new DoubleMatrix(new double[0, 0], @readonly: true);
    4163      DisplayCoordinates = null;
    4264    }
    4365    public MatrixTSPData(double[,] matrix, double[,] coordinates = null) {
    44       Matrix = (double[,])matrix.Clone();
     66      Matrix = new DoubleMatrix(matrix, @readonly: true);
    4567      if (coordinates != null) DisplayCoordinates = new DoubleMatrix(coordinates);
    4668      if (DisplayCoordinates != null && DisplayCoordinates.Columns != 2)
    4769        throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates));
    48       if (DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.GetLength(0))
     70      if (DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.Rows)
    4971        throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + ".");
    5072    }
    5173
    5274    public ITSPSolution GetSolution(Permutation tour, double tourLength) {
    53       return new PathTSPTour(DisplayCoordinates, tour, new DoubleValue(tourLength));
     75      return new TSPSolution(DisplayCoordinates, tour, new DoubleValue(tourLength));
    5476    }
    5577
     
    5981
    6082    public void SetMatrix(double[,] matrix, double[,] coordinates = null) {
    61       Matrix = (double[,])matrix.Clone();
     83      Matrix = new DoubleMatrix(matrix, @readonly: true);
     84      OnPropertyChanged(nameof(Matrix));
    6285      if (coordinates == null) DisplayCoordinates = null;
    6386      else DisplayCoordinates = new DoubleMatrix(coordinates);
    6487      if (DisplayCoordinates != null && DisplayCoordinates.Columns != 2)
    6588        throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates));
    66       if (DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.GetLength(0))
     89      if (DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.Rows)
    6790        throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + ".");
    6891    }
    6992
    70     public void SetMatrix(ValueTypeMatrix<double> matrix, DoubleMatrix coordinates = null) {
    71       Matrix = matrix.CloneAsMatrix();
     93    public void SetMatrix(DoubleMatrix matrix, DoubleMatrix coordinates = null) {
     94      Matrix = (DoubleMatrix)matrix.AsReadOnly();
     95      OnPropertyChanged(nameof(Matrix));
    7296      DisplayCoordinates = (DoubleMatrix)coordinates?.Clone();
    7397      if (DisplayCoordinates != null && DisplayCoordinates.Columns != 2)
    7498        throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates));
    75       if (DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.GetLength(0))
     99      if (DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.Rows)
    76100        throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + ".");
    77101    }
    78102
    79103    public double GetDistance(int fromCity, int toCity) => Matrix[fromCity, toCity];
     104
     105
     106    public event PropertyChangedEventHandler PropertyChanged;
     107    private void OnPropertyChanged(string property) {
     108      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
     109    }
    80110  }
    81111
     
    108138
    109139    public ITSPSolution GetSolution(Permutation tour, double tourLength) {
    110       return new PathTSPTour(Coordinates, tour, new DoubleValue(tourLength));
     140      return new TSPSolution(Coordinates, tour, new DoubleValue(tourLength));
    111141    }
    112142  }
     
    181211    }
    182212  }
    183   /// <summary>
    184   /// Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.
    185   /// </summary>
    186   [Item("PathTSPTour", "Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.")]
    187   [StorableType("2CAE7C49-751B-4802-9025-62E2268E47AE")]
    188   public sealed class PathTSPTour : Item, ITSPSolution, INotifyPropertyChanged {
    189     public static new Image StaticItemImage {
    190       get { return HeuristicLab.Common.Resources.VSImageLibrary.Image; }
    191     }
    192 
    193     [Storable]
    194     private DoubleMatrix coordinates;
    195     public DoubleMatrix Coordinates {
    196       get { return coordinates; }
    197       set {
    198         if (coordinates == value) return;
    199         coordinates = value;
    200         OnPropertyChanged(nameof(Coordinates));
    201       }
    202     }
    203 
    204     [Storable(Name = "tour", OldName = "permutation")]
    205     private Permutation tour;
    206     public Permutation Tour {
    207       get { return tour; }
    208       set {
    209         if (tour == value) return;
    210         tour = value;
    211         OnPropertyChanged(nameof(Tour));
    212       }
    213     }
    214     [Storable(Name = "tourLength", OldName = "quality")]
    215     private DoubleValue tourLength;
    216     public DoubleValue TourLength {
    217       get { return tourLength; }
    218       set {
    219         if (tourLength == value) return;
    220         tourLength = value;
    221         OnPropertyChanged(nameof(TourLength));
    222       }
    223     }
    224 
    225     [StorableConstructor]
    226     private PathTSPTour(StorableConstructorFlag _) : base(_) { }
    227     private PathTSPTour(PathTSPTour original, Cloner cloner)
    228       : base(original, cloner) {
    229       this.coordinates = cloner.Clone(original.coordinates);
    230       this.tour = cloner.Clone(original.tour);
    231       this.tourLength = cloner.Clone(original.tourLength);
    232     }
    233     public PathTSPTour() : base() { }
    234     public PathTSPTour(DoubleMatrix coordinates)
    235       : base() {
    236       this.coordinates = coordinates;
    237     }
    238     public PathTSPTour(DoubleMatrix coordinates, Permutation permutation)
    239       : base() {
    240       this.coordinates = coordinates;
    241       this.tour = permutation;
    242     }
    243     public PathTSPTour(DoubleMatrix coordinates, Permutation permutation, DoubleValue quality)
    244       : base() {
    245       this.coordinates = coordinates;
    246       this.tour = permutation;
    247       this.tourLength = quality;
    248     }
    249 
    250     public override IDeepCloneable Clone(Cloner cloner) {
    251       return new PathTSPTour(this, cloner);
    252     }
    253 
    254     public event PropertyChangedEventHandler PropertyChanged;
    255     private void OnPropertyChanged(string property) {
    256       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    257     }
    258   }
    259213}
Note: See TracChangeset for help on using the changeset viewer.