Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CFSAP/HeuristicLab.Problems.Instances.CFSAP/3.3/BozejkoCFSAPInstanceProvider.cs @ 15456

Last change on this file since 15456 was 15456, checked in by abeham, 6 years ago

#2747: worked on the CFSAP

  • Introduced new benchmark instances mentioned in the literature and updated the parser
File size: 4.0 KB
Line 
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 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.CFSAP {
31  public class BozejkoCFSAPInstanceProvider : ProblemInstanceProvider<CFSAPData> {
32
33    public override string Name {
34      get { return "Bozejko (random CFSAP)"; }
35    }
36
37    public override string Description {
38      get { return string.Empty; }
39    }
40
41    public override Uri WebLink {
42      get { return null; }
43    }
44
45    public override string ReferencePublication {
46      get { return string.Empty; }
47    }
48
49    public override bool CanImportData {
50      get { return true; }
51    }
52
53    private CFSAPData LoadInstance(Stream stream) {
54      var parser = new BozejkoCFSAPParser();
55      parser.Parse(stream);
56      return new CFSAPData {
57        Jobs = parser.Jobs,
58        Nests = parser.Nests,
59        Machines = parser.Machines,
60        ProcessingTimes = parser.ProcessingTimes,
61        SetupTimes = parser.SetupTimes
62      };
63    }
64    public override CFSAPData ImportData(string path) {
65      var instance = LoadInstance(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
66      instance.Name = Path.GetFileName(path);
67      instance.Description = "Imported from " + path + " " + DateTime.Now;
68      return instance;
69    }
70
71    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
72      var instanceArchiveName = GetResourceName(@"BozejkoCFSAP\.zip");
73      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
74
75      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
76        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
77          yield return new BozejkoCFSAPDataDescriptor(Path.GetFileNameWithoutExtension(entry),
78                                                      GetInstanceDescription(), entry);
79        }
80      }
81    }
82
83    public override CFSAPData LoadData(IDataDescriptor id) {
84      var descriptor = (BozejkoCFSAPDataDescriptor)id;
85      var instanceArchiveName = GetResourceName(@"BozejkoCFSAP\.zip");
86      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
87        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
88        var instance = LoadInstance(entry.Open());
89        instance.Name = id.Name;
90        instance.Description = id.Description;
91        return instance;
92      }
93    }
94
95    private string GetResourceName(string fileName) {
96      return Assembly.GetExecutingAssembly()
97                     .GetManifestResourceNames()
98                     .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
99    }
100
101    private string GetInstanceDescription() {
102      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
103    }
104
105  }
106}
Note: See TracBrowser for help on using the repository browser.