Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11478 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: 4.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.Globalization;
25using System.IO;
26using System.Linq;
27
28namespace HeuristicLab.Problems.Instances.VehicleRouting {
29  public abstract class CordeauFormatInstanceProvider<TData> : VRPInstanceProvider<TData> where TData : MDCVRPData {
30    protected override TData LoadData(Stream stream) {
31      return LoadInstance(new CordeauParser(stream));
32    }
33
34    public override bool CanImportData {
35      get { return true; }
36    }
37    public override TData ImportData(string path) {
38      return LoadInstance(new CordeauParser(path));
39    }
40
41    internal abstract TData LoadInstance(CordeauParser parser);
42
43    protected override void LoadSolution(Stream stream, TData instance) {
44      using (var reader = new StreamReader(stream)) {
45        double costs = double.Parse(reader.ReadLine(), CultureInfo.InvariantCulture);
46
47        var toursPerDepotQuery =
48          from line in reader.ReadAllLines()
49          where !string.IsNullOrEmpty(line)
50          let tokens = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
51          let depot = int.Parse(tokens[0])
52          //let vehicle = int.Parse(tokens[1])
53          //let duration = double.Parse(tokens[2], CultureInfo.InvariantCulture)
54          //let load = double.Parse(tokens[3], new CultureInfo.InvariantCulture)
55          let customers = tokens.Skip(4).Where(t => !t.StartsWith("(")).Select(int.Parse)
56          let numberOfCustomers = customers.Count()
57          //let serviceTimes = tokens.Skip(5).Where(t => t.StartsWith("(")).Select(t => double.Parse(t.Trim('(', ')'), CultureInfo.InvariantCulture))
58          let stops = customers.Skip(1).Take(numberOfCustomers - 2).Select(s => s - 1)
59          select new { depot, /*vehicle,*/ stops } into assignment
60          group assignment by assignment.depot;
61
62        var toursPerDepot = toursPerDepotQuery.Select(d => d.Select(v => v.stops.ToArray()).ToArray()).ToArray();
63
64        instance.BestKnownTour = toursPerDepot.SelectMany(depot => depot).ToArray();
65
66        instance.BestKnownTourVehicleAssignment = DetermineTourToVehicleAssignment(toursPerDepot, instance.VehicleDepotAssignment);
67      }
68    }
69
70    private static int[] DetermineTourToVehicleAssignment(int[][][] toursPerDepot, int[] vehicleDepotAssignments) {
71      var usedVehiclesPerDepot = toursPerDepot.Select((d, i) => new { i, d }).ToDictionary(k => k.i, v => v.d.Length);
72      var availableVehiclesPerDepot = vehicleDepotAssignments.GroupBy(a => a).ToDictionary(k => k.Key, v => v.Count());
73
74      var tourToVehicle = new List<int>(vehicleDepotAssignments.Length);
75      var unusedVehicles = new List<int>();
76
77      int vehicle = 0;
78      for (int depot = 0; depot < toursPerDepot.Length; depot++) {
79        int used = usedVehiclesPerDepot[depot];
80        tourToVehicle.AddRange(Enumerable.Range(vehicle, used));
81        vehicle += used;
82
83        int unused = availableVehiclesPerDepot[depot] - used;
84        unusedVehicles.AddRange(Enumerable.Range(vehicle, unused));
85        vehicle += unused;
86      }
87      tourToVehicle.AddRange(unusedVehicles);
88
89      return tourToVehicle.ToArray();
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.