Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/08/14 16:25:08 (10 years ago)
Author:
pfleck
Message:

#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:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/CordeauFormat/CordeauFormatInstanceProvider.cs

    r11292 r11429  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
    2224using System.IO;
     25using System.Linq;
    2326
    2427namespace HeuristicLab.Problems.Instances.VehicleRouting {
    25   public abstract class CordeauFormatInstanceProvider<T> : VRPInstanceProvider<T> where T : MDCVRPData {
    26     protected override T LoadData(Stream stream) {
     28  public abstract class CordeauFormatInstanceProvider<TData> : VRPInstanceProvider<TData> where TData : MDCVRPData {
     29    protected override TData LoadData(Stream stream) {
    2730      return LoadInstance(new CordeauParser(stream));
    2831    }
     
    3134      get { return true; }
    3235    }
    33     public override T ImportData(string path) {
     36    public override TData ImportData(string path) {
    3437      return LoadInstance(new CordeauParser(path));
    3538    }
    3639
    37     internal abstract T LoadInstance(CordeauParser parser);
     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    }
    3893  }
    3994}
Note: See TracChangeset for help on using the changeset viewer.