Search code examples
optaplanner

ConstraintConfiguration not used by ConstraintVerifier in testing class?


I am currently working with dynamic weights configured in a ConstraintConfiguration class. But the class doesn't seem to be used while executing my tests that I wrote. It is however used while actually executing the solver.

For example: one of the weights in the configuration class is 16. While testing for a score of 1, it will not be multiplied with the weight and the result will be 1. But while actually solving, it will use it and it will be 16 as expected.

I am guessing that I'm missing something in my testing class. Do you have to tell the ConstraintVerifier or the testing methods in the testing class that there is a ConstraintConfiguration? Or am I missing something else?

Thanks in advance.

My current constraintverifier:

ConstraintVerifier<ExamScheduleConstraintProvider, ExamSchedule> constraintVerifier = ConstraintVerifier.build(
            new ExamScheduleConstraintProvider(), ExamSchedule.class, Exam.class);

Example of test code that won't pass:

constraintVerifier.verifyThat(ExamScheduleConstraintProvider::TestConstraint)
                .given(firstExam, secondExam)
                .penalizesBy(16);

Solution

  • The ConstraintVerifier offers two verification methods:

    1. verifyThat(constraintFunction);
    2. verifyThat();

    verifyThat(constraintFunction) accepts a method reference to an individual constraint and verifies that the constraint, given certain entities and facts or an entire solution, penalizes or rewards by the expected match weight. Important to note, this verifier ignores constraint weight completely.

    verifyThat() does not accept any argument and checks the score impact (including constraint weight coming from ConstraintConfiguration) of all the constraints defined in the ConstraintProvider implementation for the provided entities and facts or an entire solution.