format with baeldung style
This commit is contained in:
parent
3df51484f2
commit
07a6999e56
|
@ -16,7 +16,8 @@ public class Shift {
|
|||
private Employee employee;
|
||||
|
||||
// A no-arg constructor is required for @PlanningEntity annotated classes
|
||||
public Shift() {}
|
||||
public Shift() {
|
||||
}
|
||||
|
||||
public Shift(LocalDateTime start, LocalDateTime end, String requiredSkill) {
|
||||
this(start, end, requiredSkill, null);
|
||||
|
|
|
@ -5,24 +5,24 @@ import java.time.LocalDate;
|
|||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ai.timefold.solver.core.api.solver.Solver;
|
||||
import ai.timefold.solver.core.api.solver.SolverFactory;
|
||||
import ai.timefold.solver.core.config.solver.SolverConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ShiftScheduleApp {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ShiftScheduleApp.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
SolverFactory<ShiftSchedule> solverFactory = SolverFactory.create(new SolverConfig()
|
||||
.withSolutionClass(ShiftSchedule.class)
|
||||
.withEntityClasses(Shift.class)
|
||||
.withConstraintProviderClass(ShiftScheduleConstraintProvider.class)
|
||||
// The solver runs only for 5 seconds on this small dataset.
|
||||
// It's recommended to run for at least 5 minutes ("5m") otherwise.
|
||||
.withTerminationSpentLimit(Duration.ofSeconds(5)));
|
||||
SolverFactory<ShiftSchedule> solverFactory = SolverFactory.create(new SolverConfig().withSolutionClass(ShiftSchedule.class)
|
||||
.withEntityClasses(Shift.class)
|
||||
.withConstraintProviderClass(ShiftScheduleConstraintProvider.class)
|
||||
// The solver runs only for 5 seconds on this small dataset.
|
||||
// It's recommended to run for at least 5 minutes ("5m") otherwise.
|
||||
.withTerminationSpentLimit(Duration.ofSeconds(5)));
|
||||
Solver<ShiftSchedule> solver = solverFactory.buildSolver();
|
||||
|
||||
ShiftSchedule problem = loadProblem();
|
||||
|
@ -33,25 +33,21 @@ public class ShiftScheduleApp {
|
|||
private static ShiftSchedule loadProblem() {
|
||||
LocalDate monday = LocalDate.of(2030, 4, 1);
|
||||
LocalDate tuesday = LocalDate.of(2030, 4, 2);
|
||||
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")
|
||||
));
|
||||
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")));
|
||||
}
|
||||
|
||||
private static void printSolution(ShiftSchedule solution) {
|
||||
logger.info("Shift assignments");
|
||||
for (Shift shift : solution.getShifts()) {
|
||||
logger.info(" " + shift.getStart().toLocalDate()
|
||||
+ " " + shift.getStart().toLocalTime() + " - " + shift.getEnd().toLocalTime()
|
||||
+ ": " + shift.getEmployee().getName());
|
||||
logger.info(" " + shift.getStart()
|
||||
.toLocalDate() + " " + shift.getStart()
|
||||
.toLocalTime() + " - " + shift.getEnd()
|
||||
.toLocalTime() + ": " + shift.getEmployee()
|
||||
.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,37 +1,35 @@
|
|||
package com.baeldung.timefoldsolver;
|
||||
|
||||
import static ai.timefold.solver.core.api.score.stream.Joiners.equal;
|
||||
|
||||
import ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore;
|
||||
import ai.timefold.solver.core.api.score.stream.Constraint;
|
||||
import ai.timefold.solver.core.api.score.stream.ConstraintFactory;
|
||||
import ai.timefold.solver.core.api.score.stream.ConstraintProvider;
|
||||
|
||||
import static ai.timefold.solver.core.api.score.stream.Joiners.equal;
|
||||
|
||||
public class ShiftScheduleConstraintProvider implements ConstraintProvider {
|
||||
|
||||
@Override
|
||||
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
|
||||
return new Constraint[] {
|
||||
atMostOneShiftPerDay(constraintFactory),
|
||||
requiredSkill(constraintFactory)
|
||||
};
|
||||
return new Constraint[] { atMostOneShiftPerDay(constraintFactory), requiredSkill(constraintFactory) };
|
||||
}
|
||||
|
||||
public Constraint atMostOneShiftPerDay(ConstraintFactory constraintFactory) {
|
||||
return constraintFactory.forEach(Shift.class)
|
||||
.join(Shift.class,
|
||||
equal(shift -> shift.getStart().toLocalDate()),
|
||||
equal(Shift::getEmployee))
|
||||
.filter((shift1, shift2) -> shift1 != shift2)
|
||||
.penalize(HardSoftScore.ONE_HARD)
|
||||
.asConstraint("At most one shift per day");
|
||||
.join(Shift.class, equal(shift -> shift.getStart()
|
||||
.toLocalDate()), equal(Shift::getEmployee))
|
||||
.filter((shift1, shift2) -> shift1 != shift2)
|
||||
.penalize(HardSoftScore.ONE_HARD)
|
||||
.asConstraint("At most one shift per day");
|
||||
}
|
||||
|
||||
public Constraint requiredSkill(ConstraintFactory constraintFactory) {
|
||||
return constraintFactory.forEach(Shift.class)
|
||||
.filter(shift -> !shift.getEmployee().getSkills().contains(shift.getRequiredSkill()))
|
||||
.penalize(HardSoftScore.ONE_HARD)
|
||||
.asConstraint("Required skill");
|
||||
.filter(shift -> !shift.getEmployee()
|
||||
.getSkills()
|
||||
.contains(shift.getRequiredSkill()))
|
||||
.penalize(HardSoftScore.ONE_HARD)
|
||||
.asConstraint("Required skill");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,49 +3,40 @@ package com.baeldung.timefoldsolver;
|
|||
import java.time.LocalDate;
|
||||
import java.util.Set;
|
||||
|
||||
import ai.timefold.solver.test.api.score.stream.ConstraintVerifier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import ai.timefold.solver.test.api.score.stream.ConstraintVerifier;
|
||||
|
||||
class ShiftScheduleConstraintProviderTest {
|
||||
|
||||
private static final LocalDate MONDAY = LocalDate.of(2030, 4, 1);
|
||||
private static final LocalDate TUESDAY = LocalDate.of(2030, 4, 2);
|
||||
|
||||
ConstraintVerifier<ShiftScheduleConstraintProvider, ShiftSchedule> constraintVerifier = ConstraintVerifier.build(
|
||||
new ShiftScheduleConstraintProvider(), ShiftSchedule.class, Shift.class);
|
||||
ConstraintVerifier<ShiftScheduleConstraintProvider, ShiftSchedule> constraintVerifier = ConstraintVerifier.build(new ShiftScheduleConstraintProvider(),
|
||||
ShiftSchedule.class, Shift.class);
|
||||
|
||||
@Test
|
||||
void atMostOneShiftPerDay() {
|
||||
Employee ann = new Employee("Ann", null);
|
||||
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.
|
||||
.penalizesBy(2);
|
||||
.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.
|
||||
.penalizesBy(2);
|
||||
constraintVerifier.verifyThat(ShiftScheduleConstraintProvider::atMostOneShiftPerDay)
|
||||
.given(
|
||||
ann,
|
||||
new Shift(MONDAY.atTime(6, 0), MONDAY.atTime(14, 0), null, ann),
|
||||
new Shift(TUESDAY.atTime(14, 0), TUESDAY.atTime(22, 0), null, ann))
|
||||
.penalizesBy(0);
|
||||
.given(ann, new Shift(MONDAY.atTime(6, 0), MONDAY.atTime(14, 0), null, ann), new Shift(TUESDAY.atTime(14, 0), TUESDAY.atTime(22, 0), null, ann))
|
||||
.penalizesBy(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void requiredSkill() {
|
||||
Employee ann = new Employee("Ann", Set.of("Waiter"));
|
||||
constraintVerifier.verifyThat(ShiftScheduleConstraintProvider::requiredSkill)
|
||||
.given(
|
||||
ann,
|
||||
new Shift(MONDAY.atTime(6, 0), MONDAY.atTime(14, 0), "Cook", ann))
|
||||
.penalizesBy(1);
|
||||
.given(ann, new Shift(MONDAY.atTime(6, 0), MONDAY.atTime(14, 0), "Cook", ann))
|
||||
.penalizesBy(1);
|
||||
constraintVerifier.verifyThat(ShiftScheduleConstraintProvider::requiredSkill)
|
||||
.given(
|
||||
ann,
|
||||
new Shift(MONDAY.atTime(6, 0), MONDAY.atTime(14, 0), "Waiter", ann))
|
||||
.penalizesBy(0);
|
||||
.given(ann, new Shift(MONDAY.atTime(6, 0), MONDAY.atTime(14, 0), "Waiter", ann))
|
||||
.penalizesBy(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue