[7465] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7465] | 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 {
|
---|
[7618] | 27 | public class TSPLIBTSPInstanceProvider : TSPLIBInstanceProvider<TSPData> {
|
---|
[7470] | 28 |
|
---|
[7465] | 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 |
|
---|
[7618] | 37 | protected override string FileExtension { get { return "tsp"; } }
|
---|
[7465] | 38 |
|
---|
[13484] | 39 | protected override TSPData LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null) {
|
---|
[7538] | 40 | parser.Parse();
|
---|
| 41 | if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
|
---|
[7548] | 42 | var instance = new TSPData();
|
---|
[7538] | 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:
|
---|
[7872] | 48 | instance.DistanceMeasure = DistanceMeasure.Att; break;
|
---|
[7538] | 49 | case TSPLIBEdgeWeightTypes.CEIL_2D:
|
---|
[7872] | 50 | instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
|
---|
[7538] | 51 | case TSPLIBEdgeWeightTypes.EUC_2D:
|
---|
[7872] | 52 | instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
|
---|
[7538] | 53 | case TSPLIBEdgeWeightTypes.EUC_3D:
|
---|
| 54 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
| 55 | case TSPLIBEdgeWeightTypes.EXPLICIT:
|
---|
[7872] | 56 | instance.DistanceMeasure = DistanceMeasure.Direct; break;
|
---|
[7538] | 57 | case TSPLIBEdgeWeightTypes.GEO:
|
---|
[7872] | 58 | instance.DistanceMeasure = DistanceMeasure.Geo; break;
|
---|
[7538] | 59 | case TSPLIBEdgeWeightTypes.MAN_2D:
|
---|
[7872] | 60 | instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
|
---|
[7538] | 61 | case TSPLIBEdgeWeightTypes.MAN_3D:
|
---|
| 62 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
| 63 | case TSPLIBEdgeWeightTypes.MAX_2D:
|
---|
[7872] | 64 | instance.DistanceMeasure = DistanceMeasure.Maximum; break;
|
---|
[7538] | 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
|
---|
[7618] | 74 | + GetInstanceDescription();
|
---|
[7465] | 75 | return instance;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[7618] | 78 | protected override void LoadSolution(TSPLIBParser parser, TSPData instance) {
|
---|
| 79 | parser.Parse();
|
---|
| 80 | instance.BestKnownTour = parser.Tour.FirstOrDefault();
|
---|
[7465] | 81 | }
|
---|
| 82 |
|
---|
[7618] | 83 | public TSPData LoadData(string tspFile, string tourFile, double? bestQuality) {
|
---|
| 84 | var data = LoadInstance(new TSPLIBParser(tspFile));
|
---|
| 85 | if (!String.IsNullOrEmpty(tourFile)) {
|
---|
| 86 | var tourParser = new TSPLIBParser(tourFile);
|
---|
| 87 | LoadSolution(tourParser, data);
|
---|
| 88 | }
|
---|
| 89 | if (bestQuality.HasValue)
|
---|
| 90 | data.BestKnownQuality = bestQuality.Value;
|
---|
| 91 | return data;
|
---|
[7465] | 92 | }
|
---|
[7618] | 93 |
|
---|
[7465] | 94 | }
|
---|
| 95 | }
|
---|