source: trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/SolomonFormat/SolomonFormatInstanceProvider.cs @ 14478

Last change on this file since 14478 was 14478, checked in by pfleck, 4 years ago

#2705

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.VehicleRouting {
27  public abstract class SolomonFormatInstanceProvider : VRPInstanceProvider<CVRPTWData> {
28    protected override CVRPTWData LoadData(Stream stream) {
29      return LoadInstance(new SolomonParser(stream));
30    }
31
32    public override bool CanImportData {
33      get { return true; }
34    }
35    public override CVRPTWData ImportData(string path) {
36      return LoadInstance(new SolomonParser(path));
37    }
38
39    private CVRPTWData LoadInstance(SolomonParser parser) {
40      parser.Parse();
41
42      var instance = new CVRPTWData();
43
44      instance.Dimension = parser.Cities + 1;
45      instance.Coordinates = parser.Coordinates;
46      instance.Capacity = parser.Capacity;
47      instance.Demands = parser.Demands;
48      instance.DistanceMeasure = DistanceMeasure.Euclidean;
49      instance.ReadyTimes = parser.Readytimes;
50      instance.ServiceTimes = parser.Servicetimes;
51      instance.DueTimes = parser.Duetimes;
52      instance.MaximumVehicles = parser.Vehicles;
53
54      instance.Name = parser.ProblemName;
55
56      return instance;
57    }
58
59    protected override void LoadSolution(Stream stream, CVRPTWData instance) {
60      using (var reader = new StreamReader(stream)) {
61        string instanceName = ExtractValue(reader.ReadLine());
62        string authors = ExtractValue(reader.ReadLine());
63        string date = ExtractValue(reader.ReadLine());
64        string reference = ExtractValue(reader.ReadLine());
65        switch (reader.ReadLine().Trim()) { // "Solution" or "Distance"
66          case "Solution":
67            var routesQuery = from line in reader.ReadAllLines()
68                              where !string.IsNullOrEmpty(line)
69                              let tokens = ExtractValue(line).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
70                              let stops = tokens.Select(int.Parse).Select(s => s - 1)
71                              select stops;
72            var routes = routesQuery.Select(s => s.ToArray()).ToArray();
73            instance.BestKnownTour = routes;
74            break;
75          case "Distance":
76            double quality = double.Parse(reader.ReadLine());
77            instance.BestKnownQuality = quality;
78            break;
79        }
80      }
81    }
82
83    private static string ExtractValue(string line) {
84      return line.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Last().Trim();
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.