[7881] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7881] | 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 |
|
---|
[11931] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Globalization;
|
---|
[7881] | 25 | using System.IO;
|
---|
[11931] | 26 | using System.Linq;
|
---|
[7881] | 27 |
|
---|
| 28 | namespace HeuristicLab.Problems.Instances.VehicleRouting {
|
---|
[11931] | 29 | public abstract class CordeauFormatInstanceProvider<TData> : VRPInstanceProvider<TData> where TData : MDCVRPData {
|
---|
| 30 | protected override TData LoadData(Stream stream) {
|
---|
[7881] | 31 | return LoadInstance(new CordeauParser(stream));
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[8192] | 34 | public override bool CanImportData {
|
---|
| 35 | get { return true; }
|
---|
| 36 | }
|
---|
[11931] | 37 | public override TData ImportData(string path) {
|
---|
[7881] | 38 | return LoadInstance(new CordeauParser(path));
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[11931] | 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 | }
|
---|
[7881] | 91 | }
|
---|
| 92 | }
|
---|