Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PTSP/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBHomogeneousPTSPInstanceProvider.cs @ 13412

Last change on this file since 13412 was 13412, checked in by abeham, 9 years ago

#2221:

  • Completely refactored PTSP branch
    • Added two sets of problem instances based on TSPLIB: homogeneous and heterogeneous
    • Implemented missing EvaluateByCoordinates for 1-shift moves
    • Made it clear that move evaluators are for estimated PTSP only
    • Changed parameter realization from a rather strange list of list of ints to a list of bool arrays
    • Reusing code of the 2-opt and 1-shift move evaluators in 2.5 move evaluator
    • Introducing distance calculators to properly handle the case when no distance matrix is given (previous code only worked with distance matrix and without only with euclidean distance in some settings)
    • Fixed several smaller code issues: protected, static, method parameters, copy & paste, interfaces, naming, parameters, serialization hooks, license headers, doc comments, data types
File size: 5.0 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Globalization;
25using System.IO;
26using System.Linq;
27
28namespace HeuristicLab.Problems.Instances.TSPLIB {
29  public class TSPLIBHomogeneousPTSPInstanceProvider : TSPLIBInstanceProvider<PTSPData> {
30    private static readonly double[] probabilities = new[] { 0.1, 0.2, 0.4, 0.5, 0.6, 0.8, 0.9, 1.0 };
31
32    public override string Name {
33      get { return "TSPLIB (homogeneous symmetric PTSP)"; }
34    }
35
36    public override string Description {
37      get { return "Traveling Salesman Problem Library"; }
38    }
39
40    protected override string FileExtension { get { return "tsp"; } }
41
42    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
43      var tspDescriptors = base.GetDataDescriptors().OfType<TSPLIBDataDescriptor>();
44      foreach (var desc in tspDescriptors) {
45        foreach (var p in probabilities) {
46          yield return new TSPLIBHomogeneousPTSPDataDescriptor(
47            desc.Name + "-" + p.ToString(CultureInfo.InvariantCulture.NumberFormat),
48            desc.Description,
49            desc.InstanceIdentifier,
50            p == 1.0 ? desc.SolutionIdentifier : null,
51            p);
52        }
53      }
54    }
55
56    protected override PTSPData LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null) {
57      parser.Parse();
58      if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
59      var instance = new PTSPData();
60      instance.Dimension = parser.Dimension;
61      instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
62      instance.Distances = parser.Distances;
63      switch (parser.EdgeWeightType) {
64        case TSPLIBEdgeWeightTypes.ATT:
65          instance.DistanceMeasure = DistanceMeasure.Att; break;
66        case TSPLIBEdgeWeightTypes.CEIL_2D:
67          instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
68        case TSPLIBEdgeWeightTypes.EUC_2D:
69          instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
70        case TSPLIBEdgeWeightTypes.EUC_3D:
71          throw new InvalidDataException("3D coordinates are not supported.");
72        case TSPLIBEdgeWeightTypes.EXPLICIT:
73          instance.DistanceMeasure = DistanceMeasure.Direct; break;
74        case TSPLIBEdgeWeightTypes.GEO:
75          instance.DistanceMeasure = DistanceMeasure.Geo; break;
76        case TSPLIBEdgeWeightTypes.MAN_2D:
77          instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
78        case TSPLIBEdgeWeightTypes.MAN_3D:
79          throw new InvalidDataException("3D coordinates are not supported.");
80        case TSPLIBEdgeWeightTypes.MAX_2D:
81          instance.DistanceMeasure = DistanceMeasure.Maximum; break;
82        case TSPLIBEdgeWeightTypes.MAX_3D:
83          throw new InvalidDataException("3D coordinates are not supported.");
84        default:
85          throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
86      }
87
88      instance.Name = parser.Name;
89      instance.Description = parser.Comment
90        + Environment.NewLine + Environment.NewLine
91        + GetInstanceDescription();
92      var ptspDesc = descriptor as TSPLIBHomogeneousPTSPDataDescriptor;
93      instance.Probabilities = ptspDesc != null ? Enumerable.Range(0, instance.Dimension).Select(_ => ptspDesc.Probability).ToArray()
94                                                : Enumerable.Range(0, instance.Dimension).Select(_ => 0.5).ToArray();
95
96      return instance;
97    }
98
99    protected override void LoadSolution(TSPLIBParser parser, PTSPData instance) {
100      parser.Parse();
101      instance.BestKnownTour = parser.Tour.FirstOrDefault();
102    }
103
104    public PTSPData LoadData(string tspFile, string tourFile, double? bestQuality) {
105      var data = LoadInstance(new TSPLIBParser(tspFile));
106      if (!String.IsNullOrEmpty(tourFile)) {
107        var tourParser = new TSPLIBParser(tourFile);
108        LoadSolution(tourParser, data);
109      }
110      if (bestQuality.HasValue)
111        data.BestKnownQuality = bestQuality.Value;
112      return data;
113    }
114
115  }
116}
Note: See TracBrowser for help on using the repository browser.