Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBInstanceProvider.cs @ 17514

Last change on this file since 17514 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 5.7 KB
RevLine 
[7618]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7618]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
[11932]25using System.IO.Compression;
[7618]26using System.Linq;
27using System.Reflection;
28using System.Text.RegularExpressions;
29
30namespace HeuristicLab.Problems.Instances.TSPLIB {
31  public abstract class TSPLIBInstanceProvider<T> : ProblemInstanceProvider<T> {
32
33    public override Uri WebLink {
34      get { return new Uri("http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/"); }
35    }
36
37    public override string ReferencePublication {
38      get {
39        return @"G. Reinelt. 1991.
40TSPLIB - A Traveling Salesman Problem Library.
41ORSA Journal on Computing, 3, pp. 376-384.";
42      }
43    }
44
45    protected abstract string FileExtension { get; }
46
[13486]47    protected abstract T LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null);
[7618]48    protected abstract void LoadSolution(TSPLIBParser parser, T instance);
[15259]49    protected abstract void LoadQuality(double? bestQuality, T instance);
[7618]50
51    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
52      Dictionary<string, string> solutions = new Dictionary<string, string>();
53      var solutionsArchiveName = GetResourceName(FileExtension + @"\.opt\.tour\.zip");
54      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
[11932]55        using (var solutionsZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName), ZipArchiveMode.Read)) {
56          foreach (var entry in solutionsZipFile.Entries)
57            solutions.Add(entry.Name.Substring(0, entry.Name.Length - ".opt.tour".Length) + "." + FileExtension, entry.Name);
[7618]58        }
59      }
[15259]60
61      Dictionary<string, double> qualities = new Dictionary<string, double>();
62      var qualitiesArchiveName = GetResourceName(FileExtension + @"\.qual");
63      if (!String.IsNullOrEmpty(qualitiesArchiveName)) {
64        using (var qualitiesReader = new StreamReader(GetType().Assembly.GetManifestResourceStream(qualitiesArchiveName))) {
65          while (!qualitiesReader.EndOfStream) {
66            var line = qualitiesReader.ReadLine().Split(';');
67            qualities.Add(line[0] + "." + FileExtension, double.Parse(line[1]));
68          }
69        }
70      }
71
[7618]72      var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
73      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
74
[11932]75      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
76        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
[15259]77          yield return new TSPLIBDataDescriptor(Path.GetFileNameWithoutExtension(entry),
78                                                GetInstanceDescription(),
79                                                entry,
80                                                solutions.ContainsKey(entry) ? solutions[entry] : String.Empty,
81                                                qualities.ContainsKey(entry) ? (double?)qualities[entry] : null);
[7618]82        }
83      }
84    }
85
86    public override T LoadData(IDataDescriptor id) {
87      var descriptor = (TSPLIBDataDescriptor)id;
88      var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
[11932]89      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
[7618]90        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
[11932]91        var stream = entry.Open();
[7618]92        var parser = new TSPLIBParser(stream);
[13486]93        var instance = LoadInstance(parser, id);
[15259]94        LoadQuality(descriptor.BestQuality, instance);
[7618]95
96        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
97          var solutionsArchiveName = GetResourceName(FileExtension + @"\.opt\.tour\.zip");
[11932]98          using (var solutionsZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName), ZipArchiveMode.Read)) {
[7618]99            entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier);
[11932]100            stream = entry.Open();
[7618]101            parser = new TSPLIBParser(stream);
102            LoadSolution(parser, instance);
103          }
104        }
105
106        return instance;
107      }
108    }
109
[8192]110    public override bool CanImportData {
111      get { return true; }
112    }
113    public override T ImportData(string path) {
[7618]114      return LoadInstance(new TSPLIBParser(path));
115    }
116
117    protected virtual string GetResourceName(string fileName) {
118      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
119              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
120    }
121
122    protected virtual string GetInstanceDescription() {
123      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.