Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • Removed incompatible problem linhp318.tsp (contains fixed edges)
  • Fixed AssemblyInfo for TSPLIB
  • Added unit tests
  • Worked on assignment / solution view
File size: 5.3 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        if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
67
68        instance.Dimension = parser.Dimension;
69        instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
70        instance.Distances = parser.Distances;
71        switch (parser.EdgeWeightType) {
72          case TSPLIBEdgeWeightTypes.ATT:
73            instance.DistanceMeasure = TSPDistanceMeasure.Att; break;
74          case TSPLIBEdgeWeightTypes.CEIL_2D:
75            instance.DistanceMeasure = TSPDistanceMeasure.UpperEuclidean; break;
76          case TSPLIBEdgeWeightTypes.EUC_2D:
77            instance.DistanceMeasure = TSPDistanceMeasure.RoundedEuclidean; break;
78          case TSPLIBEdgeWeightTypes.EUC_3D:
79            throw new InvalidDataException("3D coordinates are not supported.");
80          case TSPLIBEdgeWeightTypes.EXPLICIT:
81            instance.DistanceMeasure = TSPDistanceMeasure.Direct; break;
82          case TSPLIBEdgeWeightTypes.GEO:
83            instance.DistanceMeasure = TSPDistanceMeasure.Geo; break;
84          case TSPLIBEdgeWeightTypes.MAN_2D:
85            instance.DistanceMeasure = TSPDistanceMeasure.Manhattan; break;
86          case TSPLIBEdgeWeightTypes.MAN_3D:
87            throw new InvalidDataException("3D coordinates are not supported.");
88          case TSPLIBEdgeWeightTypes.MAX_2D:
89            instance.DistanceMeasure = TSPDistanceMeasure.Maximum; break;
90          case TSPLIBEdgeWeightTypes.MAX_3D:
91            throw new InvalidDataException("3D coordinates are not supported.");
92          default:
93            throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
94        }
95
96        instance.Name = parser.Name;
97        instance.Description = parser.Comment
98          + Environment.NewLine + Environment.NewLine
99          + GetDescription();
100
101        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
102          using (Stream solStream = Assembly.GetExecutingAssembly()
103            .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
104            var tourParser = new TSPLIBParser(solStream);
105            tourParser.Parse();
106            instance.BestKnownTour = tourParser.Tour[0];
107          }
108        }
109      }
110      return instance;
111    }
112
113    private string GetPrettyName(string instanceIdentifier) {
114      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.TSP\.(.*)\.tsp").Groups[1].Captures[0].Value;
115    }
116
117    private string GetDescription() {
118      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.