Optaplanner在ConstraintProvider类中为员工允许的最大轮班量添加约束



我正试图在ConstraintProvider类中添加一个限制分配给员工的轮班数量的约束,但我想知道是否有更好的方法来定义它,而不是在我的@PlanningEntity和@PlanningVariable类中都添加一个属性?

这是我的@PlanningEntity类属性:

@PlanningEntity
@Entity
public class Shift {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "shiftId")
private int shiftId;
@Column(name = "startTime")
private LocalTime startTime;
@Column(name = "endTime")
private LocalTime endTime;
@Column(name = "day")
private String day;
@Column(name = "requiredSkillLevel")
private int requiredSkillLevel;
@Column(name = "shiftAmount")
private int shiftAmount;
@Transient
@PlanningVariable(valueRangeProviderRefs = "employee")
private Employee employee;

这是我的@PlanningVariable类:

@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "employeeId")
private int employeeId;
@Column(name = "employeeName")
private String employeeName;
@Column(name = "skillLevel")
private int skillLevel;
@Column(name = "employeeType")
@Enumerated(EnumType.STRING)
private EmployeeType employeeType;
@Column(name = "associatedDepartment")
@Enumerated(EnumType.STRING)
private Departments associatedDepartment;
@Column(name = "weeklyShiftAllowance")
private int weeklyShiftAllowance;

我的限制如下:

public class ScheduleConstraintProvider implements org.optaplanner.core.api.score.stream.ConstraintProvider {
@Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
return new Constraint[]{
requiredSkillLevelOfEmployeesForShifts(constraintFactory),
maximumNumberOfAllowableShiftsForEmployees(constraintFactory)
};
}
private Constraint maximumNumberOfAllowableShiftsForEmployees(ConstraintFactory constraintFactory) {
return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, sum(Shift::getShiftAmount))
.filter((employee, shiftAmount) -> shiftAmount > employee.getWeeklyShiftAllowance())
.penalize("Weekly Shift Allowance for each Employee",
HardSoftScore.ONE_HARD,
(employee, shiftAmount) -> shiftAmount - employee.getWeeklyShiftAllowance());
}
private Constraint requiredSkillLevelOfEmployeesForShifts(ConstraintFactory constraintFactory) {
return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, sum(Shift::getRequiredSkillLevel))
.filter((employee, requiredSkillLevel) -> requiredSkillLevel > employee.getSkillLevel())
.penalize("Required Skill Level for a Shift",
HardSoftScore.ONE_HARD,
(employee, requiredSkillLevel) -> requiredSkillLevel - employee.getSkillLevel());
}

}

我可以添加一些将每个班次视为1个班次分配的内容吗?

代替:

return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, sum(Shift::getShiftAmount))

进行

return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, count())

(如果您想要长,请使用countLong()。(

最新更新