Optaplanner为ConstraintProvider类中的雇员所允许的最大班次增加限制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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());
除了我的Shift类中没有shiftAmount之外,我还可以添加一些东西来将每个Shift视为1个班次分配?
答案
是而不是:
return constraintFactory.from(Shift.class)
.groupBy(Shift::getEmployee, sum(Shift::getShiftAmount))
做
return constraintFactory.from(Shift.class) .groupBy(Shift::getEmployee, count())
((如果需要较长,请使用countLong()
。]
以上是关于Optaplanner为ConstraintProvider类中的雇员所允许的最大班次增加限制的主要内容,如果未能解决你的问题,请参考以下文章