Assert the solution

This commit is contained in:
Geoffrey De Smet 2023-11-27 08:45:03 +01:00
parent 2e60ba5007
commit 367e9e6c2a
2 changed files with 10 additions and 4 deletions

View File

@ -21,7 +21,7 @@ class ShiftScheduleConstraintProviderUnitTest {
constraintVerifier.verifyThat(ShiftScheduleConstraintProvider::atMostOneShiftPerDay)
.given(ann, new Shift(MONDAY.atTime(6, 0), MONDAY.atTime(14, 0), null, ann), new Shift(MONDAY.atTime(14, 0), MONDAY.atTime(22, 0), null, ann))
// Penalizes by 2 because both {shiftA, shiftB} and {shiftB, shiftA} match.
// To avoid that, use forEachUniquePair() in the constraint instead.
// To avoid that, use forEachUniquePair() in the constraint instead of forEach().join() in the implementation.
.penalizesBy(2);
}

View File

@ -1,5 +1,7 @@
package com.baeldung.timefoldsolver;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
@ -23,13 +25,17 @@ public class ShiftScheduleSolverUnitTest {
SolverFactory<ShiftSchedule> solverFactory = SolverFactory.create(new SolverConfig().withSolutionClass(ShiftSchedule.class)
.withEntityClasses(Shift.class)
.withConstraintProviderClass(ShiftScheduleConstraintProvider.class)
// The solver runs only for 2 seconds on this tiny dataset.
// It's recommended to run for at least 5 minutes ("5m") on large datasets.
.withTerminationSpentLimit(Duration.ofSeconds(2)));
// For this dataset, we know the optimal score in advance (which is normally not the case).
// So we can use .withBestScoreLimit() instead of .withTerminationSpentLimit().
.withTerminationConfig(new TerminationConfig().withBestScoreLimit("0hard/0soft")));
Solver<ShiftSchedule> solver = solverFactory.buildSolver();
ShiftSchedule problem = loadProblem();
ShiftSchedule solution = solver.solve(problem);
assertThat(solution.getShifts().size()).isNotZero();
for (Shift shift : solution.getShifts()) {
assertThat(shift.getEmployee()).isNotNull();
}
printSolution(solution);
}