Optaplanner为ConstraintProvider类中的雇员所允许的最大班次增加限制

问题描述 投票:0回答:1

我正在尝试添加一个约束,以限制在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个班次分配?

java spring-boot stream constraints optaplanner
1个回答
0
投票

而不是:

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

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

((如果需要较长,请使用countLong()。]
© www.soinside.com 2019 - 2024. All rights reserved.