Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.Instances.PermutationProblems/3.3/LOP/LOPSPECInstanceProvider.cs @ 15686

Last change on this file since 15686 was 15661, checked in by fholzing, 7 years ago

#2864: Simplified Project-Structure

File size: 3.9 KB
RevLine 
[15661]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Problems.Instances.Types;
23using System;
24using System.Collections.Generic;
25using System.IO;
26using System.IO.Compression;
27using System.Linq;
28using System.Reflection;
29using System.Text.RegularExpressions;
30
31namespace HeuristicLab.Problems.Instances.PermutationProblems.LinearOrdering {
32  public class LOPSPECInstanceProvider : ProblemInstanceProvider<LOPData> {
33
34    public override string Name
35    {
36      get { return "SPEC"; }
37    }
38
39    public override string Description
40    {
41      get { return "Linear Ordering Problems SPEC test instances"; }
42    }
43
44    public override Uri WebLink
45    {
46      get { return new Uri("http://www.optsicom.es/lolib/lop/SPEC.zip"); }
47    }
48
49    public override string ReferencePublication
50    {
51      get { return String.Empty; }
52    }
53
54    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
55      var instanceArchiveName = GetResourceName("SPEC.zip");
56      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
57
58      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
59        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
60          yield return new LOPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, null);
61        }
62      }
63    }
64
65    public override LOPData LoadData(IDataDescriptor id) {
66      var descriptor = (LOPDataDescriptor)id;
67      var instanceArchiveName = GetResourceName("SPEC.zip");
68      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
69        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
70
71        using (var stream = entry.Open()) {
72          var parser = new LOPParser();
73          parser.Parse(stream);
74          return Load(parser);
75        }
76      }
77    }
78
79    public override bool CanImportData
80    {
81      get { return true; }
82    }
83    public override LOPData ImportData(string path) {
84      var parser = new LOPParser();
85      parser.Parse(path);
86      return Load(parser);
87    }
88
89    private LOPData Load(LOPParser parser) {
90      var instance = new LOPData {
91        Name = parser.Name,
92        Description = parser.Description,
93        Dimension = parser.Dimension,
94        Matrix = parser.Matrix,
95        BestKnownPermutation = parser.BestKnownPermutation,
96        BestKnownQuality = parser.BestKnownQuality
97      };
98      return instance;
99    }
100
101    public override bool CanExportData
102    {
103      get { return false; }
104    }
105
106    private string GetDescription() {
107      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
108    }
109
110    protected virtual string GetResourceName(string fileName) {
111      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
112        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.