#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;
namespace HeuristicLab.Problems.Instances.TSPLIB {
public class TSPLIBTSPInstanceProvider : ProblemInstanceProvider {
public override string Name {
get { return "TSPLIB (symmetric TSP)"; }
}
public override string Description {
get { return "Traveling Salesman Problem Library"; }
}
public override Uri Link {
get { return new Uri("http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/"); }
}
public override IEnumerable GetInstanceDescriptors() {
var solutions = Assembly.GetExecutingAssembly()
.GetManifestResourceNames()
.Where(x => x.EndsWith(".opt.tour"))
.ToDictionary(x => x.Substring(0, x.Length - ".opt.tour".Length) + ".tsp", x => x);
return Assembly.GetExecutingAssembly()
.GetManifestResourceNames()
.Where(x => x.EndsWith(".tsp"))
.OrderBy(x => x)
.Select(x => new TSPLIBInstanceDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
}
public override ITSPInstance GetInstance(IInstanceDescriptor id) {
var descriptor = (TSPLIBInstanceDescriptor)id;
var instance = new TSPLIBInstance();
using (var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(descriptor.InstanceIdentifier)) {
var tspParser = new TSPLIBParser(stream);
tspParser.Parse();
instance.Coordinates = tspParser.Vertices != null ? tspParser.Vertices : tspParser.DisplayVertices;
instance.Distances = tspParser.Distances;
instance.Name = tspParser.Name;
instance.Description = tspParser.Comment
+ Environment.NewLine + Environment.NewLine
+ GetDescription();
if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
using (Stream solStream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(descriptor.SolutionIdentifier)) {
var slnParser = new TSPLIBParser(solStream);
slnParser.Parse();
instance.BestKnownTour = slnParser.Tour[0];
}
}
}
return instance;
}
private string GetPrettyName(string instanceIdentifier) {
return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.TSP\.(.*)\.tsp").Groups[1].Captures[0].Value;
}
private string GetDescription() {
return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast().First().Version + ".";
}
}
}