[8670] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 7 | using HeuristicLab.Common;
|
---|
| 8 | using HeuristicLab.PDPSimulation.Operators;
|
---|
| 9 | using HeuristicLab.Problems.VehicleRouting;
|
---|
| 10 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 11 | using HeuristicLab.Parameters;
|
---|
| 12 | using HeuristicLab.Data;
|
---|
| 13 |
|
---|
| 14 | namespace HeuristicLab.PDPSimulation {
|
---|
| 15 | [Item("WaitFirstWaitingStrategy", "A pickup and delivery waiting strategy.")]
|
---|
| 16 | [StorableClass]
|
---|
| 17 | public class WaitFirstWaitingStrategy : WaitingStrategy {
|
---|
| 18 | protected ValueParameter<BoolValue> OnlyFirstStopParameter {
|
---|
| 19 | get { return (ValueParameter<BoolValue>)Parameters["OnlyFirstStop"]; }
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public WaitFirstWaitingStrategy()
|
---|
| 23 | : base() {
|
---|
| 24 | Parameters.Add(new ValueParameter<BoolValue>("OnlyFirstStop", "Indicates, if waiting should only occur at the first stop.", new BoolValue(false)));
|
---|
| 25 | }
|
---|
| 26 | [StorableConstructor]
|
---|
| 27 | protected WaitFirstWaitingStrategy(bool deserializing) : base(deserializing) { }
|
---|
| 28 | protected WaitFirstWaitingStrategy(WaitFirstWaitingStrategy original, Cloner cloner)
|
---|
| 29 | : base(original, cloner) {
|
---|
| 30 | }
|
---|
| 31 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 32 | return new WaitFirstWaitingStrategy(this, cloner);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public static double GetSlack(double time, DynPDPProblemInstance instance, IVRPEncoding solution, Tour tour, int stop) {
|
---|
| 36 | double slack = 0;
|
---|
| 37 | double minBuffer = double.MaxValue;
|
---|
| 38 | int count = tour.Stops.Count;
|
---|
| 39 | if (!(instance as DynPDPProblemInstance).RelocateBackToDepot)
|
---|
| 40 | count -= 1;
|
---|
| 41 | for (int i = stop; i <= count; i++) {
|
---|
| 42 | int customer = 0;
|
---|
| 43 | if (i < tour.Stops.Count)
|
---|
| 44 | customer = tour.Stops[i];
|
---|
| 45 | int prev = 0;
|
---|
| 46 | if (i > 0)
|
---|
| 47 | prev = tour.Stops[i - 1];
|
---|
| 48 |
|
---|
| 49 | int depots = instance.Depots.Value;
|
---|
| 50 | double distance = instance.GetDistance(prev, customer, solution);
|
---|
| 51 | time += distance;
|
---|
| 52 |
|
---|
| 53 | double arrivalTime = time;
|
---|
| 54 | double deadline = 0;
|
---|
| 55 | if (customer > 0) {
|
---|
| 56 | double readyTime = instance.ReadyTime[customer + depots - 1];
|
---|
| 57 | if (time < readyTime) {
|
---|
| 58 | slack += readyTime - time;
|
---|
| 59 | time = readyTime;
|
---|
| 60 | arrivalTime = time;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | double serviceTime = instance.ServiceTime[customer - 1];
|
---|
| 64 | time += serviceTime;
|
---|
| 65 |
|
---|
| 66 | deadline = instance.DueTime[customer + depots - 1];
|
---|
| 67 | } else {
|
---|
| 68 | int depot = instance.GetDepot(prev, solution);
|
---|
| 69 | deadline = instance.DueTime[depot];
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | double buffer = Math.Max(0, Math.Max(slack, deadline - arrivalTime + slack));
|
---|
| 73 | if (buffer < minBuffer)
|
---|
| 74 | minBuffer = buffer;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return Math.Floor(minBuffer * 100) / 100;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public override double GetWaitingTime(double time, DynPDPProblemInstance instance, IVRPEncoding solution, Tour tour, int stop) {
|
---|
| 81 | bool onlyFirstStop = OnlyFirstStopParameter.Value.Value;
|
---|
| 82 |
|
---|
| 83 | int vehicle = solution.GetVehicleAssignment(solution.GetTourIndex(tour));
|
---|
| 84 | bool moving = instance.VehicleStates[vehicle] == DomainModel.VehicleState.Moving;
|
---|
| 85 |
|
---|
| 86 | if (onlyFirstStop) {
|
---|
| 87 | if ((!moving && stop != 0) || (moving && stop != 1))
|
---|
| 88 | return 0;
|
---|
| 89 | } else {
|
---|
| 90 | if (moving && stop == 0)
|
---|
| 91 | return 0;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | return GetSlack(time, instance, solution, tour, stop);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|