Good afternoon, I’m trying to get an optimized route matrix, but when I run the program, I encounter the following error:

Error: Exception in thread “main” java.lang.IllegalStateException: time value for relation from [x=35.162316961336394][y=-106.51387457531828] to [x=35.11498075724412][y=-106.67752402116778] does not exist

Code:

private static void optimizeRoute(GraphHopper hopper) {
VehicleTypeImpl vehicleType = VehicleTypeImpl.Builder.newInstance(“vehicle_id”)
.build();

    VehicleImpl serviceStart = VehicleImpl.Builder.newInstance("0")
            .setStartLocation(loc(Coordinate.newInstance(35.162316961336394, -106.51387457531828)))
            .setType(vehicleType)
            .setReturnToDepot(false)
            .build();

    Service service1 = Service.Builder.newInstance("1")
            .setLocation(Location.newInstance(35.11498075724412, -106.67752402116778))
            .build();

    Service service2 = Service.Builder.newInstance("2")
            .setLocation(Location.newInstance(35.11526306515924, -106.55295605374026))
            .build();

    Service service3 = Service.Builder.newInstance("3")
            .setLocation(Location.newInstance(35.114527475312386, -106.56020801171813))
            .build();


    VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);

calculateAndAddCosts(matrixBuilder, hopper, serviceStart.getStartLocation(), service1.getLocation(), “0”, “1”);
calculateAndAddCosts(matrixBuilder, hopper, serviceStart.getStartLocation(), service2.getLocation(), “0”, “2”);
calculateAndAddCosts(matrixBuilder, hopper, serviceStart.getStartLocation(), service3.getLocation(), “0”, “3”);

    calculateAndAddCosts(matrixBuilder, hopper, service1.getLocation(), service2.getLocation(), "1", "2");
    calculateAndAddCosts(matrixBuilder, hopper, service1.getLocation(), service3.getLocation(), "1", "3");

    calculateAndAddCosts(matrixBuilder, hopper, service2.getLocation(), service3.getLocation(), "2", "3");

    calculateAndAddCosts(matrixBuilder, hopper, service1.getLocation(), serviceStart.getStartLocation(), "1", "0");
    calculateAndAddCosts(matrixBuilder, hopper, service2.getLocation(), serviceStart.getStartLocation(), "2", "0");
    calculateAndAddCosts(matrixBuilder, hopper, service3.getLocation(), serviceStart.getStartLocation(), "3", "0");

    VehicleRoutingTransportCosts matrix = matrixBuilder.build();

    VehicleRoutingProblem.Builder problem = VehicleRoutingProblem.Builder.newInstance();

           problem.setRoutingCost(matrix)
                   .addVehicle(serviceStart)
            .addJob(service1)
            .addJob(service2)
            .addJob(service3);

    VehicleRoutingProblem problem1 = problem.build();

    VehicleRoutingAlgorithm algorithm = new SchrimpfFactory().createAlgorithm(problem1);

    Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();

    VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);

    SolutionPrinter.print(problem1, bestSolution, SolutionPrinter.Print.VERBOSE);
    System.out.println("Optimized points:");
    bestSolution.getRoutes().forEach(route -> route.getActivities().forEach(activity -> {
        System.out.println("Lat: " + activity.getLocation().getCoordinate().getX()
                + " Lon: " + activity.getLocation().getCoordinate().getY());
    }));
    hopper.close();
}

private static void calculateAndAddCosts(VehicleRoutingTransportCostsMatrix.Builder matrixBuilder,
GraphHopper hopper, Location from, Location to, String fromId, String toId) {
double fromLat = from.getCoordinate().getX();
double fromLon = from.getCoordinate().getY();
double toLat = to.getCoordinate().getX();
double toLon = to.getCoordinate().getY();

    GHRequest request = new GHRequest(fromLat, fromLon, toLat, toLon).setProfile("car");
    GHResponse response = hopper.route(request);

    if (response.hasErrors()) {
        System.err.println("Ошибка при расчете маршрута: " + response.getErrors());
    } else {
        double distance = response.getBest().getDistance(); // Расстояние в метрах
        double time = response.getBest().getTime() / 1000.0; // Время в секундах
        matrixBuilder.addTransportDistance(fromId, toId, distance);
        matrixBuilder.addTransportTime(fromId, toId, time);
    }
}

I’m not entirely sure if this is the problem, but be aware that typically the ‘lat’ value corresponds to the Y(!) coordinate and the ‘lon’ value to the X(!) coordinate.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.