Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Problems.Instances.VehicleRouting/3.4/TSPLibFormat/TSPLibFormatInstanceProvider.cs @ 12961

Last change on this file since 12961 was 11286, checked in by pfleck, 10 years ago

#2225
Reverted parts of r10435 since the generic VRPInstanceProvider work accordingly after recent changes.

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Problems.Instances.TSPLIB;
25
26namespace HeuristicLab.Problems.Instances.VehicleRouting {
27  public abstract class TSPLibFormatInstanceProvider : VRPInstanceProvider<CVRPData> {
28    protected override CVRPData LoadData(Stream stream) {
29      return LoadInstance(new TSPLIBParser(stream));
30    }
31
32    public override bool CanImportData {
33      get { return true; }
34    }
35    public override CVRPData ImportData(string path) {
36      return LoadInstance(new TSPLIBParser(path));
37    }
38
39    private CVRPData LoadInstance(TSPLIBParser parser) {
40      parser.Parse();
41
42      var instance = new CVRPData();
43      instance.Dimension = parser.Dimension;
44      instance.Coordinates = parser.Vertices != null ? parser.Vertices : parser.DisplayVertices;
45      instance.Distances = parser.Distances;
46      instance.Capacity = parser.Capacity.Value;
47      instance.Demands = parser.Demands;
48      switch (parser.EdgeWeightType) {
49        case TSPLIBEdgeWeightTypes.ATT:
50          instance.DistanceMeasure = DistanceMeasure.Att; break;
51        case TSPLIBEdgeWeightTypes.CEIL_2D:
52          instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
53        case TSPLIBEdgeWeightTypes.EUC_2D:
54        case TSPLIBEdgeWeightTypes.EUC_3D:
55          instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
56        case TSPLIBEdgeWeightTypes.EXPLICIT:
57          instance.DistanceMeasure = DistanceMeasure.Direct; break;
58        case TSPLIBEdgeWeightTypes.GEO:
59          instance.DistanceMeasure = DistanceMeasure.Geo; break;
60        case TSPLIBEdgeWeightTypes.MAN_2D:
61        case TSPLIBEdgeWeightTypes.MAN_3D:
62          instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
63        case TSPLIBEdgeWeightTypes.MAX_2D:
64        case TSPLIBEdgeWeightTypes.MAX_3D:
65          instance.DistanceMeasure = DistanceMeasure.Maximum; break;
66        default:
67          throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
68      }
69
70      instance.Name = parser.Name;
71      instance.Description = parser.Comment
72        + Environment.NewLine + Environment.NewLine
73        + GetInstanceDescription();
74
75      return instance;
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.