[7618] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.IO;
|
---|
[11932] | 25 | using System.IO.Compression;
|
---|
[7618] | 26 | using System.Linq;
|
---|
| 27 | using System.Reflection;
|
---|
| 28 | using System.Text.RegularExpressions;
|
---|
| 29 |
|
---|
| 30 | namespace 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.
|
---|
| 40 | TSPLIB - A Traveling Salesman Problem Library.
|
---|
| 41 | ORSA 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)) {
|
---|
[11932] | 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);
|
---|
[7618] | 57 | }
|
---|
| 58 | }
|
---|
| 59 | var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
|
---|
| 60 | if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
|
---|
| 61 |
|
---|
[11932] | 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)) {
|
---|
[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");
|
---|
[11932] | 72 | using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
|
---|
[7618] | 73 | var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
|
---|
[11932] | 74 | var stream = entry.Open();
|
---|
[7618] | 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");
|
---|
[11932] | 80 | using (var solutionsZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName), ZipArchiveMode.Read)) {
|
---|
[7618] | 81 | entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier);
|
---|
[11932] | 82 | stream = entry.Open();
|
---|
[7618] | 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 | }
|
---|