NurseRoster中的总工作时间与项目一样

时间:2017-03-07 14:51:07

标签: optaplanner

我正在像一个类似项目的托儿所工作,我需要一个关于规则的建议。问题是:每位员工每天可以在多个航班上工作,而且航班的开始时间和结束时间也不同。 每位员工都被分配到航班飞行中。 考虑到第一次飞行的开始时间和最后一次飞行的结束时间,是否有规则能够计算总工作时数?例如:flightAssignment1 employee1从8:00结束于10:00开始,flightAssigment2 employee1从12:00结束于15:00开始。 employee1的总工作时间为8:00 - 15:00(7小时)。

我正在尝试这个,但它返回无法解析ObjectType'CheckinRoster.getEmployeeTotalHours'错误:

rule "insertEmployeeAssignmentTotal"
    salience 1 // Do these rules first (optional, for performance)
when
    $employee : Employee()
    $assignmentTotal : CheckinRoster.getEmployeeTotalHours($employee)
then
    insertLogical(new EmployeeAssignmentTotal($employee, $assignmentTotal.intValue()));
end

好的我以这种方式更改了规则:

rule "test"
when
    $employee : Employee()
    accumulate(
        FlightAssignment(employee == $employee, $start : minWorkedHour, $end : maxWorkedHour),
        $max : max($end),
        $min : min($start)
        )
then
    System.out.println($employee.getLabel() + " MIN: " + $min.intValue() + " --- MAX: " + $max.intValue());
    scoreHolder.addSoftConstraintMatch(kcontext, -($max.intValue() - $min.intValue()));
end

获得像这两个好的结果:

Employee 1 MIN: 7 --- MAX: 21
Employee 2 MIN: 6 --- MAX: 21

但也有不好的结果,就像我在整数转换或其他东西中的错误相关

Employee 3 MIN: 2147483647 --- MAX: -2147483648

minWorkedHour和maxWorkedHour在FlightAssignemnt类中是整数,累加器似乎是Double或float。有什么问题?

2 个答案:

答案 0 :(得分:0)

在论坛上查看我找到了一个recent post,其中包含我针对我的问题修改过的解决方案:

    rule "dailyWorkedUp"
when
    $employee : Employee()
    $dailyTotalHours : Number( intValue > 7) from accumulate(
                 $assignmentEmployee:  FlightAssignment(employee == $employee),
                sum($assignmentEmployee.getTotalWorkedHours())
        )
then
    //System.out.println($employee.getLabel() + " " +  (7-$dailyTotalHours.intValue()));
    scoreHolder.addHardConstraintMatch(kcontext, (7-$dailyTotalHours.intValue()));
end


rule "dailyWorkedDown"
when
    $employee : Employee()
    $dailyTotalHours : Number(intValue < 5) from accumulate(
                 $assignmentEmployee:  FlightAssignment(employee == $employee),
                sum($assignmentEmployee.getTotalWorkedHours())
        )
then
    //System.out.println("dailyWorkedDown " + $employee.getLabel() + " " +  ($dailyTotalHours.intValue() - 5));
    scoreHolder.addHardConstraintMatch(kcontext, ($dailyTotalHours.intValue() - 5));
end

但我得到的分数与解决方案不一致。我得到-21的分数,但解决方案应该得分为-40。这有什么问题?

这是关于总工作时间的结果:

  Employee 1 total hours: 7
  Employee 2 total hours: 9
  Employee 3 total hours: 9
  Employee 4 total hours: 12
  Employee 5 total hours: 9
  Employee 6 total hours: 11
  Employee 7 total hours: 11
  Employee 8 total hours: 13
  Employee 9 total hours: 5
  Employee 10 total hours: 5
  Employee 11 total hours: 2
  Employee 12 total hours: 2
  Employee 13 total hours: 1
  Employee 14 total hours: 1
  Employee 15 total hours: 0

答案 1 :(得分:0)

除了一个问题外,这是我的解决方案......

rule "test"
salience 1
when
    $employee : Employee()
    accumulate(
        FlightAssignment(employee == $employee, $start : minWorkedHour, $end : maxWorkedHour),
        $max : max($end),
        $min : min($start)
        )
then
    System.out.println($employee.getLabel() + " ---DIFFERENCE MIN MAX: " + ($max.intValue() - $min.intValue())  + " --- MIN: " + $min + " --- MAX: " + $max);
    //scoreHolder.addSoftConstraintMatch(kcontext, -($max.intValue() - $min.intValue()));
     insertLogical(new EmployeeAssignmentTotal($employee, ($max.intValue() - $min.intValue()) ));
end

// Minimum number of assignments
rule "minimumTotalAssignments"
    when
        EmployeeAssignmentTotal(total < 5,
             $total : total)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, ($total - 5));
end

// Maximum number of assignments
rule "maximumTotalAssignments"
    when
        EmployeeAssignmentTotal(total > 7,
            $total : total)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, (7 - $total));
end

除了一些奇怪的值之外,给定的结果很好:

好结果

Employee 1 ---DIFFERENCE MIN MAX: 11 --- MIN: 7.0 --- MAX: 18.0
Employee 2 ---DIFFERENCE MIN MAX: 7 --- MIN: 12.0 --- MAX: 19.0

坏结果

Employee 3 ---DIFFERENCE MIN MAX: 1 --- MIN: 1.7976931348623157E308 --- MAX: -1.7976931348623157E308

这个奇怪价值观的任何线索?从他们那里得到intValue然后返回2147483647和-2147483648给出相差1