Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9074 was 8192, checked in by sforsten, 12 years ago

#1782:

  • renamed CanSave to CanExportData and SaveData to ExportData
  • added the same functionality for importing problem instance as we implemented for exporting
  • some special changes had to be made in Problems.Instances.VehicleRouting
File size: 4.8 KB
RevLine 
[7618]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
26using System.Reflection;
27using System.Text.RegularExpressions;
28using ICSharpCode.SharpZipLib.Zip;
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);
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 ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
55          foreach (var entry in GetZipContents(solutionsZipFile))
[7622]56            solutions.Add(entry.Substring(0, entry.Length - ".opt.tour".Length) + "." + FileExtension, entry);
[7618]57        }
58      }
59      var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
60      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
61
62      using (var instanceStream = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
[7638]63        foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {
[7618]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 ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
73        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
74        var stream = instancesZipFile.GetInputStream(entry);
75        var parser = new TSPLIBParser(stream);
76        var instance = LoadInstance(parser);
77
78        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
79          var solutionsArchiveName = GetResourceName(FileExtension + @"\.opt\.tour\.zip");
80          using (var solutionsZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
81            entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier);
82            stream = solutionsZipFile.GetInputStream(entry);
83            parser = new TSPLIBParser(stream);
84            LoadSolution(parser, instance);
85          }
86        }
87
88        return instance;
89      }
90    }
91
[8192]92    public override bool CanImportData {
93      get { return true; }
94    }
95    public override T ImportData(string path) {
[7618]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    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
109      ZipEntry entry;
110      while ((entry = zipFile.GetNextEntry()) != null) {
111        yield return entry.Name;
112      }
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.