I had a similar issue, where I needed to allow skipping of certain customers and minimize the number of vehicles used. I adapted the standard Optaplanner (7.12.0) VRP example by using a "Ghost" vehicle, like so:
@XStreamAlias("VrpGhostVehicle")
public class GhostVehicle extends Vehicle {
@Override
public Depot getDepot() {
return null;
}
@Override
public Long getId() {
return Long.valueOf(0);
}
@Override
public boolean isGhost() {
return true;
}
@Override
public int getCapacity() {
return Integer.MAX_VALUE;
}
}
To model customers that cannot be skipped, I added a "unskippable" boolean field to Customer.java. Then, in your vehicleRoutingScoreRules.drl, I adjusted and added constraints like:
rule "vehicleCapacity"
when
$vehicle : Vehicle(ghost == false, $capacity : capacity)
accumulate(
Customer(
vehicle == $vehicle,
$demand : demand);
$demandTotal : sum($demand);
$demandTotal > $capacity
)
then
scoreHolder.addHardConstraintMatch(kcontext, $capacity - $demandTotal);
end
// ############################################################################
// Soft distance constraints
// ############################################################################
rule "distanceToPreviousStandstill"
when
$vehicle : Vehicle(ghost == false)
$customer : Customer(previousStandstill != null, vehicle == $vehicle, $distanceFromPreviousStandstill : distanceFromPreviousStandstill)
then
scoreHolder.addSoftConstraintMatch(kcontext, - $distanceFromPreviousStandstill);
end
rule "distanceFromLastCustomerToDepot"
when
$vehicle : Vehicle(ghost == false)
$customer : Customer(previousStandstill != null, vehicle == $vehicle)
not Customer(previousStandstill == $customer)
then
Vehicle vehicle = $customer.getVehicle();
scoreHolder.addSoftConstraintMatch(kcontext, - $customer.getDistanceTo(vehicle));
end
// ############################################################################
// Time window constraints
// ############################################################################
rule "arrivalAfterDueTime"
when
$vehicle : Vehicle(ghost == false)
TimeWindowedCustomer(dueTime < arrivalTime, vehicle == $vehicle, $dueTime : dueTime, $arrivalTime : arrivalTime)
then
scoreHolder.addSoftConstraintMatch(kcontext, -1000 * Math.abs($dueTime - $arrivalTime.longValue()));
end
rule "arrivalBeforeReadyTime"
when
$vehicle : Vehicle(ghost == false)
TimeWindowedCustomer(readyTime > arrivalTime, vehicle == $vehicle, $readyTime : readyTime, $arrivalTime : arrivalTime)
then
scoreHolder.addSoftConstraintMatch(kcontext, -1000 * Math.abs($readyTime - $arrivalTime.longValue()));
end
// ############################################################################
// Constraints pertaining to station skipping and vehicle usage
// ############################################################################
rule "skippedCustomer"
when
$vehicle : Vehicle(ghost == true)
$customer : Customer(vehicle == $vehicle, $unskippable : unskippable, $demand : demand)
then
if ($unskippable) {
scoreHolder.addHardConstraintMatch(kcontext, -10000 * $demand);
} else {
scoreHolder.addSoftConstraintMatch(kcontext, -10000 * $demand);
}
end
rule "usedTooManyVehicles"
when
$vehicle : Vehicle(nextCustomer != null, ghost == false)
then
scoreHolder.addSoftConstraintMatch(kcontext, -500000);
end
The bottom three rules are constraints I added to the example and I added further checks for ghost==false to the rules that should only apply to real vehicles. Notice that for unskippable customers we set a hard constraint whereas for skippable ones we set a soft constraint. The relative weights for skipping a customer or using a vehicle are of course application specific.