Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.Orienteering/3.3/SchildeInstanceProvider.cs @ 17097

Last change on this file since 17097 was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

File size: 4.6 KB
RevLine 
[11260]1#region License Information
2/* HeuristicLab
[17097]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11260]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;
[12721]25using System.IO.Compression;
[11260]26using System.Linq;
27using System.Reflection;
28using System.Text.RegularExpressions;
29using HeuristicLab.Problems.Instances.Types;
30
31namespace HeuristicLab.Problems.Instances.Orienteering {
[11299]32  public class SchildeInstanceProvider : ProblemInstanceProvider<OPData> {
[11260]33    public override string Name {
[11299]34      get { return "Schilde (OP)"; }
[11260]35    }
36    public override string Description {
37      get { return "Modified Tsiligirdes (1984) and Chao (1996) instances by Schilde."; }
38    }
39    public override string ReferencePublication {
[11323]40      get {
41        return @"Michael Schilde, Karl F. Doerner, Richard F. Hartl, Guenter Kiechle. 2009.
42Metaheuristics for the bi-objective orienteering problem.
43Swarm Intelligence, Volume 3, Issue 3, pp 179-201.";
44      }
[11260]45    }
46    public override Uri WebLink {
47      get { return new Uri("http://prolog.univie.ac.at/research/OP/"); }
48    }
49
[11299]50    private const string FileName = "Schilde";
[11260]51
52    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
53      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
54      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
55
[12721]56      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
57        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
[11260]58          yield return new OPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetInstanceDescription(), entry);
59        }
60      }
61    }
62    public override OPData LoadData(IDataDescriptor id) {
63      var descriptor = (OPDataDescriptor)id;
64      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
[11299]65      var parser = new SchildeParser();
[12721]66      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
[11260]67        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
[12721]68        using (var stream = entry.Open()) {
[11260]69          parser.Parse(stream);
70          var instance = LoadInstance(parser);
71
72          instance.Name = id.Name;
73          instance.Description = id.Description;
74
75          return instance;
76        }
77      }
78    }
79
80    public override bool CanImportData {
81      get { return true; }
82    }
83    public override OPData ImportData(string path) {
[11299]84      var parser = new SchildeParser();
[11260]85      parser.Parse(path);
86      var instance = LoadInstance(parser);
87
88      instance.Name = Path.GetFileName(path);
89      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
90
91      return instance;
92    }
93
[11299]94    private OPData LoadInstance(SchildeParser parser) {
[11260]95      return new OPData {
96        Dimension = parser.Coordinates.GetLength(0),
97        Coordinates = parser.Coordinates,
98        Distances = parser.Distances,
[11299]99        DistanceMeasure = parser.Distances != null ? DistanceMeasure.Direct : DistanceMeasure.Euclidean,
[11260]100        MaximumDistance = parser.UpperBoundConstraint,
101        StartingPoint = parser.StartingPoint,
[11319]102        TerminalPoint = parser.TerminalPoint,
[11260]103        Scores = Enumerable.Range(0, parser.Scores.GetLength(0)).Select(i => parser.Scores[i, 0]).ToArray()
104      };
105    }
106
107    protected virtual string GetResourceName(string fileName) {
108      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
109        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
110    }
[12721]111
[11260]112    protected virtual string GetInstanceDescription() {
113      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.