Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • Added TSPLIB instances (TSP, ATSP, CVRP)
  • Improved parser for TSPLIB file format
  • Added ProblemInstanceProvider as an abstract base class which handles the feeding of consumers
File size: 3.6 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 => x.EndsWith(".opt.tour"))
48        .ToDictionary(x => x.Substring(0, x.Length - ".opt.tour".Length) + ".tsp", x => x);
49
50      return Assembly.GetExecutingAssembly()
51          .GetManifestResourceNames()
52          .Where(x => x.EndsWith(".tsp"))
53          .OrderBy(x => x)
54          .Select(x => new TSPLIBInstanceDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
55    }
56
57    public override ITSPInstance GetInstance(IInstanceDescriptor id) {
58      var descriptor = (TSPLIBInstanceDescriptor)id;
59      var instance = new TSPLIBInstance();
60      using (var stream = Assembly.GetExecutingAssembly()
61        .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
62        var tspParser = new TSPLIBParser(stream);
63        tspParser.Parse();
64        instance.Coordinates = tspParser.Vertices != null ? tspParser.Vertices : tspParser.DisplayVertices;
65        instance.Distances = tspParser.Distances;
66
67        instance.Name = tspParser.Name;
68        instance.Description = tspParser.Comment
69          + Environment.NewLine + Environment.NewLine
70          + GetDescription();
71
72        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
73          using (Stream solStream = Assembly.GetExecutingAssembly()
74            .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
75            var slnParser = new TSPLIBParser(solStream);
76            slnParser.Parse();
77            instance.BestKnownTour = slnParser.Tour[0];
78          }
79        }
80      }
81      return instance;
82    }
83
84    private string GetPrettyName(string instanceIdentifier) {
85      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.TSP\.(.*)\.tsp").Groups[1].Captures[0].Value;
86    }
87
88    private string GetDescription() {
89      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.