Limit time between nodes

318 views
Skip to first unread message

tarun walia

unread,
May 26, 2021, 7:40:15 AM5/26/21
to or-tools-discuss
Hi,

I am using this example.


and I am trying to limit the maximum time between two nodes.

routing.addDimension(transitCallbackIndex, // transit callback
30, // allow waiting time
30, // vehicle maximum capacities
false, // start cumul to zero
"Time"); 

I am trying to change the highlighted value, but it is for the maximum time a vehicle can travel not the maximum time between two nodes.

Can someone please suggest is there a way where I can limit the maximum time between two nodes.

Mizux Seiha

unread,
May 26, 2021, 11:22:00 AM5/26/21
to or-tools-discuss
Why not simply change you time matrix (i.e. returning horizon+1 for each forbidden/too long arc) ? or directly forbid arc (i.e. use of `routing.NextVar(index).SetValues([])` ) which are not allowed ?

tarun walia

unread,
May 26, 2021, 12:07:16 PM5/26/21
to or-tools-discuss
Thanks for reply!

I am looking to solve this based on the vehicle used. Each vehicle will have its own max inter-node time

node1 -> node2 : time ->5
vehicle1 -> max Inter-node time 3
vehicle2 -> max inter-node time 6

In this case (node1 -> node2 ) is allowed on vehicle two but not on vehicle1.

Below is the code I am using

int[] driveTimeDimensionArr = new int[numberOfVehicles];
LongBinaryOperator[] driveTimeCallbacks = new LongBinaryOperator[numberOfVehicles];

for (int vehicle = 0; vehicle < numberOfVehicles; ++vehicle) {
                driveTimeCallbacks[vehicle] = builTimeCallback(manager, solverRequest, vehicle);// return time callback
                //drive time is different for different vehicle.
                driveTimeDimensionArr[vehicle] = routing.registerTransitCallback(driveTimeCallbacks[vehicle]);


}

 routing.addDimensionWithVehicleTransits(driveTimeDimensionArr
                    , 1000000, 100000, false, "Time");

 RoutingDimension driveTimeDimension = model.getMutableDimension("Time");

//Set time limit for vehicle.
 for (int vehicle = 0; vehicle < numberOfVehicles; ++vehicle) {
                driveTimeDimension.cumulVar(model.end(vehicle)).setMax(solverRequest.maxTravelTimeOfVehicles[vehicle]);
                //how to set max inter node time for each vehicle ??
 }

Mizux Seiha

unread,
May 27, 2021, 4:08:23 AM5/27/21
to or-tools-discuss
small note: If you have several vehicles with the same "constraint" you can reuse the previously register callback

e.g.

vehicle1 -> max Inter-node time 3
vehicle2 -> max inter-node time 6
vehicle3 -> max Inter-node time 3
vehicle4 -> max inter-node time 6

with register callback 1 for max Inter-node time 3, and 2 for max Inter-node time 6
you could pass [1, 2, 1, 2] to AddDimension()...

tarun walia

unread,
May 28, 2021, 12:58:30 AM5/28/21
to or-tools-discuss
All vehicles have the same constraints but having different values for constraints. Also, constraints values changes based upon the nodes visited by the vehicle.

Here how I achieved the above.

 private static LongBinaryOperator distanceCallback(
            RoutingIndexManager manager, SolverRequest solverRequest, int vehicleIndex) {
        return (firstIndex, secondIndex) -> {
            int firstNode = manager.indexToNode(firstIndex);
            int secondNode = manager.indexToNode(secondIndex);
            Vehicle vehicle = solverRequest.vehicles[vehicleIndex];
            long distance =  //get the distance between first node and second node for given vehicle.
            if (distance > vehicle.getMaxInterNodeDistance().) {
                //return some big value.
                return 500 * Constants.MILES_TO_METERS;
            }
            else
                return distance;
        };
    }

The other thing I want to implement is, this constraint is only for inter-node visits, and not from depot to node.

I am trying checking with if firstNode is depot and make a decision based on that. but somehow that is not working, appreciate any suggestion on that?



Reply all
Reply to author
Forward
0 new messages