#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 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 HeuristicLab.Problems.Instances.Types;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
namespace HeuristicLab.Problems.Instances.LinearOrdering
{
public class LOPRANDBInstanceProvider : ProblemInstanceProvider
{
public override string Name {
get { return "RANDB"; }
}
public override string Description {
get { return "Linear Ordering Problems RANDB test instances"; }
}
public override Uri WebLink {
get { return new Uri("http://www.optsicom.es/lolib/lop/RANDB.zip"); }
}
public override string ReferencePublication {
get { return String.Empty; }
}
public override IEnumerable GetDataDescriptors()
{
var instanceArchiveName = GetResourceName("RANDB.zip");
if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read))
{
foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x))
{
yield return new LOPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, null);
}
}
}
public override LOPData LoadData(IDataDescriptor id)
{
var descriptor = (LOPDataDescriptor)id;
var instanceArchiveName = GetResourceName("RANDB.zip");
using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read))
{
var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
using (var stream = entry.Open())
{
var parser = new LOPParser();
parser.Parse(stream);
return Load(parser);
}
}
}
public override bool CanImportData {
get { return true; }
}
public override LOPData ImportData(string path)
{
var parser = new LOPParser();
parser.Parse(path);
return Load(parser);
}
private LOPData Load(LOPParser parser)
{
var instance = new LOPData
{
Name = parser.Name,
Description = parser.Description,
Dimension = parser.Dimension,
Matrix = parser.Matrix,
BestKnownPermutation = parser.BestKnownPermutation,
BestKnownQuality = parser.BestKnownQuality
};
return instance;
}
public override bool CanExportData {
get { return false; }
}
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);
}
}
}