Search code examples
droolsoptaplanner

How to fix number of females employees in a shift


I am adding a custom rule for Employee shift where i have 4 types of shifts and in one type of shift the number of female employees have to be fix

i have tried adding a field in shift class namely requiredFemalesEmployees which is set to 1 //hard constraint rule "OneFemaleInShiftA"

when    
$gender:Employee(gender=="F")
 $rfe:Shift(requiredFemalesEmployees==1)
 accumulate(
$a:ShiftAssignment(employee==$gender,$shift:shift.requiredFemalesEmployees),
 $total :count($a)
)

then
 if($total.intValue()!=1){
 scoreHolder.addHardConstraintMatch(kcontext, - 1);
 } 

end

any suggestions will be a great help.


Solution

  • First, you created a variable named $rfe, but is unused, and in this line: $a:ShiftAssignment(employee==$gender,$shift:shift.requiredFemalesEmployees), what'r you assigning to $shift ?

    This is my example:

    rule "oneFemaleInShift"
        when    
            $gender:Employee(gender=="F")
            $rfe:Shift(requiredFemalesEmployees==1)
            Number(intValue!=1) from accumulate(
                $a:ShiftAssignment(employee==$gender, ¿¿$shift:shift.requiredFemalesEmployees??),
                count($a)
            )
    
        then
            scoreHolder.addHardConstraintMatch(kcontext, - 1);
    
    

    We need the domain model or the source of java POJOs to know the relations between them.

    I think this will help you:

    rule "oneFemaleInShift"
        when    
            $femaleEmployee:Employee(gender=="F") //GET FEMALE POJOS
            $rfe:Shift(requiredFemalesEmployees==1) // GET SHIFT WHERE FEMALE IS REQUIRED
            Number(intValue > 0) from accumulate( //COUNT NUMBER OF FEMALE EMPLOYEES IN THAT SHIFT, PENALIZE SOLUTION WHERE THERE ARE LESS THAN 1
                $a:ShiftAssignment(employee==$femaleEmployee, shift==$rfe), 
                count($a)
            )
        then
            scoreHolder.addHardConstraintMatch(kcontext, - 1); // LOOK AT THE VALUE OF HARD SCORE, PROPORTION WITH OTHER HARD CONSTRAINT