[9348] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[13032] | 3 | * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9348] | 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 |
|
---|
[13032] | 22 |
|
---|
[9348] | 23 | using System;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.IO;
|
---|
[13460] | 26 | using System.IO.Compression;
|
---|
[9348] | 27 | using System.Linq;
|
---|
| 28 | using System.Reflection;
|
---|
| 29 | using System.Text.RegularExpressions;
|
---|
[13460] | 30 | using System.Windows.Forms;
|
---|
[9348] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.Instances.BinPacking {
|
---|
[13460] | 33 | public class BPPORLIBInstanceProvider : ProblemInstanceProvider<BPPData>, IProblemInstanceProvider<BPPData> {
|
---|
[9348] | 34 |
|
---|
| 35 | public override string Name {
|
---|
| 36 | get { return "ORLIB BPP"; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public override string Description {
|
---|
| 40 | get { return "Bin packing problems from the Operations Research Library."; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public override Uri WebLink {
|
---|
| 44 | get { return new Uri("http://people.brunel.ac.uk/~mastjjb/jeb/orlib/binpacktwoinfo.html , http://www.diku.dk/~pisinger/new3dbpp/readme.3dbpp"); }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override string ReferencePublication {
|
---|
| 48 | get { return String.Empty; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
| 52 | var instanceArchiveName = GetResourceName("BPPORLIB.zip");
|
---|
| 53 | if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
|
---|
| 54 |
|
---|
[13460] | 55 |
|
---|
| 56 | using (var file = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
|
---|
| 57 |
|
---|
| 58 | foreach (var entry in file.Entries.OrderBy(x => x.Name)) {
|
---|
| 59 | if (string.IsNullOrWhiteSpace(entry.Name)) continue;
|
---|
| 60 | yield return new BPPORLIBDataDescriptor(
|
---|
| 61 | name: Path.GetFileNameWithoutExtension(entry.Name),
|
---|
| 62 | description: GetDescription(),
|
---|
| 63 | instanceIdentifier: entry.FullName,
|
---|
| 64 | solutionIdentifier: null);
|
---|
[9348] | 65 | }
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public override BPPData LoadData(IDataDescriptor id) {
|
---|
| 70 | var descriptor = (BPPORLIBDataDescriptor)id;
|
---|
[13460] | 71 | var instanceArchiveName = GetResourceName("BPPORLIB.zip");
|
---|
| 72 | using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
|
---|
[9348] | 73 | var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
|
---|
| 74 |
|
---|
[13460] | 75 | using (var stream = entry.Open()) {
|
---|
[9348] | 76 | var parser = new BPPORLIBParser();
|
---|
| 77 | parser.Parse(stream);
|
---|
| 78 | var instance = Load(parser);
|
---|
| 79 | instance.Name = id.Name;
|
---|
| 80 | instance.Description = id.Description;
|
---|
| 81 |
|
---|
| 82 | return instance;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public override bool CanImportData {
|
---|
| 88 | get { return true; }
|
---|
| 89 | }
|
---|
| 90 | public override BPPData ImportData(string path) {
|
---|
| 91 | var parser = new BPPORLIBParser();
|
---|
| 92 | parser.Parse(path);
|
---|
| 93 | var instance = Load(parser);
|
---|
| 94 | instance.Name = Path.GetFileName(path);
|
---|
[13460] | 95 | instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now;
|
---|
[9348] | 96 | return instance;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private BPPData Load(BPPORLIBParser parser) {
|
---|
| 100 | var instance = new BPPData {
|
---|
| 101 | Items = parser.Items,
|
---|
| 102 | BinMeasures = parser.BinMeasures,
|
---|
| 103 | ItemMeasures = parser.ItemMeasures
|
---|
| 104 | };
|
---|
| 105 | return instance;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public override bool CanExportData {
|
---|
| 109 | get { return true; }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public override void ExportData(BPPData instance, string path) {
|
---|
| 113 | var parser = new BPPORLIBParser {
|
---|
| 114 | Name = instance.Name,
|
---|
| 115 | Description = instance.Description,
|
---|
| 116 | Items = instance.Items,
|
---|
| 117 | BinMeasures = instance.BinMeasures,
|
---|
| 118 | ItemMeasures = instance.ItemMeasures
|
---|
| 119 | };
|
---|
| 120 | parser.Export(path);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | private string GetDescription() {
|
---|
| 124 | return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | protected virtual string GetResourceName(string fileName) {
|
---|
| 128 | return Assembly.GetExecutingAssembly().GetManifestResourceNames()
|
---|
| 129 | .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|