Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs @ 7466

Last change on this file since 7466 was 7466, checked in by abeham, 12 years ago

#1614

  • included TSLIB's ATSP and CVRP problems as well
File size: 5.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Reflection;
27using System.Text.RegularExpressions;
28
29namespace HeuristicLab.Problems.Instances.TSPLIB {
30  public class TSPLIBTSPInstanceProvider : ProblemInstanceProvider<ITSPInstance> {
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 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\.TSP\..*").Success)
48        .Where(x => x.EndsWith(".opt.tour"))
49        .ToDictionary(x => x.Substring(0, x.Length - ".opt.tour".Length) + ".tsp", x => x);
50
51      return Assembly.GetExecutingAssembly()
52        .GetManifestResourceNames()
53        .Where(x => Regex.Match(x, @".*\.Data\.TSP\..*").Success)
54        .Where(x => x.EndsWith(".tsp"))
55        .OrderBy(x => x)
56        .Select(x => new TSPLIBInstanceDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
57    }
58
59    public override ITSPInstance GetInstance(IInstanceDescriptor id) {
60      var descriptor = (TSPLIBInstanceDescriptor)id;
61      var instance = new TSPLIBTSPInstance();
62      using (var stream = Assembly.GetExecutingAssembly()
63        .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
64        var parser = new TSPLIBParser(stream);
65        parser.Parse();
66        instance.Dimension = parser.Dimension;
67        instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
68        instance.Distances = parser.Distances;
69        switch (parser.EdgeWeightType) {
70          case TSPLIBEdgeWeightTypes.ATT:
71            instance.DistanceMeasure = TSPDistanceMeasure.Att; break;
72          case TSPLIBEdgeWeightTypes.CEIL_2D:
73            instance.DistanceMeasure = TSPDistanceMeasure.UpperEuclidean; break;
74          case TSPLIBEdgeWeightTypes.EUC_2D:
75            instance.DistanceMeasure = TSPDistanceMeasure.RoundedEuclidean; break;
76          case TSPLIBEdgeWeightTypes.EUC_3D:
77            throw new InvalidDataException("3D coordinates are not supported.");
78          case TSPLIBEdgeWeightTypes.EXPLICIT:
79            instance.DistanceMeasure = TSPDistanceMeasure.Direct; break;
80          case TSPLIBEdgeWeightTypes.GEO:
81            instance.DistanceMeasure = TSPDistanceMeasure.Geo; break;
82          case TSPLIBEdgeWeightTypes.MAN_2D:
83            instance.DistanceMeasure = TSPDistanceMeasure.Manhattan; break;
84          case TSPLIBEdgeWeightTypes.MAN_3D:
85            throw new InvalidDataException("3D coordinates are not supported.");
86          case TSPLIBEdgeWeightTypes.MAX_2D:
87            instance.DistanceMeasure = TSPDistanceMeasure.Maximum; break;
88          case TSPLIBEdgeWeightTypes.MAX_3D:
89            throw new InvalidDataException("3D coordinates are not supported.");
90          default:
91            throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
92        }
93
94        instance.Name = parser.Name;
95        instance.Description = parser.Comment
96          + Environment.NewLine + Environment.NewLine
97          + GetDescription();
98
99        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
100          using (Stream solStream = Assembly.GetExecutingAssembly()
101            .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
102            var tourParser = new TSPLIBParser(solStream);
103            tourParser.Parse();
104            instance.BestKnownTour = tourParser.Tour[0];
105          }
106        }
107      }
108      return instance;
109    }
110
111    private string GetPrettyName(string instanceIdentifier) {
112      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.TSP\.(.*)\.tsp").Groups[1].Captures[0].Value;
113    }
114
115    private string GetDescription() {
116      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.