Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.Instances.VehicleRouting/3.4/SolomonFormat/SolomonFormatInstanceProvider.cs @ 16671

Last change on this file since 16671 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

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