Correct method to get route distances

Hiyas, I have my routes calculating but just wanted to see if there is a better way of getting distance between jobs and total route distance.

for (VehicleRoute route : Solutions.bestOf(solutions).getRoutes()) {
            totalTime += route.getEnd().getArrTime();
            prevAct = route.getStart();

            for (TourActivity act : route.getActivities()) {
                if(act == route.getStart()){
                    prevAct = act;
                }else{
                    routeDistance += getDistance(prevAct, act, costMatrix);
                    prevAct = act;
                }
            }
        }

static double getDistance(TourActivity from, TourActivity to, VehicleRoutingTransportCostsMatrix costMatrix) {

            return costMatrix.getDistance(from.getLocation().getId(), to.getLocation().getId());
    
    }

Iā€™m happy with the route time, but am I correct in using the costMatrix to produce distances?

1 Like

Hi, you can use the SolutionAnalyser class.

import com.graphhopper.jsprit.core.analysis.SolutionAnalyser


val bestSolution = Solutions.bestOf(solutions)

val analyser = SolutionAnalyser(vrp, bestSolution, costsMatrix)
                
val serviceTime = analyser.serviceTime
val transportTime = analyser.transportTime
val distance = analyser.distance

To get the distance between jobs your getDistance method is fine.