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