Search code examples
or-toolsvehicle-routing

Add node precedence


I'm currently developing Python code to solve a VRP problem using OR-Tools. I'm stuck because I can't set a strict precedence constraint between a pair of nodes. For instance, let's assume I have 10 different nodes (depot-1-2-3-4-5-6-7-8-9-10) and two vehicles with varying travel speeds. My objective is that in any solution, node 3 is visited immediately before node 7 by either of the two vehicles (it doesn't matter which one). An example solution could be:

Vehicle 1: depot-1-4-10-6-2-depot Vehicle 2: depot-9-5-3-7-8-depot

The varying speeds of the vehicles prevent me from applying the standard PDP example, which involves changing from cumulVar(pickup_index) <= cumulVar(delivery_index) to cumulVar(pickup_index) == cumulVar(delivery_index) + "distance between pickup and delivery index". To address this issue, I attempted to use AddNodePrecedence. However, the code still does not produce the desired result; node 3 is not positioned exactly before node 7 as intended. If you could provide me with a straightforward explanation and a code example, that would be very helpful.


Solution

  • why don't you merge the two nodes ?

    Treat the pair as one. Distance to the merged node is distance to 3. Distance from the merged node is distance from 7.

    Then you add a min to the slack var of the merged node to be the transit from 3 to 7.