#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using ICSharpCode.SharpZipLib.Zip;
namespace HeuristicLab.Problems.Instances.BinPacking {
public class BPPORLIBInstanceProvider : ProblemInstanceProvider {
public override string Name {
get { return "ORLIB BPP"; }
}
public override string Description {
get { return "Bin packing problems from the Operations Research Library."; }
}
public override Uri WebLink {
get { return new Uri("http://people.brunel.ac.uk/~mastjjb/jeb/orlib/binpacktwoinfo.html , http://www.diku.dk/~pisinger/new3dbpp/readme.3dbpp"); }
}
public override string ReferencePublication {
get { return String.Empty; }
}
public override IEnumerable GetDataDescriptors() {
var instanceArchiveName = GetResourceName("BPPORLIB.zip");
if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
using (var instanceStream = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {
yield return new BPPORLIBDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, null);
}
}
}
public override BPPData LoadData(IDataDescriptor id) {
var descriptor = (BPPORLIBDataDescriptor)id;
var instanceArchiveName = GetResourceName("JSSPORLIB.zip");
using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
using (var stream = instancesZipFile.GetInputStream(entry)) {
var parser = new BPPORLIBParser();
parser.Parse(stream);
var instance = Load(parser);
instance.Name = id.Name;
instance.Description = id.Description;
return instance;
}
}
}
public override bool CanImportData {
get { return true; }
}
public override BPPData ImportData(string path) {
var parser = new BPPORLIBParser();
parser.Parse(path);
var instance = Load(parser);
instance.Name = Path.GetFileName(path);
instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
return instance;
}
private BPPData Load(BPPORLIBParser parser) {
var instance = new BPPData {
Items = parser.Items,
BinMeasures = parser.BinMeasures,
ItemMeasures = parser.ItemMeasures
};
return instance;
}
public override bool CanExportData {
get { return true; }
}
public override void ExportData(BPPData instance, string path) {
var parser = new BPPORLIBParser {
Name = instance.Name,
Description = instance.Description,
Items = instance.Items,
BinMeasures = instance.BinMeasures,
ItemMeasures = instance.ItemMeasures
};
parser.Export(path);
}
private string GetDescription() {
return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast().First().Version + ".";
}
protected virtual string GetResourceName(string fileName) {
return Assembly.GetExecutingAssembly().GetManifestResourceNames()
.SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
}
protected IEnumerable GetZipContents(ZipInputStream zipFile) {
ZipEntry entry;
while ((entry = zipFile.GetNextEntry()) != null) {
yield return entry.Name;
}
}
}
}