source: trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/CordeauFormat/CordeauFormatInstanceProvider.cs @ 11429

Last change on this file since 11429 was 11429, checked in by pfleck, 8 years ago

#2229

  • Implemented parsing of own solution format for Cordeau instances. (Only works for CordeauMD p-class instances yet)
  • Added CordeauMD and CordeauMDTW solution files.
File size: 3.9 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 CordeauFormatInstanceProvider<TData> : VRPInstanceProvider<TData> where TData : MDCVRPData {
29    protected override TData LoadData(Stream stream) {
30      return LoadInstance(new CordeauParser(stream));
31    }
32
33    public override bool CanImportData {
34      get { return true; }
35    }
36    public override TData ImportData(string path) {
37      return LoadInstance(new CordeauParser(path));
38    }
39
40    internal abstract TData LoadInstance(CordeauParser parser);
41
42    protected override void LoadSolution(Stream stream, TData instance) {
43      using (var reader = new StreamReader(stream)) {
44        double costs = double.Parse(reader.ReadLine());
45
46        var toursPerDepotQuery =
47          from line in ReadAllLines(reader)
48          let tokens = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
49          let depot = int.Parse(tokens[0])
50          //let vehicle = int.Parse(tokens[1])
51          //let duration = double.Parse(tokens[2])
52          //let load = double.Parse(tokens[3])
53          let customers = tokens.Skip(4).Where(t => !t.StartsWith("(")).Select(int.Parse)
54          //let serviceTimes = tokens.Skip(5).Where(t => t.StartsWith("(")).Select(t => int.Parse(t.Trim('(', ')')))
55          let stops = customers.Where(s => s != 0).Select(s => s - 1)
56          select new { depot, /*vehicle,*/ stops } into assignment
57          group assignment by assignment.depot;
58
59        var toursPerDepot = toursPerDepotQuery.Select(d => d.Select(v => v.stops.ToArray()).ToArray()).ToArray();
60
61        instance.BestKnownTour = toursPerDepot.SelectMany(depot => depot).ToArray();
62
63        instance.BestKnownTourVehicleAssignment = DetermineTourToVehicleAssignment(toursPerDepot, instance.VehicleDepotAssignment);
64      }
65    }
66
67    private static int[] DetermineTourToVehicleAssignment(int[][][] toursPerDepot, int[] vehicleDepotAssignments) {
68      var usedVehiclesPerDepot = toursPerDepot.Select((d, i) => new { i, d }).ToDictionary(k => k.i, v => v.d.Length);
69      var availableVehiclesPerDepot = vehicleDepotAssignments.GroupBy(a => a).ToDictionary(k => k.Key, v => v.Count());
70
71      var tourToVehicle = new List<int>(vehicleDepotAssignments.Length);
72      var unusedVehicles = new List<int>();
73
74      int vehicle = 0;
75      for (int depot = 0; depot < toursPerDepot.Length; depot++) {
76        int used = usedVehiclesPerDepot[depot];
77        tourToVehicle.AddRange(Enumerable.Range(vehicle, used));
78        vehicle += used;
79
80        int unused = availableVehiclesPerDepot[depot] - used;
81        unusedVehicles.AddRange(Enumerable.Range(vehicle, unused));
82        vehicle += unused;
83      }
84      tourToVehicle.AddRange(unusedVehicles);
85
86      return tourToVehicle.ToArray();
87    }
88
89    private IEnumerable<string> ReadAllLines(StreamReader reader) {
90      while (!reader.EndOfStream)
91        yield return reader.ReadLine();
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.