Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Optimizers/LocalUpdate/PriorityDispatching.cs @ 13231

Last change on this file since 13231 was 8813, checked in by svonolfe, 12 years ago

Added earliest due time and nearest destination dispatching (#1955)

File size: 5.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Encodings.RealVectorEncoding;
8using HeuristicLab.Parameters;
9using HeuristicLab.PDPSimulation.DomainModel;
10using HeuristicLab.PDPSimulation.Operators;
11using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
12
13namespace HeuristicLab.PDPSimulation {
14  [Item("PriorityDispatching", "")]
15  [StorableClass]
16  public abstract class PriorityDispatching : Dispatching {
17    [StorableConstructor]
18    protected PriorityDispatching(bool deserializing) : base(deserializing) { }
19    protected PriorityDispatching(PriorityDispatching original, Cloner cloner) : base(original, cloner) { }
20    protected PriorityDispatching() {
21    }
22
23    protected abstract double CalculatePriority(IDictionary<string, double> variables);
24
25    protected override void GetHighestPriorityOrder(DynPDPProblemInstance instance, Vehicle vehicle, IEnumerable<Order> orders, out Order order, out double priority) {
26      Order best = null;
27      double bestPriority = double.MinValue;
28      var pickupOrdersByLocation = (from o in orders
29                                   where IsPickup(o, instance)
30                                   group o by new { X = o.PickupXCoord, Y = o.PickupYCoord })
31                                   .ToDictionary(x => new XY(x.Key.X, x.Key.Y), y => y.ToArray(), new XYEqComparer());
32      var deliveryOrdersByLocation = (from o in orders
33                                      where !IsPickup(o, instance) && o.Vehicle == vehicle.Id
34                                      group o by new { X = o.DeliveryXCoord, Y = o.DeliveryYCoord })
35                                      .ToDictionary(x => new XY(x.Key.X, x.Key.Y), y => y.ToArray(), new XYEqComparer());
36      var destinations = deliveryOrdersByLocation.Keys.ToArray();
37      foreach (var o in orders) {
38        if (IsPickup(o, instance) && o.Demand > vehicle.Capacity) continue;
39       
40         double prio;
41         if (!IsPickup(o, instance) && vehicle.TargetPositionX == o.DeliveryXCoord && vehicle.TargetPositionY == o.DeliveryYCoord) {
42           prio = double.MaxValue;
43         } else {
44           var target = new XY(o.PickupXCoord, o.PickupYCoord);
45           if (!IsPickup(o, instance)) target = new XY(o.DeliveryXCoord, o.DeliveryYCoord);
46
47           var destinationDifference = destinations.Select(x => scenario.DistanceMeasure.GetDistance(target.X, target.Y, x.X, x.Y)).ToArray();
48           var courierDifference = GetVehicles().Where(x => x != vehicle).Select(x => new XY(x.TargetPositionX, x.TargetPositionY))
49             .Select(x => scenario.DistanceMeasure.GetDistance(target.X, target.Y, x.X, x.Y)).ToArray();
50
51           var commonDestinations = orders.Where(x => !IsPickup(x, instance) && x.Vehicle == vehicle.Id &&
52                                    x.DeliveryXCoord == o.PickupXCoord &&
53                                    x.DeliveryYCoord == o.PickupYCoord);
54
55           var variables = new Dictionary<string, double>();
56           variables.Add("Distance", scenario.DistanceMeasure.GetDistance(vehicle.TargetPositionX, vehicle.TargetPositionY, target.X, target.Y));
57           variables.Add("EarliestTimeOfArrival", vehicle.ReadyTime + variables["Distance"]);
58           variables.Add("DueDate", o.DeliveryDueTime - GetSimulationTime());
59           variables.Add("StartDate", IsPickup(o, instance) ? o.PickupReadyTime - GetSimulationTime() : 0);
60           variables.Add("PickupOrdersAtTarget", pickupOrdersByLocation.ContainsKey(target) ? pickupOrdersByLocation[target].Count() : 0);
61           variables.Add("PickupOrderItemsAtTarget", pickupOrdersByLocation.ContainsKey(target) ? pickupOrdersByLocation[target].Sum(x => x.Demand) : 0);
62           variables.Add("DeliveryOrdersAtTarget", deliveryOrdersByLocation.ContainsKey(target) ? deliveryOrdersByLocation[target].Count() : 0);
63           variables.Add("DeliveryOrderItemsAtTarget", deliveryOrdersByLocation.ContainsKey(target) ? deliveryOrdersByLocation[target].Sum(x => x.Demand) : 0);
64           variables.Add("AverageDistanceToDestinations", destinationDifference.Any() ? destinationDifference.Average() : 0);
65           variables.Add("MinimumDistanceToDestinations", destinationDifference.Any() ? destinationDifference.Min() : 0);
66           variables.Add("MaximumDistanceToDestinations", destinationDifference.Any() ? destinationDifference.Max() : 0);
67           variables.Add("NumberOfOtherCouriersToTarget", GetVehicles().Count > 2 ? GetVehicles().Where(x => x != vehicle).Count(x => target.X == x.TargetPositionX && target.Y == x.TargetPositionY) : 0);
68           variables.Add("AverageDistanceToOtherCouriers", courierDifference.Any() ? courierDifference.Average() : 0);
69           variables.Add("MinimumDistanceToOtherCouriers", courierDifference.Any() ? courierDifference.Min() : 0);
70           variables.Add("MaximumDistanceToOtherCouriers", courierDifference.Any() ? courierDifference.Max() : 0);
71           variables.Add("DemandSize", o.Demand);
72           variables.Add("EDD", commonDestinations.Any() ? commonDestinations.Min(d => d.DeliveryDueTime
73             - scenario.DistanceMeasure.GetDistance(vehicle.TargetPositionX, vehicle.TargetPositionY, d.DeliveryXCoord, d.DeliveryYCoord)
74             - GetSimulationTime()) : 0);
75           variables.Add("LeadTime", o.PickupReadyTime - GetSimulationTime());
76
77           prio = CalculatePriority(variables);
78         }
79
80        if (prio > bestPriority) {
81          bestPriority = prio;
82          best = o;
83        }
84      }
85
86      order = best != null ? best : orders.First();
87      priority = bestPriority;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.