- Timestamp:
- 10/08/14 16:25:08 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/CordeauFormat/CordeauFormatInstanceProvider.cs
r11292 r11429 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 22 24 using System.IO; 25 using System.Linq; 23 26 24 27 namespace 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) { 27 30 return LoadInstance(new CordeauParser(stream)); 28 31 } … … 31 34 get { return true; } 32 35 } 33 public override T ImportData(string path) {36 public override TData ImportData(string path) { 34 37 return LoadInstance(new CordeauParser(path)); 35 38 } 36 39 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 } 38 93 } 39 94 }
Note: See TracChangeset
for help on using the changeset viewer.