hi @grantm009 and @Bhoumik_Shah,
it seems to me that the implemented constraint lacks handling of the case that newAct instanceof DeliverShipment, as I’ve asked earlier.
please find at the end of the post an example where the solution does not fulfill the constraint (and also is not the optimal solution).
an interesting thing is that, in the example, if x is changed from 20 to 10 or 30, the solution will fulfill the constraint, but it is still not the optimal solution.
Best regards,
He
double x = 20;
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type")
.addCapacityDimension(0, 2)
.build();
VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1")
.setType(type)
.setReturnToDepot(false)
.setStartLocation(Location.newInstance(0, 0))
.build();
Shipment s1 = Shipment.Builder.newInstance("s1")
.setPickupLocation(Location.newInstance(0, 0))
.setDeliveryLocation(Location.newInstance(x, 0))
.addSizeDimension(0, 1)
.build();
Shipment s2 = Shipment.Builder.newInstance("s2")
.setPickupLocation(Location.newInstance(0, 0))
.setDeliveryLocation(Location.newInstance(x, 0))
.addSizeDimension(0, 1)
.build();
Shipment s3 = Shipment.Builder.newInstance("s3")
.setPickupLocation(Location.newInstance(0, 0))
.setDeliveryLocation(Location.newInstance(0, 100))
.addSizeDimension(0, 1)
.build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance()
.addJob(s1)
.addJob(s2)
.addJob(s3)
.addVehicle(v1)
.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE)
.build();
Jsprit.Builder algorithmBuilder = Jsprit.Builder.newInstance(vrp);
algorithmBuilder.addCoreStateAndConstraintStuff(true);
StateManager stateManager = new StateManager(vrp);
ConstraintManager constraintManager = new ConstraintManager(vrp, stateManager);
constraintManager.addConstraint(
new HardActivityConstraint() {
@Override
public ConstraintsStatus fulfilled(
JobInsertionContext iFacts,
TourActivity prevAct,
TourActivity newAct,
TourActivity nextAct,
double prevActDepTime
) {
if ((newAct instanceof PickupShipment) &&
(!(nextAct instanceof End) && !(nextAct instanceof PickupShipment))) {
if (iFacts.getNewVehicle().getStartLocation().getCoordinate().equals(
newAct.getLocation().getCoordinate()))
return ConstraintsStatus.NOT_FULFILLED_BREAK;
}
return ConstraintsStatus.FULFILLED;
}
},
ConstraintManager.Priority.HIGH);
algorithmBuilder.setStateAndConstraintManager(stateManager, constraintManager);
VehicleRoutingAlgorithm algorithm = algorithmBuilder.buildAlgorithm();
VehicleRoutingProblemSolution s = Solutions.bestOf(algorithm.searchSolutions());
SolutionPrinter.print(vrp, s, SolutionPrinter.Print.VERBOSE);