OPTAPLANNER: Add function of planning entities as optimization constraint

OPTAPLANNER: Add function of planning entities as optimization constraint

Problem Description:

I want to add a function of the list of planning entities as a constraint. More specifically, each planning entity has a contribution x, and I want to implement a constraint that adds a HARD score if the sum of all the x contributions is greater than X. Something like…

Constraint ConstraintX(ConstraintFactory constraintFactory) {
    return constraintFactory
            .forEach(planningEntity.class)
            .filter(planningEntity-> planningEntity.get_x())
            .sum().penalize("Sum of xs bigger than X", 
                            HardMediumSoftScore.ONE_HARD,
                            sum > X # what goes here???                                                
                            );
}

Solution – 1

See OptaPlanner documentation on Constraint Collectors.
Specifically ConstraintCollectors.sum(...) does exactly what you need.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject