Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2877_HiveImprovements/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBInstanceProvider.cs @ 15870

Last change on this file since 15870 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
25using System.IO.Compression;
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
47    protected abstract T LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null);
48    protected abstract void LoadSolution(TSPLIBParser parser, T instance);
49    protected abstract void LoadQuality(double? bestQuality, T instance);
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)) {
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);
58        }
59      }
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
72      var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
73      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
74
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)) {
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);
82        }
83      }
84    }
85
86    public override T LoadData(IDataDescriptor id) {
87      var descriptor = (TSPLIBDataDescriptor)id;
88      var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
89      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
90        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
91        var stream = entry.Open();
92        var parser = new TSPLIBParser(stream);
93        var instance = LoadInstance(parser, id);
94        LoadQuality(descriptor.BestQuality, instance);
95
96        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
97          var solutionsArchiveName = GetResourceName(FileExtension + @"\.opt\.tour\.zip");
98          using (var solutionsZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName), ZipArchiveMode.Read)) {
99            entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier);
100            stream = entry.Open();
101            parser = new TSPLIBParser(stream);
102            LoadSolution(parser, instance);
103          }
104        }
105
106        return instance;
107      }
108    }
109
110    public override bool CanImportData {
111      get { return true; }
112    }
113    public override T ImportData(string path) {
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.