Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.Problems.Instances.Scheduling/3.3/JSSPORLIBInstanceProvider.cs @ 17366

Last change on this file since 17366 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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          var instance = Load(parser);
70          instance.Name = id.Name;
71          instance.Description = id.Description;
72
73          return instance;
74        }
75      }
76    }
77
78    public override bool CanImportData {
79      get { return true; }
80    }
81    public override JSSPData ImportData(string path) {
82      var parser = new JSSPORLIBParser();
83      parser.Parse(path);
84      var instance = Load(parser);
85      instance.Name = Path.GetFileName(path);
86      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
87      return instance;
88    }
89
90    private JSSPData Load(JSSPORLIBParser parser) {
91      var instance = new JSSPData {
92        Jobs = parser.Jobs,
93        Resources = parser.Resources,
94        ProcessingTimes = parser.ProcessingTimes,
95        Demands = parser.Demands,
96        DueDates = parser.DueDates
97      };
98      return instance;
99    }
100
101    public override bool CanExportData {
102      get { return true; }
103    }
104
105    public override void ExportData(JSSPData instance, string path) {
106      var parser = new JSSPORLIBParser {
107        Name = instance.Name,
108        Description = instance.Description,
109        Jobs = instance.Jobs,
110        Resources = instance.Resources,
111        ProcessingTimes = instance.ProcessingTimes,
112        Demands = instance.Demands,
113        DueDates = instance.DueDates
114      };
115      parser.Export(path);
116    }
117
118    private string GetDescription() {
119      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
120    }
121
122    protected virtual string GetResourceName(string fileName) {
123      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
124        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.