GraphHopper.com | Forum | GitHub | Maps | Blog

Usecase - jsprit usage for existing distance&cost matrix


#1

Hello how can we use jsprit if I already have an array of distance&cost matrix? Is there any examples available or this use case is not available in jsprit?

I’ve looked into the GitHub examples and I cant seem to find the equivalent example.


#2

See FastVehicleRoutingTransportCostsMatrix and https://github.com/graphhopper/jsprit/blob/master/jsprit-examples/src/main/java/com/graphhopper/jsprit/examples/RefuseCollectionWithFastMatrixExample.java


#3

I’ve looked into that example as well the Cost matrix which is more close to what I was looking for. How do I map the locations of each service to the distance matrix because I am providing both the distance matrix as well as the location for each Service.


#4

Example:

private static void buildCostMatrix() {
Integer rowIndex = 0;
VehicleRoutingTransportCostsMatrix.Builder costMatrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(false); // For asymmetric matrices

for (ArrayList<Map<String, Integer>> row : jsonOutput.json.matrix) {
  Integer elIndex = 0;
  for (Map<String, Integer> el : row) {

    // Build distance cost matrix
    if (rowIndex != elIndex) {
      costMatrixBuilder.addTransportDistance(rowIndex.toString(), elIndex.toString(), el.get("distance"));
      costMatrixBuilder.addTransportTime(rowIndex.toString(), elIndex.toString(), el.get("duration"));
    }
    elIndex++;
  }
  rowIndex++;
}

VehicleRoutingTransportCosts costMatrix = costMatrixBuilder.build();
VRPBuilder = VehicleRoutingProblem.Builder.newInstance().setRoutingCost(costMatrix);
}

private static void buildShipments() {

for (Shipment shipment : jsonOutput.json.shipments) {

  // Build service/shipment
  VRPBuilder.addJob(Service.Builder.newInstance(shipment.consignmentNumber)
      .addSizeDimension(0, shipment.length * shipment.width * shipment.height)
      .addTimeWindow(TimeWindow.newInstance(shipment.startDateTime, shipment.endDateTime))
      .setLocation(Location.newInstance(shipment.addressLocation.latitude, shipment.addressLocation.longitude)).build());
  }
}

How can I refer the location of shipment to the cost matrix table?


#5

The JSprit Location object has an index, I’m using that.

		Builder builder = FastVehicleRoutingTransportCostsMatrix.Builder.newInstance(allLocations.size(), false);
		for (Location from : allLocations) {
			for (Location to : allLocations) {
				if (from != to && !from.equals(to)) {
					DistanceResult distanceResult = router.distance(from, to, null);
					builder.addTransportTimeAndDistance(from.getIndex(), to.getIndex(), distanceResult.getTime(), distanceResult.getDistance());
				} else {
					builder.addTransportTimeAndDistance(from.getIndex(), to.getIndex(), 0, 0);
				}
				distances++;
			}
		}