【发布时间】:2020-04-21 19:06:02
【问题描述】:
我正在使用 optaplanner spring boot starter 来解决员工排班问题。我有 2 个班级,Employee 和一个计划实体 Shift。目前,我正在使用如下约束提供程序根据员工的技能水平为他们分配班次。
public class ConstraintProvider implements
org.optaplanner.core.api.score.stream.ConstraintProvider {
@Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
return new Constraint[]{
requiredSkillLevelOfEmployeesForShifts(constraintFactory)
};
}
private Constraint requiredSkillLevelOfEmployeesForShifts(ConstraintFactory constraintFactory) {
return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, sum(Shift::getRequiredSkillLevel))
.filter((employee, requiredSkillLevel) -> requiredSkillLevel > employee.getSkillLevel())
.penalize("requiredSkillLevelForShifts",
HardSoftScore.ONE_HARD,
(employee, requiredSkillLevel) -> requiredSkillLevel - employee.getSkillLevel());
}
}
我通过 JSON 将轮班和员工列表传递给控制器,然后控制器解决并返回最佳解决方案。
@RestController
@RequestMapping("/api")
public class RostersController {
@Autowired
private SolverManager<Roster, UUID> solverManager;
@PostMapping("/solve")
public Roster solve(@RequestBody Roster problem) {
UUID problemId = UUID.randomUUID();
// Submit the problem to start solving
SolverJob<Roster, UUID> solverJob = solverManager.solve(problemId, problem);
Roster solution;
try {
// Wait until the solving ends
solution = solverJob.getFinalBestSolution();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException("Solving failed.", e);
}
return solution;
}
}
我想添加另一个约束条件,我限制员工一周内可以轮班的数量,并且员工每天只能轮班。是否可以通过添加另一个约束来做到这一点,或者我是否需要某种 config .xml 文件以及 drools .drl 文件来添加更具体的约束?
【问题讨论】:
标签: java spring-boot constraints solver optaplanner