Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBCVRPInstanceProvider.cs @ 7538

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

#1614

  • Fixed plugin dependencies
  • Updated GQAP view
  • Changed instances infrastructure
    • Changed interface types into classes
    • Removed the library specific instance classes
File size: 5.5 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 TSPLIBCVRPInstanceProvider : ProblemInstanceProvider<CVRPInstance> {
31
32    public override string Name {
33      get { return "TSPLIB (CVRP)"; }
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<IInstanceDescriptor> GetInstanceDescriptors() {
53      var solutions = Assembly.GetExecutingAssembly()
54        .GetManifestResourceNames()
55        .Where(x => Regex.Match(x, @".*\.Data\.CVRP\..*").Success)
56        .Where(x => x.EndsWith(".opt.tour"))
57        .ToDictionary(x => x.Substring(0, x.Length - ".opt.tour".Length) + ".vrp", x => x);
58
59      return Assembly.GetExecutingAssembly()
60          .GetManifestResourceNames()
61          .Where(x => Regex.Match(x, @".*\.Data\.CVRP\..*").Success)
62          .OrderBy(x => x)
63          .Select(x => new TSPLIBInstanceDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
64    }
65
66    public override CVRPInstance LoadInstance(IInstanceDescriptor id) {
67      var descriptor = (TSPLIBInstanceDescriptor)id;
68      using (var stream = Assembly.GetExecutingAssembly()
69        .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
70        var instance = Load(new TSPLIBParser(stream));
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;
78          }
79        }
80        return instance;
81      }
82    }
83
84    public override CVRPInstance LoadInstance(string path) {
85      return Load(new TSPLIBParser(path));
86    }
87
88    public override void SaveInstance(CVRPInstance instance, string path) {
89      throw new NotSupportedException();
90    }
91
92    private CVRPInstance Load(TSPLIBParser parser) {
93      parser.Parse();
94      var instance = new CVRPInstance();
95      instance.Dimension = parser.Dimension;
96      instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
97      instance.Distances = parser.Distances;
98      instance.Capacity = parser.Capacity.Value;
99      instance.Demands = parser.Demands;
100      switch (parser.EdgeWeightType) {
101        case TSPLIBEdgeWeightTypes.ATT:
102          instance.DistanceMeasure = CVRPDistanceMeasure.Att; break;
103        case TSPLIBEdgeWeightTypes.CEIL_2D:
104          instance.DistanceMeasure = CVRPDistanceMeasure.UpperEuclidean; break;
105        case TSPLIBEdgeWeightTypes.EUC_2D:
106        case TSPLIBEdgeWeightTypes.EUC_3D:
107          instance.DistanceMeasure = CVRPDistanceMeasure.RoundedEuclidean; break;
108        case TSPLIBEdgeWeightTypes.EXPLICIT:
109          instance.DistanceMeasure = CVRPDistanceMeasure.Direct; break;
110        case TSPLIBEdgeWeightTypes.GEO:
111          instance.DistanceMeasure = CVRPDistanceMeasure.Geo; break;
112        case TSPLIBEdgeWeightTypes.MAN_2D:
113        case TSPLIBEdgeWeightTypes.MAN_3D:
114          instance.DistanceMeasure = CVRPDistanceMeasure.Manhattan; break;
115        case TSPLIBEdgeWeightTypes.MAX_2D:
116        case TSPLIBEdgeWeightTypes.MAX_3D:
117          instance.DistanceMeasure = CVRPDistanceMeasure.Maximum; break;
118        default:
119          throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
120      }
121
122      instance.Name = parser.Name;
123      instance.Description = parser.Comment
124        + Environment.NewLine + Environment.NewLine
125        + GetDescription();
126
127      return instance;
128    }
129
130    private string GetPrettyName(string instanceIdentifier) {
131      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.CVRP\.(.*)\.vrp").Groups[1].Captures[0].Value;
132    }
133
134    private string GetDescription() {
135      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.