1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Linq;
|
---|
26 | using System.Reflection;
|
---|
27 | using System.Text.RegularExpressions;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.Instances.TSPLIB {
|
---|
30 | public class TSPLIBCVRPInstanceProvider : ProblemInstanceProvider<ICVRPInstance> {
|
---|
31 |
|
---|
32 | public override string Name {
|
---|
33 | get { return "TSPLIB (CVRP)"; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public override string Description {
|
---|
37 | get { return "Traveling Salesman Problem Library"; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public override Uri Link {
|
---|
41 | get { return new Uri("http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/"); }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public override IEnumerable<IInstanceDescriptor> GetInstanceDescriptors() {
|
---|
45 | var solutions = Assembly.GetExecutingAssembly()
|
---|
46 | .GetManifestResourceNames()
|
---|
47 | .Where(x => Regex.Match(x, @".*\.Data\.CVRP\..*").Success)
|
---|
48 | .Where(x => x.EndsWith(".opt.tour"))
|
---|
49 | .ToDictionary(x => x.Substring(0, x.Length - ".opt.tour".Length) + ".vrp", x => x);
|
---|
50 |
|
---|
51 | return Assembly.GetExecutingAssembly()
|
---|
52 | .GetManifestResourceNames()
|
---|
53 | .Where(x => Regex.Match(x, @".*\.Data\.CVRP\..*").Success)
|
---|
54 | .OrderBy(x => x)
|
---|
55 | .Select(x => new TSPLIBInstanceDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override ICVRPInstance GetInstance(IInstanceDescriptor id) {
|
---|
59 | var descriptor = (TSPLIBInstanceDescriptor)id;
|
---|
60 | var instance = new TSPLIBCVRPInstance();
|
---|
61 | using (var stream = Assembly.GetExecutingAssembly()
|
---|
62 | .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
|
---|
63 | var parser = new TSPLIBParser(stream);
|
---|
64 | parser.Parse();
|
---|
65 | instance.Dimension = parser.Dimension;
|
---|
66 | instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
|
---|
67 | instance.Distances = parser.Distances;
|
---|
68 | instance.Capacity = parser.Capacity.Value;
|
---|
69 | instance.Demands = parser.Demands;
|
---|
70 | switch (parser.EdgeWeightType) {
|
---|
71 | case TSPLIBEdgeWeightTypes.ATT:
|
---|
72 | instance.DistanceMeasure = CVRPDistanceMeasure.Att; break;
|
---|
73 | case TSPLIBEdgeWeightTypes.CEIL_2D:
|
---|
74 | instance.DistanceMeasure = CVRPDistanceMeasure.UpperEuclidean; break;
|
---|
75 | case TSPLIBEdgeWeightTypes.EUC_2D:
|
---|
76 | case TSPLIBEdgeWeightTypes.EUC_3D:
|
---|
77 | instance.DistanceMeasure = CVRPDistanceMeasure.RoundedEuclidean; break;
|
---|
78 | case TSPLIBEdgeWeightTypes.EXPLICIT:
|
---|
79 | instance.DistanceMeasure = CVRPDistanceMeasure.Direct; break;
|
---|
80 | case TSPLIBEdgeWeightTypes.GEO:
|
---|
81 | instance.DistanceMeasure = CVRPDistanceMeasure.Geo; break;
|
---|
82 | case TSPLIBEdgeWeightTypes.MAN_2D:
|
---|
83 | case TSPLIBEdgeWeightTypes.MAN_3D:
|
---|
84 | instance.DistanceMeasure = CVRPDistanceMeasure.Manhattan; break;
|
---|
85 | case TSPLIBEdgeWeightTypes.MAX_2D:
|
---|
86 | case TSPLIBEdgeWeightTypes.MAX_3D:
|
---|
87 | instance.DistanceMeasure = CVRPDistanceMeasure.Maximum; break;
|
---|
88 | default:
|
---|
89 | throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
|
---|
90 | }
|
---|
91 |
|
---|
92 | instance.Name = parser.Name;
|
---|
93 | instance.Description = parser.Comment
|
---|
94 | + Environment.NewLine + Environment.NewLine
|
---|
95 | + GetDescription();
|
---|
96 |
|
---|
97 | if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
|
---|
98 | using (Stream solStream = Assembly.GetExecutingAssembly()
|
---|
99 | .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
|
---|
100 | var slnParser = new TSPLIBParser(solStream);
|
---|
101 | slnParser.Parse();
|
---|
102 | instance.BestKnownTour = slnParser.Tour;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | return instance;
|
---|
107 | }
|
---|
108 |
|
---|
109 | private string GetPrettyName(string instanceIdentifier) {
|
---|
110 | return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.CVRP\.(.*)\.vrp").Groups[1].Captures[0].Value;
|
---|
111 | }
|
---|
112 |
|
---|
113 | private string GetDescription() {
|
---|
114 | return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|