Optaplanner 在 ConstraintProvider class 中为员工允许的最大班次添加约束
Optaplanner add constraint for max amount of shifts allowable for an employee in ConstraintProvider class
我试图在 ConstraintProvider class 中添加一个限制分配给员工的班次数量的约束,但我想知道是否有更好的方法来定义它而不是必须添加一个属性在我的@PlanningEntity 和@PlanningVariable class ?
这是我的@PlanningEntity class 属性:
@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 class:
@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());
}
}
与其在我的 Shift 中使用 shiftAmount class,我能否添加一些将每个 Shift 视为 1 个班次分配的东西?
是
而不是:
return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, sum(Shift::getShiftAmount))
做
return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, count())
(如果你想要长的,使用countLong()
。)
我试图在 ConstraintProvider class 中添加一个限制分配给员工的班次数量的约束,但我想知道是否有更好的方法来定义它而不是必须添加一个属性在我的@PlanningEntity 和@PlanningVariable class ?
这是我的@PlanningEntity class 属性:
@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 class:
@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());
}
}
与其在我的 Shift 中使用 shiftAmount class,我能否添加一些将每个 Shift 视为 1 个班次分配的东西?
是
而不是:
return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, sum(Shift::getShiftAmount))
做
return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, count())
(如果你想要长的,使用countLong()
。)