[14757] | 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 System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using System.IO.Compression;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using System.Reflection;
|
---|
| 28 | using System.Text.RegularExpressions;
|
---|
| 29 |
|
---|
| 30 | namespace 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 | MachineNests = 2,
|
---|
| 59 | ProcessingTimes = parser.ProcessingTimes,
|
---|
| 60 | SetupTimes = parser.SetupTimes
|
---|
| 61 | };
|
---|
| 62 | }
|
---|
| 63 | public override CFSAPData ImportData(string path) {
|
---|
| 64 | var instance = LoadInstance(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
|
---|
| 65 | instance.Name = Path.GetFileName(path);
|
---|
| 66 | instance.Description = "Imported from " + path + " " + DateTime.Now;
|
---|
| 67 | return instance;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
| 71 | var instanceArchiveName = GetResourceName(@"BozejkoCFSAP\.zip");
|
---|
| 72 | if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
|
---|
| 73 |
|
---|
| 74 | using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
|
---|
| 75 | foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
|
---|
| 76 | yield return new BozejkoCFSAPDataDescriptor(Path.GetFileNameWithoutExtension(entry),
|
---|
| 77 | GetInstanceDescription(), entry);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public override CFSAPData LoadData(IDataDescriptor id) {
|
---|
| 83 | var descriptor = (BozejkoCFSAPDataDescriptor)id;
|
---|
| 84 | var instanceArchiveName = GetResourceName(@"BozejkoCFSAP\.zip");
|
---|
| 85 | using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
|
---|
| 86 | var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
|
---|
| 87 | var instance = LoadInstance(entry.Open());
|
---|
| 88 | instance.Name = id.Name;
|
---|
| 89 | instance.Description = id.Description;
|
---|
| 90 | return instance;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | private string GetResourceName(string fileName) {
|
---|
| 95 | return Assembly.GetExecutingAssembly()
|
---|
| 96 | .GetManifestResourceNames()
|
---|
| 97 | .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | private string GetInstanceDescription() {
|
---|
| 101 | return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | }
|
---|
| 105 | }
|
---|