How index management works in jsprit 2.0
masterIn jsprit 2.0, indices are no longer stored on Job and Vehicle objects. Instead, each VehicleRoutingProblem (VRP) manages its own indices internally.
Key changes:
- Object Reuse: You can safely reuse the same
JoborVehicleobjects across multipleVehicleRoutingProbleminstances without index conflicts. - Automatic Assignment: Indices are assigned automatically (0 to n-1) when
build()is called. - Retrieving Indices: Use the VRP instance to retrieve the index for a specific object.
Note: Job.getIndex() and Vehicle.getIndex() are deprecated. They still work but return the index from the last VRP they were added to. Use the VRP methods instead.
// Define jobs once
Service job1 = Service.Builder.newInstance("job1").setLocation(loc1).build();
Service job2 = Service.Builder.newInstance("job2").setLocation(loc2).build();
// Create different problem variations reusing the same jobs
VehicleRoutingProblem vrp1 = VehicleRoutingProblem.Builder.newInstance()
.addJob(job1).addJob(job2)
.addVehicle(smallFleet)
.build();
VehicleRoutingProblem vrp2 = VehicleRoutingProblem.Builder.newInstance()
.addJob(job1).addJob(job2)
.addVehicle(largeFleet) // Different fleet
.build();
// Solve both independently - no index conflicts
// Retrieving indices:
int jobIndex = vrp1.getJobIndex(job1);
int vehicleIndex = vrp1.getVehicleIndex(vehicle);