I’m wondering how to iterate through the jobs list to build my distance matrix.
// Add all the jobs
for (int i = 0; i < points.size(); i++) {
int offset = i + 1;
Service service = Service.Builder.newInstance(String.valueOf(i)).addSizeDimension(WEIGHT_INDEX, 1).setLocation(Location.newInstance(points.get(i), points.get(offset))).build();
vrpBuilder.addJob(service);
i++;
}
//define a matrix-builder building a symmetric matrix
VehicleRoutingTransportCostsMatrix.Builder costMatrixBuilder =
VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
I then want to iterate through each of the jobs to build the costmatrix. eg.
For each job{
// This is the bit I'm unsure of...
originlat = ;
originlong = ;
tolat = ;
tolong = ;
// Use graphhopper to return distances between the two points
distance = pointToPointDistance(double originlat,double originlong,double tolat,double tolong);
costMatrixBuilder.addTransportDistance(job1, job2, distance);
}
Then the next iteration would produce;
costMatrixBuilder.addTransportDistance(job1, job3, distance);
Eventually running through all jobs
costMatrixBuilder.addTransportDistance(job300, job299, distance);
final int WEIGHT_INDEX = 0;
/*
* get a vehicle type-builder and build a type with the typeId "vehicleType" and a capacity of 2
*/
VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType").addCapacityDimension(0, 2);
vehicleTypeBuilder.setFixedCost(100);
VehicleType vehicleType = vehicleTypeBuilder.build();
/*
* get a vehicle-builder and build a vehicle located at the first DP OR depot with type "vehicleType"
*/
Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
vehicleBuilder.setStartLocation(Location.newInstance(points.get(0), points.get(1)));
vehicleBuilder.setType(vehicleType);
vehicleBuilder.setReturnToDepot(false);
VehicleImpl vehicle = vehicleBuilder.build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
vrpBuilder.addVehicle(vehicle);
// Add all the jobs
/*
* build services at the required locations, each with a capacity-demand of 1.
*/
for (int i = 0; i < points.size(); i++) {
int offset = i + 1;
Service service = Service.Builder.newInstance(String.valueOf(i)).addSizeDimension(WEIGHT_INDEX, 1).setLocation(Location.newInstance(points.get(i), points.get(offset))).build();
vrpBuilder.addJob(service);
i++;
}
//define a matrix-builder building a symmetric matrix
VehicleRoutingTransportCostsMatrix.Builder costMatrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
System.out.println(vrpBuilder.getAddedJobs().size());
for (int i = 1; i <= vrpBuilder.getAddedJobs().size(); i++){
for (int j = 1; j <= vrpBuilder.getAddedJobs().size(); j++) {
double distance = pointToPointDistance(points.get(i*2-2), points.get(i*2-1), points.get(j*2-2), points.get(j*2-1));
if (distance < 150){
costMatrixBuilder.addTransportDistance(String.valueOf(i), String.valueOf(j), distance);
}
}
}
}
static public double pointToPointDistance(double fLat, double fLong, double tLat, double tLong){
GHRequest request = new GHRequest().setWeighting("fastest").setVehicle("car");
request.addPoint(new GHPoint(fLat, fLong));
request.addPoint(new GHPoint(tLat, tLong));
GHResponse route = graphHopper.route(request);
path = route.getBest();
return path.getDistance();
}