Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBPTSPInstanceProvider.cs

Last change on this file was 17265, checked in by abeham, 5 years ago

#2521: remove usage of solutions or best-known quality when converting TSPLIB instances to pTSP instances

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
24
25namespace HeuristicLab.Problems.Instances.TSPLIB {
26  public abstract class TSPLIBPTSPInstanceProvider : TSPLIBInstanceProvider<PTSPData> {
27
28    public override string Description {
29      get { return "Traveling Salesman Problem Library (adapted for generating PTSP instances)"; }
30    }
31
32    protected override string FileExtension { get { return "tsp"; } }
33
34    protected override PTSPData LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null) {
35      parser.Parse();
36      if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
37      var instance = new PTSPData();
38      instance.Dimension = parser.Dimension;
39      instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
40      instance.Distances = parser.Distances;
41      switch (parser.EdgeWeightType) {
42        case TSPLIBEdgeWeightTypes.ATT:
43          instance.DistanceMeasure = DistanceMeasure.Att; break;
44        case TSPLIBEdgeWeightTypes.CEIL_2D:
45          instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
46        case TSPLIBEdgeWeightTypes.EUC_2D:
47          instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
48        case TSPLIBEdgeWeightTypes.EUC_3D:
49          throw new InvalidDataException("3D coordinates are not supported.");
50        case TSPLIBEdgeWeightTypes.EXPLICIT:
51          instance.DistanceMeasure = DistanceMeasure.Direct; break;
52        case TSPLIBEdgeWeightTypes.GEO:
53          instance.DistanceMeasure = DistanceMeasure.Geo; break;
54        case TSPLIBEdgeWeightTypes.MAN_2D:
55          instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
56        case TSPLIBEdgeWeightTypes.MAN_3D:
57          throw new InvalidDataException("3D coordinates are not supported.");
58        case TSPLIBEdgeWeightTypes.MAX_2D:
59          instance.DistanceMeasure = DistanceMeasure.Maximum; break;
60        case TSPLIBEdgeWeightTypes.MAX_3D:
61          throw new InvalidDataException("3D coordinates are not supported.");
62        default:
63          throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
64      }
65
66      instance.Name = parser.Name;
67      instance.Description = parser.Comment
68        + Environment.NewLine + Environment.NewLine
69        + GetInstanceDescription();
70
71      instance.Probabilities = GetProbabilities(descriptor, instance);
72      return instance;
73    }
74
75    protected abstract double[] GetProbabilities(IDataDescriptor descriptor, PTSPData instance);
76
77    public override PTSPData LoadData(IDataDescriptor id) {
78      var tsplibId = (id as TSPLIBDataDescriptor);
79      if (tsplibId != null) tsplibId.SolutionIdentifier = null;
80      return base.LoadData(tsplibId ?? id);
81    }
82
83    protected override void LoadSolution(TSPLIBParser parser, PTSPData instance) {
84      // solutions are not defined for pTSP
85    }
86
87    protected override void LoadQuality(double? bestQuality, PTSPData instance) {
88      // best known qualities are not defined for pTSP
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.