Mischa Stone
Mischa Stone

Reputation: 21

Execution of rules with a score of 0

I have a employee scheduling application that runs with Timefold. The user can exclude rules dynamically. We handle that with ConstraintWeightOverrides.

As far as I can see, also the rules with a penalty value of 0 are executed (but not penalized). So, if I disable all rules except of one rule, the speed is not higher than when I select all rules. Is that correct or does Timefold somehow know in the ConstraintProvider which rule should not get executed, because it has anyway a score of zero?

If the ConstraintProvider does not know that, is there a recommended way to exclude such a rule?

My approach would be to do something like this (here in an example of a holiday constraint):

public Constraint holiday(ConstraintFactory constraintFactory) {
    return constraintFactory.forEach(Shift.class)
        .filter((shift)-> shift.getRoster().getConstraintWeightOverrides().getConstraintWeight(ConstraintNames.HOLIDAY) == HardMediumSoftLongScore.of(0, 0, 0))
        .join(
            Absence.class,
            Joiners.equal(Shift::getEmployee, Absence::getEmployee),
            Joiners.greaterThanOrEqual(Shift::getDate, Absence::getStartDate),
            Joiners.lessThanOrEqual(Shift::getDate, Absence::getEndDate)
        )
        .penalize(HardMediumSoftLongScore.ONE_HARD)
        .asConstraint(ConstraintNames.HOLIDAY);
}

With the filtering in the first line, we at least do not all the other calculations.

Upvotes: 1

Views: 57

Answers (1)

Tom Cools
Tom Cools

Reputation: 1178

"In Constraint Streams, if you set the constraint weight to zero, the constraint will be disabled and have no performance impact at all."

Source: https://docs.timefold.ai/timefold-solver/latest/constraints-and-score/performance#pointlessConstraints

They should not be executed if the weight is 0. I also sanity-checked that statement by trying it locally and the constraint is not executed while solving.

Upvotes: 2

Related Questions