[13484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[13484] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.IO;
|
---|
| 24 | using System.Linq;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.Instances.TSPLIB {
|
---|
| 27 | public abstract class TSPLIBPTSPInstanceProvider : TSPLIBInstanceProvider<PTSPData> {
|
---|
| 28 |
|
---|
| 29 | public override string Description {
|
---|
| 30 | get { return "Traveling Salesman Problem Library (adapted for generating PTSP instances)"; }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | protected override string FileExtension { get { return "tsp"; } }
|
---|
| 34 |
|
---|
| 35 | protected override PTSPData LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null) {
|
---|
| 36 | parser.Parse();
|
---|
| 37 | if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
|
---|
| 38 | var instance = new PTSPData();
|
---|
| 39 | instance.Dimension = parser.Dimension;
|
---|
| 40 | instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
|
---|
| 41 | instance.Distances = parser.Distances;
|
---|
| 42 | switch (parser.EdgeWeightType) {
|
---|
| 43 | case TSPLIBEdgeWeightTypes.ATT:
|
---|
| 44 | instance.DistanceMeasure = DistanceMeasure.Att; break;
|
---|
| 45 | case TSPLIBEdgeWeightTypes.CEIL_2D:
|
---|
| 46 | instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
|
---|
| 47 | case TSPLIBEdgeWeightTypes.EUC_2D:
|
---|
| 48 | instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
|
---|
| 49 | case TSPLIBEdgeWeightTypes.EUC_3D:
|
---|
| 50 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
| 51 | case TSPLIBEdgeWeightTypes.EXPLICIT:
|
---|
| 52 | instance.DistanceMeasure = DistanceMeasure.Direct; break;
|
---|
| 53 | case TSPLIBEdgeWeightTypes.GEO:
|
---|
| 54 | instance.DistanceMeasure = DistanceMeasure.Geo; break;
|
---|
| 55 | case TSPLIBEdgeWeightTypes.MAN_2D:
|
---|
| 56 | instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
|
---|
| 57 | case TSPLIBEdgeWeightTypes.MAN_3D:
|
---|
| 58 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
| 59 | case TSPLIBEdgeWeightTypes.MAX_2D:
|
---|
| 60 | instance.DistanceMeasure = DistanceMeasure.Maximum; break;
|
---|
| 61 | case TSPLIBEdgeWeightTypes.MAX_3D:
|
---|
| 62 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
| 63 | default:
|
---|
| 64 | throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | instance.Name = parser.Name;
|
---|
| 68 | instance.Description = parser.Comment
|
---|
| 69 | + Environment.NewLine + Environment.NewLine
|
---|
| 70 | + GetInstanceDescription();
|
---|
| 71 |
|
---|
| 72 | instance.Probabilities = GetProbabilities(descriptor, instance);
|
---|
| 73 | return instance;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | protected abstract double[] GetProbabilities(IDataDescriptor descriptor, PTSPData instance);
|
---|
| 77 |
|
---|
| 78 | protected override void LoadSolution(TSPLIBParser parser, PTSPData instance) {
|
---|
| 79 | parser.Parse();
|
---|
| 80 | instance.BestKnownTour = parser.Tour.FirstOrDefault();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[15259] | 83 | protected override void LoadQuality(double? bestQuality, PTSPData instance) {
|
---|
| 84 | instance.BestKnownQuality = bestQuality;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[13484] | 87 | public PTSPData 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 | }
|
---|