Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Instances.Orienteering/3.3/SchildeOPInstanceProvider.cs @ 11260

Last change on this file since 11260 was 11260, checked in by pfleck, 10 years ago

#2208

  • Added OPData
  • Added SchildeOPParser and SchildOPInstanceProvider
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Problems.Instances.Types;
29using ICSharpCode.SharpZipLib.Zip;
30
31namespace HeuristicLab.Problems.Instances.Orienteering {
32  public class SchildeOPInstanceProvider : ProblemInstanceProvider<OPData> {
33    public override string Name {
34      get { return "Schilde OP instances"; }
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 { return "Metaheuristics for the bi-objective orienteering problem (Michael Schilde, Karl F. Doerner, Richard F. Hartl, Guenter Kiechle)"; }
41    }
42    public override Uri WebLink {
43      get { return new Uri("http://prolog.univie.ac.at/research/OP/"); }
44    }
45
46    private const string FileName = "SchildeOP";
47
48    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
49      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
50      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
51
52      using (var instanceStream = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
53        foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {
54          yield return new OPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetInstanceDescription(), entry);
55        }
56      }
57    }
58    public override OPData LoadData(IDataDescriptor id) {
59      var descriptor = (OPDataDescriptor)id;
60      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
61      var parser = new SchildeOPParser();
62      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
63        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
64        using (var stream = instancesZipFile.GetInputStream(entry)) {
65          parser.Parse(stream);
66          var instance = LoadInstance(parser);
67
68          instance.Name = id.Name;
69          instance.Description = id.Description;
70
71          return instance;
72        }
73      }
74    }
75
76    public override bool CanImportData {
77      get { return true; }
78    }
79    public override OPData ImportData(string path) {
80      var parser = new SchildeOPParser();
81      parser.Parse(path);
82      var instance = LoadInstance(parser);
83
84      instance.Name = Path.GetFileName(path);
85      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
86
87      return instance;
88    }
89
90    private OPData LoadInstance(SchildeOPParser parser) {
91      return new OPData {
92        Dimension = parser.Coordinates.GetLength(0),
93        Coordinates = parser.Coordinates,
94        Distances = parser.Distances,
95        MaximumDistance = parser.UpperBoundConstraint,
96        StartingPoint = parser.StartingPoint,
97        TerminusPoint = parser.TerminusPoint,
98        Scores = Enumerable.Range(0, parser.Scores.GetLength(0)).Select(i => parser.Scores[i, 0]).ToArray()
99      };
100    }
101
102    protected virtual string GetResourceName(string fileName) {
103      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
104        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
105    }
106    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
107      ZipEntry entry;
108      while ((entry = zipFile.GetNextEntry()) != null) {
109        yield return entry.Name;
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.