Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.Problems.Instances.Scheduling/3.3/JSSPORLIBInstanceProvider.cs @ 16659

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

#2640: Updated year of copyrights in license headers

File size: 4.4 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;
29
30namespace HeuristicLab.Problems.Instances.Scheduling {
31  public class JSSPORLIBInstanceProvider : ProblemInstanceProvider<JSSPData> {
32
33    public override string Name {
34      get { return "ORLIB JSSP"; }
35    }
36
37    public override string Description {
38      get { return "Job shop scheduling problems from the Operations Research Library."; }
39    }
40
41    public override Uri WebLink {
42      get { return new Uri("http://people.brunel.ac.uk/~mastjjb/jeb/orlib/jobshopinfo.html"); }
43    }
44
45    public override string ReferencePublication {
46      get { return String.Empty; }
47    }
48
49    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
50      var instanceArchiveName = GetResourceName("JSSPORLIB.zip");
51      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
52
53      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
54        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
55          yield return new JSSPORLIBDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, null);
56        }
57      }
58    }
59
60    public override JSSPData LoadData(IDataDescriptor id) {
61      var descriptor = (JSSPORLIBDataDescriptor)id;
62      var instanceArchiveName = GetResourceName("JSSPORLIB.zip");
63      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
64        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
65
66        using (var stream = entry.Open()) {
67          var parser = new JSSPORLIBParser();
68          parser.Parse(stream);
69          return Load(parser);
70        }
71      }
72    }
73
74    public override bool CanImportData {
75      get { return true; }
76    }
77    public override JSSPData ImportData(string path) {
78      var parser = new JSSPORLIBParser();
79      parser.Parse(path);
80      return Load(parser);
81    }
82
83    private JSSPData Load(JSSPORLIBParser parser) {
84      var instance = new JSSPData {
85        Name = parser.Name,
86        Description = parser.Description,
87        Jobs = parser.Jobs,
88        Resources = parser.Resources,
89        ProcessingTimes = parser.ProcessingTimes,
90        Demands = parser.Demands,
91        DueDates = parser.DueDates
92      };
93      return instance;
94    }
95
96    public override bool CanExportData {
97      get { return true; }
98    }
99
100    public override void ExportData(JSSPData instance, string path) {
101      var parser = new JSSPORLIBParser {
102        Name = instance.Name,
103        Description = instance.Description,
104        Jobs = instance.Jobs,
105        Resources = instance.Resources,
106        ProcessingTimes = instance.ProcessingTimes,
107        Demands = instance.Demands,
108        DueDates = instance.DueDates
109      };
110      parser.Export(path);
111    }
112
113    private string GetDescription() {
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      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
119        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.