Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBInstanceProvider.cs @ 14458

Last change on this file since 14458 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
50    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
51      Dictionary<string, string> solutions = new Dictionary<string, string>();
52      var solutionsArchiveName = GetResourceName(FileExtension + @"\.opt\.tour\.zip");
53      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
54        using (var solutionsZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName), ZipArchiveMode.Read)) {
55          foreach (var entry in solutionsZipFile.Entries)
56            solutions.Add(entry.Name.Substring(0, entry.Name.Length - ".opt.tour".Length) + "." + FileExtension, entry.Name);
57        }
58      }
59      var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
60      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
61
62      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
63        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
64          yield return new TSPLIBDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetInstanceDescription(), entry, solutions.ContainsKey(entry) ? solutions[entry] : String.Empty);
65        }
66      }
67    }
68
69    public override T LoadData(IDataDescriptor id) {
70      var descriptor = (TSPLIBDataDescriptor)id;
71      var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
72      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
73        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
74        var stream = entry.Open();
75        var parser = new TSPLIBParser(stream);
76        var instance = LoadInstance(parser, id);
77
78        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
79          var solutionsArchiveName = GetResourceName(FileExtension + @"\.opt\.tour\.zip");
80          using (var solutionsZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName), ZipArchiveMode.Read)) {
81            entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier);
82            stream = entry.Open();
83            parser = new TSPLIBParser(stream);
84            LoadSolution(parser, instance);
85          }
86        }
87
88        return instance;
89      }
90    }
91
92    public override bool CanImportData {
93      get { return true; }
94    }
95    public override T ImportData(string path) {
96      return LoadInstance(new TSPLIBParser(path));
97    }
98
99    protected virtual string GetResourceName(string fileName) {
100      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
101              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
102    }
103
104    protected virtual string GetInstanceDescription() {
105      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.