Changeset 17251
- Timestamp:
- 09/13/19 11:37:59 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 4 added
- 2 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/ValueTypeMatrix.cs
r17248 r17251 190 190 191 191 public virtual ValueTypeMatrix<T> AsReadOnly() { 192 ValueTypeMatrix<T> readOnlyValueTypeMatrix = (ValueTypeMatrix<T>)this.Clone(); 192 if (readOnly) return this; 193 var readOnlyValueTypeMatrix = (ValueTypeMatrix<T>)this.Clone(); 193 194 readOnlyValueTypeMatrix.readOnly = true; 194 195 return readOnlyValueTypeMatrix; -
branches/2521_ProblemRefactoring/HeuristicLab.Optimizer/3.3/StartPage.cs
r17241 r17251 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing; 24 25 using System.IO; 25 26 using System.Linq; … … 110 111 } 111 112 112 } catch {113 113 } finally { 114 114 OnAllSamplesLoaded(); … … 120 120 using (var stream = assembly.GetManifestResourceStream(name)) { 121 121 var serializer = new ProtoBufSerializer(); 122 var item = (NamedItem)serializer.Deserialize(stream, false); 122 NamedItem item; 123 try { 124 item = (NamedItem)serializer.Deserialize(stream, false); 125 } catch { 126 item = new ErrorMessage(name, "Sample failed to load!"); 127 } 123 128 OnSampleLoaded(item, group, 1.0 / count); 124 129 } … … 197 202 Properties.Settings.Default.Save(); 198 203 } 204 205 [Item("Error Message", "")] 206 class ErrorMessage : NamedItem { 207 public override Image ItemImage => Common.Resources.VSImageLibrary.Error; 208 209 protected ErrorMessage(ErrorMessage original, Cloner cloner) : base(original, cloner) { } 210 public ErrorMessage(string error, string description) : base(error, description) { } 211 212 public override IDeepCloneable Clone(Cloner cloner) { 213 return new ErrorMessage(this, cloner); 214 } 215 } 199 216 } 200 217 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs
r17226 r17251 96 96 } 97 97 98 public override bool CanExportData => true; 99 100 public override void ExportData(TSPData instance, string path) { 101 using (var writer = new StreamWriter(File.OpenWrite(path))) { 102 writer.WriteLine("NAME: " + instance.Name); 103 writer.WriteLine("TYPE: TSP"); 104 writer.WriteLine("COMMENT: " + instance.Description.Replace(Environment.NewLine, " \\\\ " )); 105 writer.WriteLine("DIMENSION: " + instance.Dimension); 106 107 if (instance.DistanceMeasure == DistanceMeasure.Euclidean) { 108 // TSPLIB only knows rounded euclidean distance 109 instance.Distances = instance.GetDistanceMatrix(); 110 instance.DistanceMeasure = DistanceMeasure.Direct; 111 } 112 113 if (instance.DistanceMeasure == DistanceMeasure.Direct) { 114 writer.WriteLine("EDGE_WEIGHT_TYPE: EXPLICIT"); 115 writer.WriteLine("EDGE_WEIGHT_FORMAT: UPPER_ROW"); 116 if (instance.Coordinates != null && instance.Coordinates.GetLength(1) == 2) { 117 writer.WriteLine("DISPLAY_DATA_TYPE: TWOD_DISPLAY"); 118 } 119 } else { 120 writer.Write("EDGE_WEIGHT_TYPE: "); 121 switch (instance.DistanceMeasure) { 122 case DistanceMeasure.RoundedEuclidean: 123 writer.WriteLine("EUC_2D"); 124 break; 125 case DistanceMeasure.UpperEuclidean: 126 writer.WriteLine("CEIL_2D"); 127 break; 128 case DistanceMeasure.Att: 129 writer.WriteLine("ATT"); 130 break; 131 case DistanceMeasure.Geo: 132 writer.WriteLine("GEO"); 133 break; 134 case DistanceMeasure.Manhattan: 135 writer.WriteLine("MAN_2D"); 136 break; 137 case DistanceMeasure.Maximum: 138 writer.WriteLine("MAX_2D"); 139 break; 140 default: throw new InvalidOperationException("Unknown distance measure: " + instance.DistanceMeasure); 141 } 142 writer.WriteLine("DISPLAY_DATA_TYPE: COORD_DISPLAY"); 143 } 144 145 if (instance.DistanceMeasure == DistanceMeasure.Direct) { 146 writer.WriteLine("EDGE_WEIGHT_SECTION"); 147 for (var i = 0; i < instance.Distances.GetLength(0) - 1; i++) { 148 for (var j = i + 1; j < instance.Distances.GetLength(1); j++) { 149 writer.Write(instance.Distances[i, j] + " "); 150 } 151 writer.WriteLine(); 152 } 153 } 154 155 if (instance.Coordinates != null && instance.Coordinates.GetLength(1) == 2) { 156 if (instance.DistanceMeasure == DistanceMeasure.Direct) 157 writer.WriteLine("DISPLAY_DATA_SECTION"); 158 else writer.WriteLine("NODE_COORD_SECTION"); 159 for (var i = 1; i <= instance.Coordinates.GetLength(0); i++) { 160 writer.WriteLine(i + " " + instance.Coordinates[i - 1, 0] + " " + instance.Coordinates[i - 1, 1]); 161 } 162 } 163 writer.WriteLine("EOF"); 164 writer.Flush(); 165 } 166 } 98 167 } 99 168 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman.Views/3.3/HeuristicLab.Problems.TravelingSalesman.Views-3.3.csproj
r17248 r17251 123 123 </ItemGroup> 124 124 <ItemGroup> 125 <Compile Include="CoordinatesTSPDataView.cs"> 126 <SubType>UserControl</SubType> 127 </Compile> 128 <Compile Include="CoordinatesTSPDataView.Designer.cs"> 129 <DependentUpon>CoordinatesTSPDataView.cs</DependentUpon> 130 </Compile> 131 <Compile Include="EuclideanTSPDataView.cs"> 132 <SubType>UserControl</SubType> 133 </Compile> 134 <Compile Include="EuclideanTSPDataView.Designer.cs"> 135 <DependentUpon>EuclideanTSPDataView.cs</DependentUpon> 136 </Compile> 125 137 <Compile Include="MatrixTSPDataView.cs"> 126 138 <SubType>UserControl</SubType> … … 235 247 <Private>False</Private> 236 248 </ProjectReference> 237 </ItemGroup>238 <ItemGroup>239 <EmbeddedResource Include="MatrixTSPDataView.resx">240 <DependentUpon>MatrixTSPDataView.cs</DependentUpon>241 </EmbeddedResource>242 <EmbeddedResource Include="TSPSolutionView.resx">243 <DependentUpon>TSPSolutionView.cs</DependentUpon>244 </EmbeddedResource>245 249 </ItemGroup> 246 250 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman.Views/3.3/MatrixTSPDataView.Designer.cs
r17248 r17251 26 26 this.tabControl = new System.Windows.Forms.TabControl(); 27 27 this.distancesTabPage = new System.Windows.Forms.TabPage(); 28 this. setMatrixButton = new System.Windows.Forms.Button();28 this.distanceMatrixView = new HeuristicLab.Data.Views.StringConvertibleMatrixView(); 29 29 this.coordinatesTabPage = new System.Windows.Forms.TabPage(); 30 this.distanceMatrixView = new HeuristicLab.Data.Views.StringConvertibleMatrixView();31 30 this.coordinatesSplitContainer = new System.Windows.Forms.SplitContainer(); 32 this.setCoordinatesButton = new System.Windows.Forms.Button();33 31 this.coordinatesMatrixView = new HeuristicLab.Data.Views.StringConvertibleMatrixView(); 34 32 this.coordinatesPictureBox = new System.Windows.Forms.PictureBox(); 33 ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit(); 35 34 this.tabControl.SuspendLayout(); 36 35 this.distancesTabPage.SuspendLayout(); … … 43 42 this.SuspendLayout(); 44 43 // 44 // nameTextBox 45 // 46 this.errorProvider.SetIconAlignment(this.nameTextBox, System.Windows.Forms.ErrorIconAlignment.MiddleLeft); 47 this.errorProvider.SetIconPadding(this.nameTextBox, 2); 48 this.nameTextBox.Size = new System.Drawing.Size(688, 20); 49 // 50 // infoLabel 51 // 52 this.infoLabel.Location = new System.Drawing.Point(752, 3); 53 // 45 54 // tabControl 46 55 // 56 this.tabControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 57 | System.Windows.Forms.AnchorStyles.Left) 58 | System.Windows.Forms.AnchorStyles.Right))); 47 59 this.tabControl.Controls.Add(this.distancesTabPage); 48 60 this.tabControl.Controls.Add(this.coordinatesTabPage); 49 this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill; 50 this.tabControl.Location = new System.Drawing.Point(0, 0); 61 this.tabControl.Location = new System.Drawing.Point(0, 26); 51 62 this.tabControl.Name = "tabControl"; 52 63 this.tabControl.SelectedIndex = 0; 53 this.tabControl.Size = new System.Drawing.Size(771, 5 96);64 this.tabControl.Size = new System.Drawing.Size(771, 570); 54 65 this.tabControl.TabIndex = 1; 55 66 // 56 67 // distancesTabPage 57 68 // 58 this.distancesTabPage.Controls.Add(this.setMatrixButton);59 69 this.distancesTabPage.Controls.Add(this.distanceMatrixView); 60 70 this.distancesTabPage.Location = new System.Drawing.Point(4, 22); 61 71 this.distancesTabPage.Name = "distancesTabPage"; 62 72 this.distancesTabPage.Padding = new System.Windows.Forms.Padding(3); 63 this.distancesTabPage.Size = new System.Drawing.Size(763, 5 70);73 this.distancesTabPage.Size = new System.Drawing.Size(763, 544); 64 74 this.distancesTabPage.TabIndex = 0; 65 75 this.distancesTabPage.Text = "Distances"; 66 76 this.distancesTabPage.UseVisualStyleBackColor = true; 67 77 // 68 // setMatrixButton78 // distanceMatrixView 69 79 // 70 this.setMatrixButton.Location = new System.Drawing.Point(6, 6); 71 this.setMatrixButton.Name = "setMatrixButton"; 72 this.setMatrixButton.Size = new System.Drawing.Size(26, 23); 73 this.setMatrixButton.TabIndex = 1; 74 this.setMatrixButton.Text = "Set"; 75 this.setMatrixButton.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText; 76 this.setMatrixButton.UseVisualStyleBackColor = true; 77 this.setMatrixButton.Click += new System.EventHandler(this.SetMatrixButton_Click); 80 this.distanceMatrixView.Caption = "StringConvertibleMatrix View"; 81 this.distanceMatrixView.Content = null; 82 this.distanceMatrixView.Dock = System.Windows.Forms.DockStyle.Fill; 83 this.distanceMatrixView.Location = new System.Drawing.Point(3, 3); 84 this.distanceMatrixView.Name = "distanceMatrixView"; 85 this.distanceMatrixView.ReadOnly = false; 86 this.distanceMatrixView.ShowRowsAndColumnsTextBox = true; 87 this.distanceMatrixView.ShowStatisticalInformation = true; 88 this.distanceMatrixView.Size = new System.Drawing.Size(757, 538); 89 this.distanceMatrixView.TabIndex = 0; 78 90 // 79 91 // coordinatesTabPage … … 88 100 this.coordinatesTabPage.UseVisualStyleBackColor = true; 89 101 // 90 // distanceMatrixView91 //92 this.distanceMatrixView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)93 | System.Windows.Forms.AnchorStyles.Left)94 | System.Windows.Forms.AnchorStyles.Right)));95 this.distanceMatrixView.Caption = "StringConvertibleMatrix View";96 this.distanceMatrixView.Content = null;97 this.distanceMatrixView.Location = new System.Drawing.Point(6, 35);98 this.distanceMatrixView.Name = "distanceMatrixView";99 this.distanceMatrixView.ReadOnly = false;100 this.distanceMatrixView.ShowRowsAndColumnsTextBox = true;101 this.distanceMatrixView.ShowStatisticalInformation = true;102 this.distanceMatrixView.Size = new System.Drawing.Size(751, 529);103 this.distanceMatrixView.TabIndex = 0;104 //105 102 // coordinatesSplitContainer 106 103 // … … 111 108 // coordinatesSplitContainer.Panel1 112 109 // 113 this.coordinatesSplitContainer.Panel1.Controls.Add(this.setCoordinatesButton);114 110 this.coordinatesSplitContainer.Panel1.Controls.Add(this.coordinatesMatrixView); 115 111 // … … 121 117 this.coordinatesSplitContainer.TabIndex = 0; 122 118 // 123 // setCoordinatesButton124 //125 this.setCoordinatesButton.Location = new System.Drawing.Point(3, 3);126 this.setCoordinatesButton.Name = "setCoordinatesButton";127 this.setCoordinatesButton.Size = new System.Drawing.Size(26, 23);128 this.setCoordinatesButton.TabIndex = 2;129 this.setCoordinatesButton.Text = "Set";130 this.setCoordinatesButton.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;131 this.setCoordinatesButton.UseVisualStyleBackColor = true;132 this.setCoordinatesButton.Click += new System.EventHandler(this.SetCoordinatesButton_Click);133 //134 119 // coordinatesMatrixView 135 120 // 136 this.coordinatesMatrixView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)137 | System.Windows.Forms.AnchorStyles.Left)138 | System.Windows.Forms.AnchorStyles.Right)));139 121 this.coordinatesMatrixView.Caption = "StringConvertibleMatrix View"; 140 122 this.coordinatesMatrixView.Content = null; 141 this.coordinatesMatrixView.Location = new System.Drawing.Point(0, 32); 123 this.coordinatesMatrixView.Dock = System.Windows.Forms.DockStyle.Fill; 124 this.coordinatesMatrixView.Location = new System.Drawing.Point(0, 0); 142 125 this.coordinatesMatrixView.Name = "coordinatesMatrixView"; 143 126 this.coordinatesMatrixView.ReadOnly = false; 144 127 this.coordinatesMatrixView.ShowRowsAndColumnsTextBox = true; 145 128 this.coordinatesMatrixView.ShowStatisticalInformation = true; 146 this.coordinatesMatrixView.Size = new System.Drawing.Size(2 49, 532);129 this.coordinatesMatrixView.Size = new System.Drawing.Size(252, 564); 147 130 this.coordinatesMatrixView.TabIndex = 1; 148 131 // … … 163 146 this.Name = "MatrixTSPDataView"; 164 147 this.Size = new System.Drawing.Size(771, 596); 148 this.Controls.SetChildIndex(this.tabControl, 0); 149 this.Controls.SetChildIndex(this.nameLabel, 0); 150 this.Controls.SetChildIndex(this.nameTextBox, 0); 151 this.Controls.SetChildIndex(this.infoLabel, 0); 152 ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).EndInit(); 165 153 this.tabControl.ResumeLayout(false); 166 154 this.distancesTabPage.ResumeLayout(false); … … 172 160 ((System.ComponentModel.ISupportInitialize)(this.coordinatesPictureBox)).EndInit(); 173 161 this.ResumeLayout(false); 162 this.PerformLayout(); 174 163 175 164 } … … 183 172 private Data.Views.StringConvertibleMatrixView coordinatesMatrixView; 184 173 private System.Windows.Forms.TabPage distancesTabPage; 185 private System.Windows.Forms.Button setMatrixButton;186 174 private System.Windows.Forms.PictureBox coordinatesPictureBox; 187 private System.Windows.Forms.Button setCoordinatesButton;188 175 } 189 176 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman.Views/3.3/MatrixTSPDataView.cs
r17248 r17251 20 20 #endregion 21 21 22 using System;23 using System.ComponentModel;24 22 using System.Drawing; 25 23 using System.Windows.Forms; 26 using HeuristicLab.Common.Resources;27 24 using HeuristicLab.Core.Views; 28 25 using HeuristicLab.Data; … … 33 30 [View("Matrix TSP Data View")] 34 31 [Content(typeof(MatrixTSPData), IsDefaultView = true)] 35 public partial class MatrixTSPDataView : ItemView {32 public partial class MatrixTSPDataView : NamedItemView { 36 33 37 34 public new MatrixTSPData Content { … … 42 39 public MatrixTSPDataView() { 43 40 InitializeComponent(); 44 setMatrixButton.Text = string.Empty;45 setMatrixButton.Image = VSImageLibrary.Edit;46 setCoordinatesButton.Text = string.Empty;47 setCoordinatesButton.Image = VSImageLibrary.Edit;48 41 } 49 50 #region Content Events51 protected override void RegisterContentEvents() {52 base.RegisterContentEvents();53 Content.PropertyChanged += ContentOnPropertyChanged;54 if (Content.DisplayCoordinates != null) {55 Content.DisplayCoordinates.Reset += ContentDisplayCoordinatesOnChanged;56 Content.DisplayCoordinates.ItemChanged += ContentDisplayCoordinatesOnChanged;57 }58 }59 60 protected override void DeregisterContentEvents() {61 base.DeregisterContentEvents();62 Content.PropertyChanged -= ContentOnPropertyChanged;63 if (Content.DisplayCoordinates != null) {64 Content.DisplayCoordinates.Reset -= ContentDisplayCoordinatesOnChanged;65 Content.DisplayCoordinates.ItemChanged -= ContentDisplayCoordinatesOnChanged;66 }67 }68 69 private void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) {70 switch (e.PropertyName) {71 case nameof(Content.Matrix):72 distanceMatrixView.Content = Content.Matrix;73 break;74 case nameof(Content.DisplayCoordinates):75 if (coordinatesMatrixView.Content != null) {76 coordinatesMatrixView.Content.Reset -= ContentDisplayCoordinatesOnChanged;77 coordinatesMatrixView.Content.ItemChanged -= ContentDisplayCoordinatesOnChanged;78 }79 if (Content.DisplayCoordinates != null) {80 Content.DisplayCoordinates.Reset += ContentDisplayCoordinatesOnChanged;81 Content.DisplayCoordinates.ItemChanged += ContentDisplayCoordinatesOnChanged;82 }83 coordinatesMatrixView.Content = Content.DisplayCoordinates;84 GenerateImage();85 break;86 }87 }88 89 private void ContentDisplayCoordinatesOnChanged(object sender, EventArgs e) {90 GenerateImage();91 }92 #endregion93 42 94 43 protected override void OnContentChanged() { … … 107 56 protected override void SetEnabledStateOfControls() { 108 57 base.SetEnabledStateOfControls(); 109 setMatrixButton.Enabled = !ReadOnly && !Locked && Content != null;110 setCoordinatesButton.Enabled = !ReadOnly && !Locked && Content != null;111 58 distanceMatrixView.Enabled = !ReadOnly && !Locked && Content != null; 112 59 coordinatesMatrixView.Enabled = !ReadOnly && !Locked && Content != null; … … 152 99 } 153 100 } 154 155 private void SetCoordinatesButton_Click(object sender, EventArgs e) {156 if (Content.DisplayCoordinates != null) {157 Content.DisplayCoordinates.Reset -= ContentDisplayCoordinatesOnChanged;158 Content.DisplayCoordinates.ItemChanged -= ContentDisplayCoordinatesOnChanged;159 }160 var items = Math.Max(Content.Matrix.Rows, 1);161 Content.DisplayCoordinates = new DoubleMatrix(items, 2);162 }163 164 private void SetMatrixButton_Click(object sender, EventArgs e) {165 MessageBox.Show("Not yet implemented");166 }167 101 } 168 102 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/TSP.cs
r17248 r17251 37 37 [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 100)] 38 38 [StorableType("8415476a-69de-45ad-95be-298ed7c97e84")] 39 public class TSP : PermutationProblem, IProblemInstanceConsumer<TSPData> {39 public class TSP : PermutationProblem, IProblemInstanceConsumer<TSPData>, IProblemInstanceExporter<TSPData> { 40 40 /// <summary> 41 41 /// This limit governs when a distance matrix is used. For all problems smaller than that, the distance matrix is … … 70 70 Parameters.Add(TSPDataParameter = new ValueParameter<ITSPData>("TSPData", "The main parameters of the TSP.")); 71 71 Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<ITSPSolution>("BestKnownSolution", "The best known solution.")); 72 73 TSPData = new EuclideanTSPData() { 74 Coordinates = new DoubleMatrix(new double[,] { 75 { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 }, 76 { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 }, 77 { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 }, 78 { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 } 79 }), 80 Rounding = EuclideanTSPData.RoundingMode.Midpoint 81 }; 72 73 TSPData = new EuclideanTSPData(); 82 74 83 75 InitializeOperators(); … … 144 136 || data.DistanceMeasure == DistanceMeasure.Maximum 145 137 || data.Dimension <= DistanceMatrixSizeLimit) { 146 TSPData = new MatrixTSPData(data. GetDistanceMatrix(), data.Coordinates);138 TSPData = new MatrixTSPData(data.Name, data.GetDistanceMatrix(), data.Coordinates) { Description = data.Description }; 147 139 } else if (data.DistanceMeasure == DistanceMeasure.Direct && data.Distances != null) { 148 TSPData = new MatrixTSPData(data. Distances, data.Coordinates);140 TSPData = new MatrixTSPData(data.Name, data.Distances, data.Coordinates) { Description = data.Description }; 149 141 } else { 150 142 switch (data.DistanceMeasure) { 151 143 case DistanceMeasure.Euclidean: 152 TSPData = new EuclideanTSPData(data. Coordinates) { Rounding = EuclideanTSPData.RoundingMode.None};144 TSPData = new EuclideanTSPData(data.Name, data.Coordinates, EuclideanTSPData.DistanceRounding.None) { Description = data.Description }; 153 145 break; 154 146 case DistanceMeasure.RoundedEuclidean: 155 TSPData = new EuclideanTSPData(data. Coordinates) { Rounding = EuclideanTSPData.RoundingMode.Midpoint};147 TSPData = new EuclideanTSPData(data.Name, data.Coordinates, EuclideanTSPData.DistanceRounding.Midpoint) { Description = data.Description }; 156 148 break; 157 149 case DistanceMeasure.UpperEuclidean: 158 TSPData = new EuclideanTSPData(data. Coordinates) { Rounding = EuclideanTSPData.RoundingMode.Ceiling};150 TSPData = new EuclideanTSPData(data.Name, data.Coordinates, EuclideanTSPData.DistanceRounding.Ceiling) { Description = data.Description }; 159 151 break; 160 152 case DistanceMeasure.Geo: 161 TSPData = new GeoTSPData(data. Coordinates);153 TSPData = new GeoTSPData(data.Name, data.Coordinates) { Description = data.Description }; 162 154 break; 163 155 default: … … 184 176 } 185 177 178 public TSPData Export() { 179 var instance = TSPData.Export(); 180 if (!double.IsNaN(BestKnownQuality)) 181 instance.BestKnownQuality = BestKnownQuality; 182 if (BestKnownSolution?.Tour != null) 183 instance.BestKnownTour = BestKnownSolution.Tour.ToArray(); 184 return instance; 185 } 186 186 187 protected override void OnEncodingChanged() { 187 188 base.OnEncodingChanged(); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/TSPData.cs
r17248 r17251 21 21 22 22 using System; 23 using System.ComponentModel;24 23 using HEAL.Attic; 25 24 using HeuristicLab.Common; … … 27 26 using HeuristicLab.Data; 28 27 using HeuristicLab.Encodings.PermutationEncoding; 28 using HeuristicLab.Problems.Instances; 29 29 30 30 namespace HeuristicLab.Problems.TravelingSalesman { 31 31 [StorableType("1a9bf60c-b6a6-4c95-8e99-5a2dec0ee892")] 32 public interface ITSPData : I Item {32 public interface ITSPData : INamedItem { 33 33 double GetDistance(int fromCity, int toCity); 34 34 ITSPSolution GetSolution(Permutation tspTour, double tourLength); 35 TSPData Export(); 35 36 } 36 37 37 38 [Item("Matrix-based TSP Data", "TSP that is representd by a distance matrix.")] 38 39 [StorableType("4df58a35-679d-4414-b815-9450ae100823")] 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 } 40 public sealed class MatrixTSPData : NamedItem, ITSPData { 41 [Storable] public DoubleMatrix Matrix { get; private set; } 42 [Storable] public DoubleMatrix DisplayCoordinates { get; private set; } 54 43 55 44 [StorableConstructor] … … 57 46 private MatrixTSPData(MatrixTSPData original, Cloner cloner) : base(original, cloner) { 58 47 Matrix = original.Matrix; 59 displayCoordinates = cloner.Clone(original.displayCoordinates);48 DisplayCoordinates = original.DisplayCoordinates; 60 49 } 61 50 public MatrixTSPData() { 62 Matrix = new DoubleMatrix(new double[0, 0], @readonly: true); 63 DisplayCoordinates = null; 64 } 65 public MatrixTSPData(double[,] matrix, double[,] coordinates = null) { 51 Name = TSPDefaultInstance.Name; 52 Matrix = TSPDefaultInstance.Distances; 53 DisplayCoordinates = TSPDefaultInstance.Coordinates; 54 } 55 public MatrixTSPData(string name, double[,] matrix, double[,] coordinates = null) { 56 if (coordinates != null && coordinates.GetLength(1) != 2) 57 throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates)); 58 if (coordinates != null && coordinates.GetLength(0) != matrix.GetLength(0)) 59 throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + "."); 60 Name = name; 66 61 Matrix = new DoubleMatrix(matrix, @readonly: true); 67 if (coordinates != null) DisplayCoordinates = new DoubleMatrix(coordinates); 68 if (DisplayCoordinates != null && DisplayCoordinates.Columns != 2) 62 if (coordinates != null) DisplayCoordinates = new DoubleMatrix(coordinates, @readonly: true); 63 } 64 public MatrixTSPData(string name, DoubleMatrix matrix, DoubleMatrix coordinates = null) { 65 if (coordinates != null && coordinates.Columns != 2) 69 66 throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates)); 70 if ( DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.Rows)67 if (coordinates != null && coordinates.Rows != matrix.Rows) 71 68 throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + "."); 69 Name = name; 70 Matrix = (DoubleMatrix)matrix.AsReadOnly(); 71 DisplayCoordinates = (DoubleMatrix)coordinates?.AsReadOnly(); 72 72 } 73 73 … … 80 80 } 81 81 82 public void SetMatrix(double[,] matrix, double[,] coordinates = null) {83 Matrix = new DoubleMatrix(matrix, @readonly: true);84 OnPropertyChanged(nameof(Matrix));85 if (coordinates == null) DisplayCoordinates = null;86 else DisplayCoordinates = new DoubleMatrix(coordinates);87 if (DisplayCoordinates != null && DisplayCoordinates.Columns != 2)88 throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates));89 if (DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.Rows)90 throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + ".");91 }92 93 public void SetMatrix(DoubleMatrix matrix, DoubleMatrix coordinates = null) {94 Matrix = (DoubleMatrix)matrix.AsReadOnly();95 OnPropertyChanged(nameof(Matrix));96 DisplayCoordinates = (DoubleMatrix)coordinates?.Clone();97 if (DisplayCoordinates != null && DisplayCoordinates.Columns != 2)98 throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates));99 if (DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.Rows)100 throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + ".");101 }102 103 82 public double GetDistance(int fromCity, int toCity) => Matrix[fromCity, toCity]; 104 83 105 106 public event PropertyChangedEventHandler PropertyChanged; 107 private void OnPropertyChanged(string property) { 108 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); 84 public TSPData Export() { 85 return new TSPData() { 86 Name = name, 87 Description = description, 88 Coordinates = DisplayCoordinates?.CloneAsMatrix(), 89 Dimension = Matrix.Rows, 90 DistanceMeasure = DistanceMeasure.Direct, 91 Distances = Matrix.CloneAsMatrix() 92 }; 109 93 } 110 94 } … … 112 96 [Item("Coordinates-based TSP Data", "TSP that is represented by coordinates of locations.")] 113 97 [StorableType("3955d07a-d43c-4a01-9505-d2effb1ea865")] 114 public abstract class CoordinatesTSPData : Item, ITSPData { 115 [Storable] 116 public DoubleMatrix Coordinates { get; set; } 98 public abstract class CoordinatesTSPData : NamedItem, ITSPData { 99 [Storable] public DoubleMatrix Coordinates { get; private set; } 117 100 118 101 [StorableConstructor] 119 102 protected CoordinatesTSPData(StorableConstructorFlag _) : base(_) { } 120 103 protected CoordinatesTSPData(CoordinatesTSPData original, Cloner cloner) : base(original, cloner) { 121 Coordinates = cloner.Clone(original.Coordinates);104 Coordinates = original.Coordinates; 122 105 } 123 106 protected CoordinatesTSPData() : base() { 124 Coordinates = new DoubleMatrix(); 125 } 126 protected CoordinatesTSPData(double[,] coordinates) : base() { 107 Name = TSPDefaultInstance.Name; 108 Coordinates = TSPDefaultInstance.Coordinates; 109 } 110 protected CoordinatesTSPData(string name, double[,] coordinates) : base() { 127 111 if (coordinates == null) throw new ArgumentNullException(nameof(coordinates)); 128 112 if (coordinates.GetLength(1) != 2) throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates)); 129 Coordinates = new DoubleMatrix(coordinates); 113 Name = name; 114 Coordinates = new DoubleMatrix(coordinates, @readonly: true); 115 } 116 protected CoordinatesTSPData(string name, DoubleMatrix coordinates) : base() { 117 if (coordinates == null) throw new ArgumentNullException(nameof(coordinates)); 118 if (coordinates.Columns != 2) throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates)); 119 Name = name; 120 Coordinates = (DoubleMatrix)coordinates.AsReadOnly(); 130 121 } 131 122 … … 140 131 return new TSPSolution(Coordinates, tour, new DoubleValue(tourLength)); 141 132 } 133 134 public abstract TSPData Export(); 142 135 } 143 136 … … 145 138 [StorableType("4bf58348-cd98-46c5-a4c0-55f486ca88b4")] 146 139 public sealed class EuclideanTSPData : CoordinatesTSPData { 147 public enum RoundingMode{ None, Midpoint, Ceiling }140 public enum DistanceRounding { None, Midpoint, Ceiling } 148 141 149 142 [Storable] 150 public RoundingMode Rounding { get;set; }143 public DistanceRounding Rounding { get; private set; } 151 144 152 145 [StorableConstructor] … … 155 148 Rounding = original.Rounding; 156 149 } 157 public EuclideanTSPData() : base() { } 158 public EuclideanTSPData(double[,] coordinates) : base(coordinates) { } 150 public EuclideanTSPData() 151 : base() { 152 Rounding = DistanceRounding.Midpoint; 153 } 154 public EuclideanTSPData(string name, double[,] coordinates, DistanceRounding rounding = DistanceRounding.None) 155 : base(name, coordinates) { 156 Rounding = rounding; 157 } 158 public EuclideanTSPData(string name, DoubleMatrix coordinates, DistanceRounding rounding = DistanceRounding.None) 159 : base(name, coordinates) { 160 Rounding = rounding; 161 } 159 162 160 163 public override IDeepCloneable Clone(Cloner cloner) { … … 165 168 var dist = Math.Sqrt((fromX - toX) * (fromX - toX) + (fromY - toY) * (fromY - toY)); 166 169 switch (Rounding) { 167 case RoundingMode.None: return dist;168 case RoundingMode.Midpoint: return Math.Round(dist);169 case RoundingMode.Ceiling: return Math.Ceiling(dist);170 case DistanceRounding.None: return dist; 171 case DistanceRounding.Midpoint: return Math.Round(dist); 172 case DistanceRounding.Ceiling: return Math.Ceiling(dist); 170 173 default: throw new InvalidOperationException("Unknown rounding mode " + Rounding); 171 174 } 175 } 176 177 public override TSPData Export() { 178 var data = new TSPData() { 179 Name = name, 180 Description = description, 181 Coordinates = Coordinates.CloneAsMatrix(), 182 Dimension = Coordinates.Rows 183 }; 184 switch (Rounding) { 185 case DistanceRounding.None: data.DistanceMeasure = DistanceMeasure.Euclidean; break; 186 case DistanceRounding.Midpoint: data.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break; 187 case DistanceRounding.Ceiling: data.DistanceMeasure = DistanceMeasure.UpperEuclidean; break; 188 } 189 return data; 172 190 } 173 191 } … … 183 201 private GeoTSPData(GeoTSPData original, Cloner cloner) : base(original, cloner) { } 184 202 public GeoTSPData() : base() { } 185 public GeoTSPData(double[,] coordinates) : base(coordinates) { } 203 public GeoTSPData(string name, double[,] coordinates) : base(name, coordinates) { } 204 public GeoTSPData(string name, DoubleMatrix coordinates) : base(name, coordinates) { } 186 205 187 206 public override IDeepCloneable Clone(Cloner cloner) { … … 210 229 return PI * (Math.Truncate(x) + 5.0 * (x - Math.Truncate(x)) / 3.0) / 180.0; 211 230 } 231 232 public override TSPData Export() { 233 return new TSPData() { 234 Name = name, 235 Description = description, 236 Coordinates = Coordinates.CloneAsMatrix(), 237 Dimension = Coordinates.Rows, 238 DistanceMeasure = DistanceMeasure.Geo 239 }; 240 } 241 } 242 243 internal static class TSPDefaultInstance { 244 internal static readonly DoubleMatrix Distances = new DoubleMatrix(new double[,] { 245 { 0, 100, 200, 300, 100, 141, 224, 316, 200, 224, 283, 361, 300, 316, 361, 424 }, 246 { 100, 0, 100, 200, 141, 100, 141, 224, 224, 200, 224, 283, 316, 300, 316, 361 }, 247 { 200, 100, 0, 100, 224, 141, 100, 141, 283, 224, 200, 224, 361, 316, 300, 316 }, 248 { 300, 200, 100, 0, 316, 224, 141, 100, 361, 283, 224, 200, 424, 361, 316, 300 }, 249 { 100, 141, 224, 316, 0, 100, 200, 300, 100, 141, 224, 316, 200, 224, 283, 361 }, 250 { 141, 100, 141, 224, 100, 0, 100, 200, 141, 100, 141, 224, 224, 200, 224, 283 }, 251 { 224, 141, 100, 141, 200, 100, 0, 100, 224, 141, 100, 141, 283, 224, 200, 224 }, 252 { 316, 224, 141, 100, 300, 200, 100, 0, 316, 224, 141, 100, 361, 283, 224, 200 }, 253 { 200, 224, 283, 361, 100, 141, 224, 316, 0, 100, 200, 300, 100, 141, 224, 316 }, 254 { 224, 200, 224, 283, 141, 100, 141, 224, 100, 0, 100, 200, 141, 100, 141, 224 }, 255 { 283, 224, 200, 224, 224, 141, 100, 141, 200, 100, 0, 100, 224, 141, 100, 141 }, 256 { 361, 283, 224, 200, 316, 224, 141, 100, 300, 200, 100, 0, 316, 224, 141, 100 }, 257 { 300, 316, 361, 424, 200, 224, 283, 361, 100, 141, 224, 316, 0, 100, 200, 300 }, 258 { 316, 300, 316, 361, 224, 200, 224, 283, 141, 100, 141, 224, 100, 0, 100, 200 }, 259 { 361, 316, 300, 316, 283, 224, 200, 224, 224, 141, 100, 141, 200, 100, 0, 100 }, 260 { 424, 361, 316, 300, 361, 283, 224, 200, 316, 224, 141, 100, 300, 200, 100, 0 }, 261 }, @readonly: true); 262 internal static readonly DoubleMatrix Coordinates = new DoubleMatrix(new double[,] { 263 { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 }, 264 { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 }, 265 { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 }, 266 { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 } 267 }, @readonly: true); 268 internal static readonly string Name = "HL TSP Default"; 212 269 } 213 270 }
Note: See TracChangeset
for help on using the changeset viewer.