Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/16/12 16:28:49 (12 years ago)
Author:
gkronber
Message:

merged r7609:7840 from trunk into time series branch

Location:
branches/HeuristicLab.TimeSeries
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries

  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs

    r7558 r7842  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.IO;
    2524using System.Linq;
    26 using System.Reflection;
    27 using System.Text.RegularExpressions;
    2825
    2926namespace HeuristicLab.Problems.Instances.TSPLIB {
    30   public class TSPLIBTSPInstanceProvider : ProblemInstanceProvider<TSPData> {
     27  public class TSPLIBTSPInstanceProvider : TSPLIBInstanceProvider<TSPData> {
    3128
    3229    public override string Name {
     
    3835    }
    3936
    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"; } }
    4338
    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) {
    10840      parser.Parse();
    10941      if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
     
    14072      instance.Description = parser.Comment
    14173        + Environment.NewLine + Environment.NewLine
    142         + GetDescription();
     74        + GetInstanceDescription();
    14375      return instance;
    14476    }
    14577
    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();
    14881    }
    14982
    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;
    15292    }
     93
    15394  }
    15495}
Note: See TracChangeset for help on using the changeset viewer.