1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.Instances.TSPLIB {
|
---|
28 | public class TSPLIBHeterogeneousPTSPInstanceProvider : TSPLIBInstanceProvider<PTSPData> {
|
---|
29 |
|
---|
30 | public override string Name {
|
---|
31 | get { return "TSPLIB (heterogeneous symmetric PTSP)"; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public override string Description {
|
---|
35 | get { return "Traveling Salesman Problem Library"; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | protected override string FileExtension { get { return "tsp"; } }
|
---|
39 |
|
---|
40 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
41 | foreach (var desc in base.GetDataDescriptors().OfType<TSPLIBDataDescriptor>()) {
|
---|
42 | desc.Name += " [0.1;0.9]";
|
---|
43 | desc.SolutionIdentifier = null;
|
---|
44 | yield return desc;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected override PTSPData LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null) {
|
---|
49 | parser.Parse();
|
---|
50 | if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
|
---|
51 | var instance = new PTSPData();
|
---|
52 | instance.Dimension = parser.Dimension;
|
---|
53 | instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
|
---|
54 | instance.Distances = parser.Distances;
|
---|
55 | switch (parser.EdgeWeightType) {
|
---|
56 | case TSPLIBEdgeWeightTypes.ATT:
|
---|
57 | instance.DistanceMeasure = DistanceMeasure.Att; break;
|
---|
58 | case TSPLIBEdgeWeightTypes.CEIL_2D:
|
---|
59 | instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
|
---|
60 | case TSPLIBEdgeWeightTypes.EUC_2D:
|
---|
61 | instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
|
---|
62 | case TSPLIBEdgeWeightTypes.EUC_3D:
|
---|
63 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
64 | case TSPLIBEdgeWeightTypes.EXPLICIT:
|
---|
65 | instance.DistanceMeasure = DistanceMeasure.Direct; break;
|
---|
66 | case TSPLIBEdgeWeightTypes.GEO:
|
---|
67 | instance.DistanceMeasure = DistanceMeasure.Geo; break;
|
---|
68 | case TSPLIBEdgeWeightTypes.MAN_2D:
|
---|
69 | instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
|
---|
70 | case TSPLIBEdgeWeightTypes.MAN_3D:
|
---|
71 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
72 | case TSPLIBEdgeWeightTypes.MAX_2D:
|
---|
73 | instance.DistanceMeasure = DistanceMeasure.Maximum; break;
|
---|
74 | case TSPLIBEdgeWeightTypes.MAX_3D:
|
---|
75 | throw new InvalidDataException("3D coordinates are not supported.");
|
---|
76 | default:
|
---|
77 | throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
|
---|
78 | }
|
---|
79 |
|
---|
80 | instance.Name = parser.Name;
|
---|
81 | instance.Description = parser.Comment
|
---|
82 | + Environment.NewLine + Environment.NewLine
|
---|
83 | + GetInstanceDescription();
|
---|
84 |
|
---|
85 | var random = new MarsagliaRandom((uint)(descriptor != null ? descriptor.Name : instance.Name).GetHashCode());
|
---|
86 | instance.Probabilities = Enumerable.Range(0, instance.Dimension).Select(_ => 0.1 + 0.9 * random.NextDouble()).ToArray();
|
---|
87 |
|
---|
88 | return instance;
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override void LoadSolution(TSPLIBParser parser, PTSPData instance) {
|
---|
92 | parser.Parse();
|
---|
93 | instance.BestKnownTour = parser.Tour.FirstOrDefault();
|
---|
94 | }
|
---|
95 |
|
---|
96 | public PTSPData LoadData(string tspFile, string tourFile, double? bestQuality) {
|
---|
97 | var data = LoadInstance(new TSPLIBParser(tspFile));
|
---|
98 | if (!String.IsNullOrEmpty(tourFile)) {
|
---|
99 | var tourParser = new TSPLIBParser(tourFile);
|
---|
100 | LoadSolution(tourParser, data);
|
---|
101 | }
|
---|
102 | if (bestQuality.HasValue)
|
---|
103 | data.BestKnownQuality = bestQuality.Value;
|
---|
104 | return data;
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|
108 | }
|
---|