Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PTSP/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBPTSPInstanceProvider.cs @ 13492

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

#2221:

  • implemented review comments
    • hid rng as private class, implemented djb2 hash function (hash function implementation may also change)
    • added missing probabilities
    • base class for instance providers
    • prebuild event events
    • build platforms
    • unit test will be removed on trunk integration
    • corrected assembly file version
    • distance calculator parameter was not hidden, can be changed by user, updates distance matrix
    • fixed performance problems (ouch!) also for estimated ptsp (inlined GetDistance method)
  • added moves (full evaluation) for analytical tsp
  • added local improvement operators for analytical ptsp
  • added recalculation of distance matrix when parameters change
  • still lots of other changes
File size: 4.0 KB
RevLine 
[7465]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
22using System;
23using System.IO;
24using System.Linq;
25
26namespace HeuristicLab.Problems.Instances.TSPLIB {
[13470]27  public abstract class TSPLIBPTSPInstanceProvider : TSPLIBInstanceProvider<PTSPData> {
[7470]28
[7465]29    public override string Description {
[13470]30      get { return "Traveling Salesman Problem Library (adapted for generating PTSP instances)"; }
[7465]31    }
32
[7618]33    protected override string FileExtension { get { return "tsp"; } }
[7465]34
[13412]35    protected override PTSPData LoadInstance(TSPLIBParser parser, IDataDescriptor descriptor = null) {
[7538]36      parser.Parse();
37      if (parser.FixedEdges != null) throw new InvalidDataException("TSP instance " + parser.Name + " contains fixed edges which are not supported by HeuristicLab.");
[13412]38      var instance = new PTSPData();
[7538]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:
[7872]44          instance.DistanceMeasure = DistanceMeasure.Att; break;
[7538]45        case TSPLIBEdgeWeightTypes.CEIL_2D:
[7872]46          instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
[7538]47        case TSPLIBEdgeWeightTypes.EUC_2D:
[7872]48          instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
[7538]49        case TSPLIBEdgeWeightTypes.EUC_3D:
50          throw new InvalidDataException("3D coordinates are not supported.");
51        case TSPLIBEdgeWeightTypes.EXPLICIT:
[7872]52          instance.DistanceMeasure = DistanceMeasure.Direct; break;
[7538]53        case TSPLIBEdgeWeightTypes.GEO:
[7872]54          instance.DistanceMeasure = DistanceMeasure.Geo; break;
[7538]55        case TSPLIBEdgeWeightTypes.MAN_2D:
[7872]56          instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
[7538]57        case TSPLIBEdgeWeightTypes.MAN_3D:
58          throw new InvalidDataException("3D coordinates are not supported.");
59        case TSPLIBEdgeWeightTypes.MAX_2D:
[7872]60          instance.DistanceMeasure = DistanceMeasure.Maximum; break;
[7538]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
[7618]70        + GetInstanceDescription();
[13412]71
[13470]72      instance.Probabilities = GetProbabilities(descriptor, instance);
[7465]73      return instance;
74    }
75
[13470]76    protected abstract double[] GetProbabilities(IDataDescriptor descriptor, PTSPData instance);
77
[13412]78    protected override void LoadSolution(TSPLIBParser parser, PTSPData instance) {
[7618]79      parser.Parse();
80      instance.BestKnownTour = parser.Tour.FirstOrDefault();
[7465]81    }
82
[13412]83    public PTSPData LoadData(string tspFile, string tourFile, double? bestQuality) {
[7618]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}
Note: See TracBrowser for help on using the repository browser.