Changeset 7842 for branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs
- Timestamp:
- 05/16/12 16:28:49 (12 years ago)
- Location:
- branches/HeuristicLab.TimeSeries
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries
- Property svn:mergeinfo changed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs
r7558 r7842 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.IO; 25 24 using System.Linq; 26 using System.Reflection;27 using System.Text.RegularExpressions;28 25 29 26 namespace HeuristicLab.Problems.Instances.TSPLIB { 30 public class TSPLIBTSPInstanceProvider : ProblemInstanceProvider<TSPData> {27 public class TSPLIBTSPInstanceProvider : TSPLIBInstanceProvider<TSPData> { 31 28 32 29 public override string Name { … … 38 35 } 39 36 40 public override Uri WebLink { 41 get { return new Uri("http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/"); } 42 } 37 protected override string FileExtension { get { return "tsp"; } } 43 38 44 public override string ReferencePublication { 45 get { 46 return @"G. Reinelt. 1991. 47 TSPLIB - A Traveling Salesman Problem Library. 48 ORSA Journal on Computing, 3, pp. 376-384."; 49 } 50 } 51 52 public override IEnumerable<IDataDescriptor> GetDataDescriptors() { 53 var solutions = Assembly.GetExecutingAssembly() 54 .GetManifestResourceNames() 55 .Where(x => Regex.Match(x, @".*\.Data\.TSP\..*").Success) 56 .Where(x => x.EndsWith(".opt.tour")) 57 .ToDictionary(x => x.Substring(0, x.Length - ".opt.tour".Length) + ".tsp", x => x); 58 59 return Assembly.GetExecutingAssembly() 60 .GetManifestResourceNames() 61 .Where(x => Regex.Match(x, @".*\.Data\.TSP\..*").Success) 62 .Where(x => x.EndsWith(".tsp")) 63 .OrderBy(x => x) 64 .Select(x => new TSPLIBDataDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty)); 65 } 66 67 public override TSPData LoadData(IDataDescriptor id) { 68 var descriptor = (TSPLIBDataDescriptor)id; 69 using (var stream = Assembly.GetExecutingAssembly() 70 .GetManifestResourceStream(descriptor.InstanceIdentifier)) { 71 var parser = new TSPLIBParser(stream); 72 var instance = Load(parser); 73 74 if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) { 75 using (Stream solStream = Assembly.GetExecutingAssembly() 76 .GetManifestResourceStream(descriptor.SolutionIdentifier)) { 77 var tourParser = new TSPLIBParser(solStream); 78 tourParser.Parse(); 79 instance.BestKnownTour = tourParser.Tour[0]; 80 } 81 } 82 return instance; 83 } 84 } 85 86 public override TSPData LoadData(string path) { 87 return Load(new TSPLIBParser(path)); 88 } 89 90 public TSPData LoadData(string tspFile, string tourFile, double? bestQuality) { 91 var data = Load(new TSPLIBParser(tspFile)); 92 if (bestQuality.HasValue) 93 data.BestKnownQuality = bestQuality.Value; 94 else data.BestKnownQuality = null; 95 if (!String.IsNullOrEmpty(tourFile)) { 96 var tourParser = new TSPLIBParser(tourFile); 97 tourParser.Parse(); 98 data.BestKnownTour = tourParser.Tour[0]; 99 } 100 return data; 101 } 102 103 public override void SaveData(TSPData instance, string path) { 104 throw new NotSupportedException(); 105 } 106 107 private TSPData Load(TSPLIBParser parser) { 39 protected override TSPData LoadInstance(TSPLIBParser parser) { 108 40 parser.Parse(); 109 41 if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab."); … … 140 72 instance.Description = parser.Comment 141 73 + Environment.NewLine + Environment.NewLine 142 + Get Description();74 + GetInstanceDescription(); 143 75 return instance; 144 76 } 145 77 146 private string GetPrettyName(string instanceIdentifier) { 147 return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.TSP\.(.*)\.tsp").Groups[1].Captures[0].Value; 78 protected override void LoadSolution(TSPLIBParser parser, TSPData instance) { 79 parser.Parse(); 80 instance.BestKnownTour = parser.Tour.FirstOrDefault(); 148 81 } 149 82 150 private string GetDescription() { 151 return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + "."; 83 public TSPData LoadData(string tspFile, string tourFile, double? bestQuality) { 84 var data = LoadInstance(new TSPLIBParser(tspFile)); 85 if (!String.IsNullOrEmpty(tourFile)) { 86 var tourParser = new TSPLIBParser(tourFile); 87 LoadSolution(tourParser, data); 88 } 89 if (bestQuality.HasValue) 90 data.BestKnownQuality = bestQuality.Value; 91 return data; 152 92 } 93 153 94 } 154 95 }
Note: See TracChangeset
for help on using the changeset viewer.