Time/Distance matrix calculation and How to take traffic into consideration?


I have checked other threads, and couldn’t find a direct answer to this question, in the time/distance matrix calculated, can we:

  1. Provide a Time/Distance Matrix calculated by a map provider such as Google Maps or Here Maps ?
  2. Incorporate traffic information (mainly reduced speed limits) with the Time/Distance matrix ?

An approach that occurs on mind is to create 3 Time/Distance matrices for all locations, each representing a period of the day, say 08:00 to 12:00 , 12:00 to 16:00, 16:00 to 20:00 for example, then calculate new Distance/Travel Time for each matrix , and then rely on different matrices depending on Start time, arrival time and departure time.

thank you.


1 Like

regarding time-dependent travel time/distance matrix, perhaps you can refer to the following two threads:




Thanks for the reply, the links in the first article are not working, how can I access the class documentation and functions ?

thank you,

Hi @keymo13,

It is the same VehicleRoutingTransportCosts class Stefan mentioned in the second article.

So, for your purpose, firstly, you need to prepare a time/distance matrix for each time period: e.g., morningMatrix, afternoonMatrix, eveningMatrix, etc. You can use graphhopper or google maps or any similar service to calculate the matrices. If you would like to use graphhopper for this purpose, there are two blog posts on how to integrate traffic data into the process:

After you have got the time-dependent matrices, your problem would be to call corresponding cost matrix in the VehicleRoutingTransportCosts class.

Something like:

vrpBuilder.setRoutingCost(new TimeDependentCosts(vrpBuilder.getLocations(), morningMatrix, afternoonMatrix, eveningMatrix, ...));

Then in the TimeDependentCosts class, in

getTransportCost(Location from, Location to, double time, Driver driver, Vehicle vehicle) {}


getTransportTime(Location from, Location to, double time, Driver driver, Vehicle vehicle) {}

you have something like:

if (time is morning) {
    double travelTime = morningMatrix[from.getIndex()][to.getIndex()][1];
    double travelDistance = morningMatrix[from.getIndex()][to.getIndex()][0];
    VehicleTypeImpl.VehicleCostParams costParams = vehicle.getType().getVehicleCostParams();
    double cost = costParams.perDistanceUnit * travelDistance + costParams.perTimeUnit * travelTime;

Hopefully this helps.

Best regards,