66 lines
3.0 KiB
Java
Raw Normal View History

2023-11-21 14:09:04 +01:00
package com.baeldung.timefoldsolver;
2023-11-27 08:45:03 +01:00
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2023-11-21 14:09:04 +01:00
import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
2023-11-25 21:09:32 +01:00
import org.junit.jupiter.api.Test;
2023-11-24 21:40:29 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore;
2023-11-21 14:09:04 +01:00
import ai.timefold.solver.core.api.solver.Solver;
import ai.timefold.solver.core.api.solver.SolverFactory;
import ai.timefold.solver.core.config.solver.SolverConfig;
2023-11-25 21:09:32 +01:00
import ai.timefold.solver.core.config.solver.termination.TerminationConfig;
2023-11-21 14:09:04 +01:00
2023-11-25 21:09:32 +01:00
public class ShiftScheduleSolverUnitTest {
2023-11-21 14:09:04 +01:00
2023-11-25 21:09:32 +01:00
private static final Logger logger = LoggerFactory.getLogger(ShiftScheduleSolverUnitTest.class);
2023-11-24 21:01:28 +01:00
2023-11-25 21:09:32 +01:00
@Test
2023-12-02 20:20:40 +01:00
public void given3Employees5Shifts_whenSolve_thenScoreIsOptimalAndAllShiftsAssigned() {
2023-11-24 21:40:29 +01:00
SolverFactory<ShiftSchedule> solverFactory = SolverFactory.create(new SolverConfig().withSolutionClass(ShiftSchedule.class)
.withEntityClasses(Shift.class)
.withConstraintProviderClass(ShiftScheduleConstraintProvider.class)
2023-11-27 08:45:03 +01:00
// 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")));
2023-11-21 14:09:04 +01:00
Solver<ShiftSchedule> solver = solverFactory.buildSolver();
ShiftSchedule problem = loadProblem();
ShiftSchedule solution = solver.solve(problem);
assertThat(solution.getScore()).isEqualTo(HardSoftScore.ZERO);
2023-11-27 08:45:03 +01:00
assertThat(solution.getShifts().size()).isNotZero();
for (Shift shift : solution.getShifts()) {
assertThat(shift.getEmployee()).isNotNull();
}
2023-11-21 14:09:04 +01:00
printSolution(solution);
}
2023-11-25 21:09:32 +01:00
private ShiftSchedule loadProblem() {
2023-11-21 14:09:04 +01:00
LocalDate monday = LocalDate.of(2030, 4, 1);
LocalDate tuesday = LocalDate.of(2030, 4, 2);
2023-11-24 21:40:29 +01:00
return new ShiftSchedule(
List.of(new Employee("Ann", Set.of("Bartender")), new Employee("Beth", Set.of("Waiter", "Bartender")), new Employee("Carl", Set.of("Waiter"))),
List.of(new Shift(monday.atTime(6, 0), monday.atTime(14, 0), "Waiter"), new Shift(monday.atTime(9, 0), monday.atTime(17, 0), "Bartender"),
new Shift(monday.atTime(14, 0), monday.atTime(22, 0), "Bartender"), new Shift(tuesday.atTime(6, 0), tuesday.atTime(14, 0), "Waiter"),
new Shift(tuesday.atTime(14, 0), tuesday.atTime(22, 0), "Bartender")));
2023-11-21 14:09:04 +01:00
}
2023-11-25 21:09:32 +01:00
private void printSolution(ShiftSchedule solution) {
2023-11-24 21:01:28 +01:00
logger.info("Shift assignments");
2023-11-21 14:09:04 +01:00
for (Shift shift : solution.getShifts()) {
2023-11-24 21:40:29 +01:00
logger.info(" " + shift.getStart()
.toLocalDate() + " " + shift.getStart()
.toLocalTime() + " - " + shift.getEnd()
.toLocalTime() + ": " + shift.getEmployee()
.getName());
2023-11-21 14:09:04 +01:00
}
}
}