Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBATSPInstanceProvider.cs @ 7558

Last change on this file since 7558 was 7548, checked in by abeham, 13 years ago

#1614: changed according to architects review

File size: 4.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 TSPLIBATSPInstanceProvider : ProblemInstanceProvider<ATSPData> {
31
32    public override string Name {
33      get { return "TSPLIB (asymmetric 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.
47TSPLIB - A Traveling Salesman Problem Library.
48ORSA 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\.ATSP\..*").Success)
56        .Where(x => x.EndsWith(".opt.tour"))
57        .ToDictionary(x => x.Substring(0, x.Length - ".opt.tour".Length) + ".atsp", x => x);
58
59      return Assembly.GetExecutingAssembly()
60          .GetManifestResourceNames()
61        .Where(x => Regex.Match(x, @".*\.Data\.ATSP\..*").Success)
62          .Where(x => x.EndsWith(".atsp"))
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 ATSPData LoadData(IDataDescriptor id) {
68      var descriptor = (TSPLIBDataDescriptor)id;
69      using (var stream = Assembly.GetExecutingAssembly()
70        .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
71        var instance = Load(new TSPLIBParser(stream));
72
73        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
74          using (Stream solStream = Assembly.GetExecutingAssembly()
75            .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
76            var tourParser = new TSPLIBParser(solStream);
77            tourParser.Parse();
78            instance.BestKnownTour = tourParser.Tour[0];
79          }
80        }
81        return instance;
82      }
83    }
84
85    public override ATSPData LoadData(string path) {
86      return Load(new TSPLIBParser(path));
87    }
88
89    public override void SaveData(ATSPData instance, string path) {
90      throw new NotSupportedException();
91    }
92
93    private ATSPData Load(TSPLIBParser parser) {
94      var instance = new ATSPData();
95
96      parser.Parse();
97      instance.Dimension = parser.Dimension;
98      instance.Coordinates = parser.DisplayVertices != null ? parser.DisplayVertices : parser.Vertices;
99      instance.Distances = parser.Distances;
100
101      instance.Name = parser.Name;
102      instance.Description = parser.Comment
103        + Environment.NewLine + Environment.NewLine
104        + GetDescription();
105
106      return instance;
107    }
108
109    private string GetPrettyName(string instanceIdentifier) {
110      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.ATSP\.(.*)\.atsp").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}
Note: See TracBrowser for help on using the repository browser.