Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/WaitingStrategies/WaitFirstWaitingStrategy.cs @ 16092

Last change on this file since 16092 was 8670, checked in by svonolfe, 12 years ago

Added first version of the dynamic vehicle routing addon (#1955)

File size: 3.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.Common;
8using HeuristicLab.PDPSimulation.Operators;
9using HeuristicLab.Problems.VehicleRouting;
10using HeuristicLab.Problems.VehicleRouting.Interfaces;
11using HeuristicLab.Parameters;
12using HeuristicLab.Data;
13
14namespace 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}
Note: See TracBrowser for help on using the repository browser.