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 TSPLIBTSPInstanceProvider : ProblemInstanceProvider<TSPData> {
|
---|
31 |
|
---|
32 | public override string Name {
|
---|
33 | get { return "TSPLIB (symmetric TSP)"; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public override string Description {
|
---|
37 | get { return "Traveling Salesman Problem Library"; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public override Uri WebLink {
|
---|
41 | get { return new Uri("http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/"); }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public override string ReferencePublication {
|
---|
45 | get {
|
---|
46 | return @"G. Reinelt. 1991.
|
---|
47 | TSPLIB - A Traveling Salesman Problem Library.
|
---|
48 | ORSA Journal on Computing, 3, pp. 376-384.";
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
53 | var solutions = Assembly.GetExecutingAssembly()
|
---|
54 | .GetManifestResourceNames()
|
---|
55 | .Where(x => Regex.Match(x, @".*\.Data\.TSP\..*").Success)
|
---|
56 | .Where(x => x.EndsWith(".opt.tour"))
|
---|
57 | .ToDictionary(x => x.Substring(0, x.Length - ".opt.tour".Length) + ".tsp", x => x);
|
---|
58 |
|
---|
59 | return Assembly.GetExecutingAssembly()
|
---|
60 | .GetManifestResourceNames()
|
---|
61 | .Where(x => Regex.Match(x, @".*\.Data\.TSP\..*").Success)
|
---|
62 | .Where(x => x.EndsWith(".tsp"))
|
---|
63 | .OrderBy(x => x)
|
---|
64 | .Select(x => new TSPLIBDataDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override TSPData LoadData(IDataDescriptor id) {
|
---|
68 | var descriptor = (TSPLIBDataDescriptor)id;
|
---|
69 | using (var stream = Assembly.GetExecutingAssembly()
|
---|
70 | .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
|
---|
71 | var parser = new TSPLIBParser(stream);
|
---|
72 | var instance = Load(parser);
|
---|
73 |
|
---|
74 | if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
|
---|
75 | using (Stream solStream = Assembly.GetExecutingAssembly()
|
---|
76 | .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
|
---|
77 | var tourParser = new TSPLIBParser(solStream);
|
---|
78 | tourParser.Parse();
|
---|
79 | instance.BestKnownTour = tourParser.Tour[0];
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return instance;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override TSPData LoadData(string path) {
|
---|
87 | return Load(new TSPLIBParser(path));
|
---|
88 | }
|
---|
89 |
|
---|
90 | public TSPData LoadData(string tspFile, string tourFile, double? bestQuality) {
|
---|
91 | var data = Load(new TSPLIBParser(tspFile));
|
---|
92 | if (bestQuality.HasValue)
|
---|
93 | data.BestKnownQuality = bestQuality.Value;
|
---|
94 | else data.BestKnownQuality = null;
|
---|
95 | if (!String.IsNullOrEmpty(tourFile)) {
|
---|
96 | var tourParser = new TSPLIBParser(tourFile);
|
---|
97 | tourParser.Parse();
|
---|
98 | data.BestKnownTour = tourParser.Tour[0];
|
---|
99 | }
|
---|
100 | return data;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public override void SaveData(TSPData instance, string path) {
|
---|
104 | throw new NotSupportedException();
|
---|
105 | }
|
---|
106 |
|
---|
107 | private TSPData Load(TSPLIBParser parser) {
|
---|
108 | parser.Parse();
|
---|
109 | if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
|
---|
110 | var instance = new TSPData();
|
---|
111 | instance.Dimension = parser.Dimension;
|
---|
112 | instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
|
---|
113 | instance.Distances = parser.Distances;
|
---|
114 | switch (parser.EdgeWeightType) {
|
---|
115 | case TSPLIBEdgeWeightTypes.ATT:
|
---|
116 | instance.DistanceMeasure = TSPDistanceMeasure.Att; break;
|
---|
117 | case TSPLIBEdgeWeightTypes.CEIL_2D:
|
---|
118 | instance.DistanceMeasure = TSPDistanceMeasure.UpperEuclidean; break;
|
---|
119 | case TSPLIBEdgeWeightTypes.EUC_2D:
|
---|
120 | instance.DistanceMeasure = TSPDistanceMeasure.RoundedEuclidean; break;
|
---|
121 | case TSPLIBEdgeWeightTypes.EUC_3D:
|
---|
122 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
123 | case TSPLIBEdgeWeightTypes.EXPLICIT:
|
---|
124 | instance.DistanceMeasure = TSPDistanceMeasure.Direct; break;
|
---|
125 | case TSPLIBEdgeWeightTypes.GEO:
|
---|
126 | instance.DistanceMeasure = TSPDistanceMeasure.Geo; break;
|
---|
127 | case TSPLIBEdgeWeightTypes.MAN_2D:
|
---|
128 | instance.DistanceMeasure = TSPDistanceMeasure.Manhattan; break;
|
---|
129 | case TSPLIBEdgeWeightTypes.MAN_3D:
|
---|
130 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
131 | case TSPLIBEdgeWeightTypes.MAX_2D:
|
---|
132 | instance.DistanceMeasure = TSPDistanceMeasure.Maximum; break;
|
---|
133 | case TSPLIBEdgeWeightTypes.MAX_3D:
|
---|
134 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
135 | default:
|
---|
136 | throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
|
---|
137 | }
|
---|
138 |
|
---|
139 | instance.Name = parser.Name;
|
---|
140 | instance.Description = parser.Comment
|
---|
141 | + Environment.NewLine + Environment.NewLine
|
---|
142 | + GetDescription();
|
---|
143 | return instance;
|
---|
144 | }
|
---|
145 |
|
---|
146 | private string GetPrettyName(string instanceIdentifier) {
|
---|
147 | return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.TSP\.(.*)\.tsp").Groups[1].Captures[0].Value;
|
---|
148 | }
|
---|
149 |
|
---|
150 | private string GetDescription() {
|
---|
151 | return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|