Changeset 17248 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/TSPData.cs
- Timestamp:
- 09/11/19 21:35:59 (5 years ago)
- 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 22 using System; 2 23 using System.ComponentModel; 3 using System.Drawing;4 24 using HEAL.Attic; 5 25 using HeuristicLab.Common; … … 15 35 } 16 36 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 24 37 [Item("Matrix-based TSP Data", "TSP that is representd by a distance matrix.")] 25 38 [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) { 36 58 Matrix = original.Matrix; 37 DisplayCoordinates = cloner.Clone(original.DisplayCoordinates);59 displayCoordinates = cloner.Clone(original.displayCoordinates); 38 60 } 39 61 public MatrixTSPData() { 40 Matrix = new double[0, 0];62 Matrix = new DoubleMatrix(new double[0, 0], @readonly: true); 41 63 DisplayCoordinates = null; 42 64 } 43 65 public MatrixTSPData(double[,] matrix, double[,] coordinates = null) { 44 Matrix = (double[,])matrix.Clone();66 Matrix = new DoubleMatrix(matrix, @readonly: true); 45 67 if (coordinates != null) DisplayCoordinates = new DoubleMatrix(coordinates); 46 68 if (DisplayCoordinates != null && DisplayCoordinates.Columns != 2) 47 69 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) 49 71 throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + "."); 50 72 } 51 73 52 74 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)); 54 76 } 55 77 … … 59 81 60 82 public void SetMatrix(double[,] matrix, double[,] coordinates = null) { 61 Matrix = (double[,])matrix.Clone(); 83 Matrix = new DoubleMatrix(matrix, @readonly: true); 84 OnPropertyChanged(nameof(Matrix)); 62 85 if (coordinates == null) DisplayCoordinates = null; 63 86 else DisplayCoordinates = new DoubleMatrix(coordinates); 64 87 if (DisplayCoordinates != null && DisplayCoordinates.Columns != 2) 65 88 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) 67 90 throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + "."); 68 91 } 69 92 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)); 72 96 DisplayCoordinates = (DoubleMatrix)coordinates?.Clone(); 73 97 if (DisplayCoordinates != null && DisplayCoordinates.Columns != 2) 74 98 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) 76 100 throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + "."); 77 101 } 78 102 79 103 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 } 80 110 } 81 111 … … 108 138 109 139 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)); 111 141 } 112 142 } … … 181 211 } 182 212 } 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 }259 213 }
Note: See TracChangeset
for help on using the changeset viewer.