Search code examples
droolsoptaplanner

How to add custom HardConstraint in VRP for optaplanner?


Thanks optaplanner team for such a fantastic Project.

I need to add a constraint in Vehicle called capacityCap (a customer is assigned to the vehicle only if the "demand <= capacityCap") I have added a variable capacityCap in Vehicle.java.

protected int capacityCap;
public int getCapacityCap() {
    return capacityCap;
}

public void setCapacityCap(int capacityCap) {
    this.capacityCap = capacityCap;
}

And added drool for this in vehicleRoutingScoreRules.drl

// Hard constraints
rule "vehicleCapacityCap"
when
    $vehicle : Vehicle($capacityCap : capacityCap)
    $customer : Customer(demand > $capacityCap);
then
    scoreHolder.addHardConstraintMatch(kcontext, -1);
end

But after running it leaves all the customer unassigned.Am I doing some wrong in drool ?

UseCase for This: If I want to assign order to a biker only if it weights less than 5Kg.

Please ask if more information needed. Thanks


Solution

  • Try this

    // Hard constraints
    rule "vehicleCapacityCap"
    when
       $vehicle : Vehicle($capacityCap : capacityCap)
       $customer : Customer(vehicle == $vehicle, demand > $capacityCap);
    then
       scoreHolder.addHardConstraintMatch(kcontext, -1);
    end