Search code examples
optaplannerscoring

OptaPlanner: Squaring the match weight score - when and why?


I have noticed in some of the OP examples that the match weight is squared, for example here: https://github.com/kiegroup/optaplanner/blob/b1ad3d90773be5194979246c4c19b4a141efdf85/optaplanner-examples/src/main/java/org/optaplanner/examples/taskassigning/score/TaskAssigningConstraintProvider.java#L55

private Constraint minimizeMakespan(ConstraintFactory constraintFactory) {
        return constraintFactory.forEach(Employee.class)
                .penalize(BendableScore.ofSoft(BENDABLE_SCORE_HARD_LEVELS_SIZE, BENDABLE_SCORE_SOFT_LEVELS_SIZE, 1, 1),
                        employee -> employee.getEndTime() * employee.getEndTime())
                .asConstraint("Minimize makespan, latest ending employee first");
    }

I understand the reason for having a score function is to minimise / prevent score traps, but am wondering when you would consider squaring the score? Are there any rules of thumb when deciding this?

Thanks in advance.


Solution

  • The purpose of that is to tell the solver that 1 violation of the constraint is much more acceptable than 2, and 2 is much more acceptable than 3 violations, 4, 5... It is a penalty that gets worse the further you get away from the target value - so as the difference from the target value grows, the solver has much less incentive to pick that solution.

    You may also want to see our discussion of fairness, which is tangentially related to this.