Free cookie consent management tool by TermsFeed Policy Generator

source: branches/dataset-ids-2695/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs @ 15500

Last change on this file since 15500 was 14697, checked in by swagner, 8 years ago

#2737: Enhanced TSPLIBInstanceProvider to set best known quality even if no optimal solution is provided in the TSPLIB.

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.IO;
24using System.Linq;
25
26namespace HeuristicLab.Problems.Instances.TSPLIB {
27  public class TSPLIBTSPInstanceProvider : TSPLIBInstanceProvider<TSPData> {
28
29    public override string Name {
30      get { return "TSPLIB (symmetric TSP)"; }
31    }
32
33    public override string Description {
34      get { return "Traveling Salesman Problem Library"; }
35    }
36
37    protected override string FileExtension { get { return "tsp"; } }
38
39    protected override TSPData LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null) {
40      parser.Parse();
41      if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
42      var instance = new TSPData();
43      instance.Dimension = parser.Dimension;
44      instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
45      instance.Distances = parser.Distances;
46      switch (parser.EdgeWeightType) {
47        case TSPLIBEdgeWeightTypes.ATT:
48          instance.DistanceMeasure = DistanceMeasure.Att; break;
49        case TSPLIBEdgeWeightTypes.CEIL_2D:
50          instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
51        case TSPLIBEdgeWeightTypes.EUC_2D:
52          instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
53        case TSPLIBEdgeWeightTypes.EUC_3D:
54          throw new InvalidDataException("3D coordinates are not supported.");
55        case TSPLIBEdgeWeightTypes.EXPLICIT:
56          instance.DistanceMeasure = DistanceMeasure.Direct; break;
57        case TSPLIBEdgeWeightTypes.GEO:
58          instance.DistanceMeasure = DistanceMeasure.Geo; break;
59        case TSPLIBEdgeWeightTypes.MAN_2D:
60          instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
61        case TSPLIBEdgeWeightTypes.MAN_3D:
62          throw new InvalidDataException("3D coordinates are not supported.");
63        case TSPLIBEdgeWeightTypes.MAX_2D:
64          instance.DistanceMeasure = DistanceMeasure.Maximum; break;
65        case TSPLIBEdgeWeightTypes.MAX_3D:
66          throw new InvalidDataException("3D coordinates are not supported.");
67        default:
68          throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
69      }
70
71      instance.Name = parser.Name;
72      instance.Description = parser.Comment
73        + Environment.NewLine + Environment.NewLine
74        + GetInstanceDescription();
75      return instance;
76    }
77
78    protected override void LoadSolution(TSPLIBParser parser, TSPData instance) {
79      parser.Parse();
80      instance.BestKnownTour = parser.Tour.FirstOrDefault();
81    }
82
83    protected override void LoadQuality(double? bestQuality, TSPData instance) {
84      instance.BestKnownQuality = bestQuality;
85    }
86
87    public TSPData LoadData(string tspFile, string tourFile, double? bestQuality) {
88      var data = LoadInstance(new TSPLIBParser(tspFile));
89      if (!String.IsNullOrEmpty(tourFile)) {
90        var tourParser = new TSPLIBParser(tourFile);
91        LoadSolution(tourParser, data);
92      }
93      if (bestQuality.HasValue)
94        data.BestKnownQuality = bestQuality.Value;
95      return data;
96    }
97
98  }
99}
Note: See TracBrowser for help on using the repository browser.