Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Problems.Instances.VehicleRouting/3.4/SolomonFormat/SolomonFormatInstanceProvider.cs @ 13042

Last change on this file since 13042 was 11478, checked in by pfleck, 10 years ago

#2229:

  • Added fall-back for solution parsing to try the own .opt-Format.
  • Parsing uses InvariantCulture instead of a specific one.
  • Small refactoring.
File size: 3.0 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.Collections.Generic;
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        reader.ReadLine(); // Solution
67
68        var routesQuery =
69          from line in reader.ReadAllLines()
70          where !string.IsNullOrEmpty(line)
71          let tokens = ExtractValue(line).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
72          let stops = tokens.Select(int.Parse).Select(s => s - 1)
73          select stops;
74
75        var routes = routesQuery.Select(s => s.ToArray()).ToArray();
76
77        instance.BestKnownTour = routes;
78      }
79    }
80
81    private static string ExtractValue(string line) {
82      return line.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Last().Trim();
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.