Set max driving time and vehicle selection

Hi, I am wondering if:

(1) jsprit provides a feature to set max driving time for the driver?

(2) there are two vehicle types with different operation time, e.g., vehicle A can do roundtrip of 20 hrs, while vehicle B can do roundtrip of 10 hrs. The problem is jsprit may select vehicle A for route that takes only 9 hrs. Since this route takes only 9 hrs, then vehicle B is more appropriate to serve this route. How can we select the best vehicles for the routes? Also, is it possible to assign vehicles of type B a priority to use first?

Thank you!


ad 1) This should be easily possible with a custom constraint. First, you need to record the driving time in your vehicle route. Second, you always need to check whether this driving time and the additional driving time of a new insertion/job exceeds max driving time.

ad 2) I would try to make vehicles of type A more expensive in terms of fixed costs as well as costs per transport time unit.

Best, Stefan

Thanks Stefan, I was thinking along those lines. But, actually I am looking for max working hours (which includes driving + service time at service + any waiting time). In other words, I am looking for time from start till the end of the route.

Here is my thoughts/logic:

// assume that we have a route

  1. get the arrival time of first act/service on the route, i.e., arrivalTimeForFirstActOnRoute

  2. get driving time from depot to that first service as follows:
    drivingTimeFromDepotToFirstAct = costMatrix.getTransportTime(iFacts.getRoute().getStart().getLocation(), firstActOnRoute.getLocation(), 0, null, null);

  3. get the actual departure time of route (vehicle) as follows:
    actualDept = arrivalTimeForFirstActOnRoute - drivingTimeFromDepotToFirstAct;

  4. get actual end time of route (vehicle) as follows:
    actualEnd = iFacts.getRoute().getEnd().getArrTime();

  5. calculate the woking time needed to insert newAct as follows:
    workingTimeForNewAct = get driving Time From PrevAct To NewAct + get driving Time From NewAct To NextAct + newAct.getOperationTime();

  6. get the total working time when newAct is inserted
    TotalWorkingTime = (actualEnd - actualDept) + workingTimeForNewAct

  7. then check TotalWorkingTime if it is not in a specific range, e.g,
    if (x < TotalWorkingTime < y) then return ConstraintsStatus.NOT_FULFILLED;

I tried this solution, but for some routes, I got the TotalWorkingTime to be in this range which I don’t want.

Am I missing anything here, or doing something wrong?

Thanks so much!

Just to follow up, even if I check just for max working time, for example:

if (TotalWorkingTime > specified total duration) then return ConstraintsStatus.NOT_FULFILLED;

I still get some routes with more than “specified total duration” for the vehicle. Any advice?


From what I see I do not have any idea why your approach should not work. I would always look at how time windows are implemented in jsprit, i.e. look at the according states and constraints, and implement this max working time feature in much the same way.

Ok, thanks, I will look into it :slightly_smiling:

Hi Karam,

I think two things are missing in your approach:

  1. In Step 6, you need to deduct driving time from PrevAct to NextAct, as it was part of the route before the insertion of NewAct but not after.

  2. You also need to take into account the waiting time at activities. For example, suppose before the insertion, the vehicle needs to wait for 2 hours at NextAct, and after the insertion, it waits for 1 hour at NextAct, then in this case the insertion does not result in an increase in TotalWorkingTime.

Btw, you might want to check out #99 and the playground of jsprit.

Best regards,


Hi He,

You are right, nice catch. Thanks for your help.


Hi, has anyone looked at this further?

It might be useful for some stuff I’m doing to have a cost dependent on the total working time of the vehicle - i.e. from say drive-to-first stop time to return to depot time. The calculation is pretty much the same as the kind of max operating time as discussed above.

@jie31best your point 2. about taking account of waiting time is very relevant here. I can’t see anyway to calculate this properly in an insertion which doesn’t involve parsing the whole route to take account of changes in waiting times. This would be pretty slow. I guess the alternative would be to just estimate the change in the insertion (but I’m not keen on using an estimate).

Hi @PGWelch,

If you would like to calculate the new route end time with the newAct inserted, then I think the future waiting time state updater should be sufficient, so you would not need to loop over all the activities in the route in the constraint.

Btw, if you have multiple vehicles with different start times/locations, etc., you might want to use a vehicle-dependent version of the state updater.

Best regards,