timefold-solver icon indicating copy to clipboard operation
timefold-solver copied to clipboard

Feat: Support testing of node sharing between constraints

Open rsynek opened this issue 2 years ago • 2 comments

Assume two constraints sharing the same UniConstraintStream:

    @Override
    public Constraint[] defineConstraints(ConstraintFactory factory) {
        UniConstraintStream<VehicleShift> nonVirtualVehicles = factory.forEach(VehicleShift.class)
                .filter(vehicleShift -> !vehicleShift.isVirtual());

        return new Constraint[] {
                // hard
                endLocationMaxArrivalTimeHardLimit(nonVirtualVehicles),
                lastVisitMaxDepartureTimeHardLimit(nonVirtualVehicles)
        };
    }

These constraints cannot be unit-tested using the ConstraintVerifier as the constraintVerifier.verifyThat() expects a method reference with a single parameter, the ConstraintFactory .

rsynek avatar Jul 26 '23 10:07 rsynek

Out of curiosity: Does node sharing like this improve performance or is it solely done to avoid code duplication?

greyhairredbear avatar Mar 05 '25 22:03 greyhairredbear

This improves performance. If your constraint streams share parts, those parts will only be executed once, regardless of how many constraints they're part of.

The Enterprise Edition takes care of this automatically, no need to think about it, or to restructure your code for it.

triceo avatar Mar 06 '25 05:03 triceo