Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs @ 17900

Last change on this file since 17900 was 17251, checked in by abeham, 5 years ago

#2521: finished refactoring TSP

File size: 6.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;
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    public override bool CanExportData => true;
99
100    public override void ExportData(TSPData instance, string path) {
101      using (var writer = new StreamWriter(File.OpenWrite(path))) {
102        writer.WriteLine("NAME: " + instance.Name);
103        writer.WriteLine("TYPE: TSP");
104        writer.WriteLine("COMMENT: " + instance.Description.Replace(Environment.NewLine, " \\\\ " ));
105        writer.WriteLine("DIMENSION: " + instance.Dimension);
106
107        if (instance.DistanceMeasure == DistanceMeasure.Euclidean) {
108          // TSPLIB only knows rounded euclidean distance
109          instance.Distances = instance.GetDistanceMatrix();
110          instance.DistanceMeasure = DistanceMeasure.Direct;
111        }
112
113        if (instance.DistanceMeasure == DistanceMeasure.Direct) {
114          writer.WriteLine("EDGE_WEIGHT_TYPE: EXPLICIT");
115          writer.WriteLine("EDGE_WEIGHT_FORMAT: UPPER_ROW");
116          if (instance.Coordinates != null && instance.Coordinates.GetLength(1) == 2) {
117            writer.WriteLine("DISPLAY_DATA_TYPE: TWOD_DISPLAY");
118          }
119        } else {
120          writer.Write("EDGE_WEIGHT_TYPE: ");
121          switch (instance.DistanceMeasure) {
122            case DistanceMeasure.RoundedEuclidean:
123              writer.WriteLine("EUC_2D");
124              break;
125            case DistanceMeasure.UpperEuclidean:
126              writer.WriteLine("CEIL_2D");
127              break;
128            case DistanceMeasure.Att:
129              writer.WriteLine("ATT");
130              break;
131            case DistanceMeasure.Geo:
132              writer.WriteLine("GEO");
133              break;
134            case DistanceMeasure.Manhattan:
135              writer.WriteLine("MAN_2D");
136              break;
137            case DistanceMeasure.Maximum:
138              writer.WriteLine("MAX_2D");
139              break;
140            default: throw new InvalidOperationException("Unknown distance measure: " + instance.DistanceMeasure);
141          }
142          writer.WriteLine("DISPLAY_DATA_TYPE: COORD_DISPLAY");
143        }
144
145        if (instance.DistanceMeasure == DistanceMeasure.Direct) {
146          writer.WriteLine("EDGE_WEIGHT_SECTION");
147          for (var i = 0; i < instance.Distances.GetLength(0) - 1; i++) {
148            for (var j = i + 1; j < instance.Distances.GetLength(1); j++) {
149              writer.Write(instance.Distances[i, j] + " ");
150            }
151            writer.WriteLine();
152          }
153        }
154
155        if (instance.Coordinates != null && instance.Coordinates.GetLength(1) == 2) {
156          if (instance.DistanceMeasure == DistanceMeasure.Direct)
157            writer.WriteLine("DISPLAY_DATA_SECTION");
158          else writer.WriteLine("NODE_COORD_SECTION");
159          for (var i = 1; i <= instance.Coordinates.GetLength(0); i++) {
160            writer.WriteLine(i + " " + instance.Coordinates[i - 1, 0] + " " + instance.Coordinates[i - 1, 1]);
161          }
162        }
163        writer.WriteLine("EOF");
164        writer.Flush();
165      }
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.