Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2870_AutoDiff-nuget/HeuristicLab.Problems.Instances.Orienteering/3.3/SchildeInstanceProvider.cs @ 17047

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

#2640: Updated year of copyrights in license headers

File size: 4.6 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;
29using HeuristicLab.Problems.Instances.Types;
30
31namespace HeuristicLab.Problems.Instances.Orienteering {
32  public class SchildeInstanceProvider : ProblemInstanceProvider<OPData> {
33    public override string Name {
34      get { return "Schilde (OP)"; }
35    }
36    public override string Description {
37      get { return "Modified Tsiligirdes (1984) and Chao (1996) instances by Schilde."; }
38    }
39    public override string ReferencePublication {
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      }
45    }
46    public override Uri WebLink {
47      get { return new Uri("http://prolog.univie.ac.at/research/OP/"); }
48    }
49
50    private const string FileName = "Schilde";
51
52    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
53      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
54      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
55
56      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
57        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
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");
65      var parser = new SchildeParser();
66      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
67        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
68        using (var stream = entry.Open()) {
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) {
84      var parser = new SchildeParser();
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
94    private OPData LoadInstance(SchildeParser parser) {
95      return new OPData {
96        Dimension = parser.Coordinates.GetLength(0),
97        Coordinates = parser.Coordinates,
98        Distances = parser.Distances,
99        DistanceMeasure = parser.Distances != null ? DistanceMeasure.Direct : DistanceMeasure.Euclidean,
100        MaximumDistance = parser.UpperBoundConstraint,
101        StartingPoint = parser.StartingPoint,
102        TerminalPoint = parser.TerminalPoint,
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    }
111
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.