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.