Changeset 7538 for branches/GeneralizedQAP/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs
- Timestamp:
- 03/01/12 15:15:22 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs
r7482 r7538 28 28 29 29 namespace HeuristicLab.Problems.Instances.TSPLIB { 30 public class TSPLIBTSPInstanceProvider : ProblemInstanceProvider< ITSPInstance> {30 public class TSPLIBTSPInstanceProvider : ProblemInstanceProvider<TSPInstance> { 31 31 32 32 public override string Name { … … 65 65 } 66 66 67 public override ITSPInstance GetInstance(IInstanceDescriptor id) {67 public override TSPInstance LoadInstance(IInstanceDescriptor id) { 68 68 var descriptor = (TSPLIBInstanceDescriptor)id; 69 var instance = new TSPLIBTSPInstance();70 69 using (var stream = Assembly.GetExecutingAssembly() 71 70 .GetManifestResourceStream(descriptor.InstanceIdentifier)) { 72 71 var parser = new TSPLIBParser(stream); 73 parser.Parse(); 74 if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab."); 75 76 instance.Dimension = parser.Dimension; 77 instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices; 78 instance.Distances = parser.Distances; 79 switch (parser.EdgeWeightType) { 80 case TSPLIBEdgeWeightTypes.ATT: 81 instance.DistanceMeasure = TSPDistanceMeasure.Att; break; 82 case TSPLIBEdgeWeightTypes.CEIL_2D: 83 instance.DistanceMeasure = TSPDistanceMeasure.UpperEuclidean; break; 84 case TSPLIBEdgeWeightTypes.EUC_2D: 85 instance.DistanceMeasure = TSPDistanceMeasure.RoundedEuclidean; break; 86 case TSPLIBEdgeWeightTypes.EUC_3D: 87 throw new InvalidDataException("3D coordinates are not supported."); 88 case TSPLIBEdgeWeightTypes.EXPLICIT: 89 instance.DistanceMeasure = TSPDistanceMeasure.Direct; break; 90 case TSPLIBEdgeWeightTypes.GEO: 91 instance.DistanceMeasure = TSPDistanceMeasure.Geo; break; 92 case TSPLIBEdgeWeightTypes.MAN_2D: 93 instance.DistanceMeasure = TSPDistanceMeasure.Manhattan; break; 94 case TSPLIBEdgeWeightTypes.MAN_3D: 95 throw new InvalidDataException("3D coordinates are not supported."); 96 case TSPLIBEdgeWeightTypes.MAX_2D: 97 instance.DistanceMeasure = TSPDistanceMeasure.Maximum; break; 98 case TSPLIBEdgeWeightTypes.MAX_3D: 99 throw new InvalidDataException("3D coordinates are not supported."); 100 default: 101 throw new InvalidDataException("The given edge weight is not supported by HeuristicLab."); 102 } 103 104 instance.Name = parser.Name; 105 instance.Description = parser.Comment 106 + Environment.NewLine + Environment.NewLine 107 + GetDescription(); 72 var instance = Load(parser); 108 73 109 74 if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) { … … 115 80 } 116 81 } 82 return instance; 117 83 } 84 } 85 86 public override TSPInstance LoadInstance(string path) { 87 return Load(new TSPLIBParser(path)); 88 } 89 90 public override void SaveInstance(TSPInstance instance, string path) { 91 throw new NotSupportedException(); 92 } 93 94 private TSPInstance Load(TSPLIBParser parser) { 95 parser.Parse(); 96 if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab."); 97 var instance = new TSPInstance(); 98 instance.Dimension = parser.Dimension; 99 instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices; 100 instance.Distances = parser.Distances; 101 switch (parser.EdgeWeightType) { 102 case TSPLIBEdgeWeightTypes.ATT: 103 instance.DistanceMeasure = TSPDistanceMeasure.Att; break; 104 case TSPLIBEdgeWeightTypes.CEIL_2D: 105 instance.DistanceMeasure = TSPDistanceMeasure.UpperEuclidean; break; 106 case TSPLIBEdgeWeightTypes.EUC_2D: 107 instance.DistanceMeasure = TSPDistanceMeasure.RoundedEuclidean; break; 108 case TSPLIBEdgeWeightTypes.EUC_3D: 109 throw new InvalidDataException("3D coordinates are not supported."); 110 case TSPLIBEdgeWeightTypes.EXPLICIT: 111 instance.DistanceMeasure = TSPDistanceMeasure.Direct; break; 112 case TSPLIBEdgeWeightTypes.GEO: 113 instance.DistanceMeasure = TSPDistanceMeasure.Geo; break; 114 case TSPLIBEdgeWeightTypes.MAN_2D: 115 instance.DistanceMeasure = TSPDistanceMeasure.Manhattan; break; 116 case TSPLIBEdgeWeightTypes.MAN_3D: 117 throw new InvalidDataException("3D coordinates are not supported."); 118 case TSPLIBEdgeWeightTypes.MAX_2D: 119 instance.DistanceMeasure = TSPDistanceMeasure.Maximum; break; 120 case TSPLIBEdgeWeightTypes.MAX_3D: 121 throw new InvalidDataException("3D coordinates are not supported."); 122 default: 123 throw new InvalidDataException("The given edge weight is not supported by HeuristicLab."); 124 } 125 126 instance.Name = parser.Name; 127 instance.Description = parser.Comment 128 + Environment.NewLine + Environment.NewLine 129 + GetDescription(); 118 130 return instance; 119 131 }
Note: See TracChangeset
for help on using the changeset viewer.