Magick
Magick

Reputation: 5112

Optaplanner newbie: nurse softconstraint for weekends

I'm studying Optaplanner, and am doing some experiments with the Nursing Roster.

My goal, for this experiment, is simple: to have nurse "1" be more in favor, and more likely, to work weekends.

I have written the following rules to help make this happen:

 rule "nurseNamed1WorksWeekends"
    when
        $oneNurse: Employee( name = "1")
        $wk : ShiftAssignment( isWeekend = true)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, 1);
end

rule "nurseNamed1MustNotWorkWeekdays"
    when
        $oneNurse: Employee( name = "1")
        not $wk : ShiftAssignment( isWeekend = false)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, 1);

end

However, after running the sample for some time, nurse "1" still never ends up working weekends.

What am I doing wrong?

Thanks

Edit of rule according to laune's suggestions but optaplanner is still reluctant to put the nurse on weekend shifts:

rule "nurseNamed1WorksWeekends"
when
  $oneNurse: Employee( name == "1", )
  $wk : ShiftAssignment( isWeekend == true, employee == $oneNurse)
then
  scoreHolder.addSoftConstraintMatch(kcontext, 1);
end
rule "nurseNamed1MustNotWorkWeekdays"
when
  $oneNurse: Employee( name == "1")
  not ShiftAssignment( isWeekend = false, employee == $oneNurse)
then
  scoreHolder.addSoftConstraintMatch(kcontext, 1);
end

Upvotes: 1

Views: 277

Answers (1)

laune
laune

Reputation: 31290

Don't use = in your constraints - test for equality is expressed using ==.

If the getter for a boolean is called isWeekend, the constraint should be written as

ShiftAssignment( weekend == true )
ShiftAssignment( weekend == false )

or, (for me) preferably

ShiftAssignment( weekend )
ShiftAssignment( ! weekend )

A binding variable in a Conditional Element such as $wk in

not $wk : ShiftAssignment( ! isWeekend )

doesn't make sense. The rule fires if there is no such ShiftAssignment - and then what would $wk being bound to?

The CE

not ShiftAssignment( ! weekend )

is strange: the rule fires if and only if there is no ShiftAssignment for any weekday around at all - not likely.

Adding a value higher than one in the "WorksWeekends" rule should favour nurse 1 on weekends.

Later

rule dislikeNurseOneOnWeekdays
when
  $oneNurse: Employee( name == "1")
  ShiftAssignment( isWeekend = false, employee == $oneNurse)
then
  scoreHolder.addSoftConstraintMatch(kcontext, -1);
end

Using a smaller value (e.g. -10) will make it even harder for the First Nurse to work on weekdays: ten shifts during the weekend are needed to balance one during the week.

Upvotes: 4

Related Questions