Changeset 13470 for branches/PTSP/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBHomogeneousPTSPInstanceProvider.cs
- Timestamp:
- 12/15/15 16:38:08 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PTSP/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBHomogeneousPTSPInstanceProvider.cs
r13412 r13470 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using System.Globalization; 25 using System.IO;26 24 using System.Linq; 27 25 28 26 namespace HeuristicLab.Problems.Instances.TSPLIB { 29 public class TSPLIBHomogeneousPTSPInstanceProvider : TSPLIB InstanceProvider<PTSPData>{30 private static readonly double[] probabilities = new[] { 0.1, 0.2, 0.4, 0.5, 0.6, 0.8, 0.9, 1.0 };27 public class TSPLIBHomogeneousPTSPInstanceProvider : TSPLIBPTSPInstanceProvider { 28 private static readonly double[] Probabilities = new[] { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 }; 31 29 32 30 public override string Name { … … 34 32 } 35 33 36 public override string Description {37 get { return "Traveling Salesman Problem Library"; }38 }39 40 protected override string FileExtension { get { return "tsp"; } }41 42 34 public override IEnumerable<IDataDescriptor> GetDataDescriptors() { 43 35 var tspDescriptors = base.GetDataDescriptors().OfType<TSPLIBDataDescriptor>(); 44 36 foreach (var desc in tspDescriptors) { 45 foreach (var p in probabilities) {37 foreach (var p in Probabilities) { 46 38 yield return new TSPLIBHomogeneousPTSPDataDescriptor( 47 39 desc.Name + "-" + p.ToString(CultureInfo.InvariantCulture.NumberFormat), … … 54 46 } 55 47 56 protected override PTSPData LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null) { 57 parser.Parse(); 58 if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab."); 59 var instance = new PTSPData(); 60 instance.Dimension = parser.Dimension; 61 instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices; 62 instance.Distances = parser.Distances; 63 switch (parser.EdgeWeightType) { 64 case TSPLIBEdgeWeightTypes.ATT: 65 instance.DistanceMeasure = DistanceMeasure.Att; break; 66 case TSPLIBEdgeWeightTypes.CEIL_2D: 67 instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break; 68 case TSPLIBEdgeWeightTypes.EUC_2D: 69 instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break; 70 case TSPLIBEdgeWeightTypes.EUC_3D: 71 throw new InvalidDataException("3D coordinates are not supported."); 72 case TSPLIBEdgeWeightTypes.EXPLICIT: 73 instance.DistanceMeasure = DistanceMeasure.Direct; break; 74 case TSPLIBEdgeWeightTypes.GEO: 75 instance.DistanceMeasure = DistanceMeasure.Geo; break; 76 case TSPLIBEdgeWeightTypes.MAN_2D: 77 instance.DistanceMeasure = DistanceMeasure.Manhattan; break; 78 case TSPLIBEdgeWeightTypes.MAN_3D: 79 throw new InvalidDataException("3D coordinates are not supported."); 80 case TSPLIBEdgeWeightTypes.MAX_2D: 81 instance.DistanceMeasure = DistanceMeasure.Maximum; break; 82 case TSPLIBEdgeWeightTypes.MAX_3D: 83 throw new InvalidDataException("3D coordinates are not supported."); 84 default: 85 throw new InvalidDataException("The given edge weight is not supported by HeuristicLab."); 86 } 87 88 instance.Name = parser.Name; 89 instance.Description = parser.Comment 90 + Environment.NewLine + Environment.NewLine 91 + GetInstanceDescription(); 48 protected override double[] GetProbabilities(IDataDescriptor descriptor, PTSPData instance) { 92 49 var ptspDesc = descriptor as TSPLIBHomogeneousPTSPDataDescriptor; 93 instance.Probabilities = ptspDesc != null ? Enumerable.Range(0, instance.Dimension).Select(_ => ptspDesc.Probability).ToArray() 94 : Enumerable.Range(0, instance.Dimension).Select(_ => 0.5).ToArray(); 95 96 return instance; 50 return ptspDesc != null ? Enumerable.Range(0, instance.Dimension).Select(_ => ptspDesc.Probability).ToArray() 51 : Enumerable.Range(0, instance.Dimension).Select(_ => 0.5).ToArray(); 97 52 } 98 99 protected override void LoadSolution(TSPLIBParser parser, PTSPData instance) {100 parser.Parse();101 instance.BestKnownTour = parser.Tour.FirstOrDefault();102 }103 104 public PTSPData LoadData(string tspFile, string tourFile, double? bestQuality) {105 var data = LoadInstance(new TSPLIBParser(tspFile));106 if (!String.IsNullOrEmpty(tourFile)) {107 var tourParser = new TSPLIBParser(tourFile);108 LoadSolution(tourParser, data);109 }110 if (bestQuality.HasValue)111 data.BestKnownQuality = bestQuality.Value;112 return data;113 }114 115 53 } 116 54 }
Note: See TracChangeset
for help on using the changeset viewer.