Optaplanner newbie: nurse softconstraint for weekends - drools

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

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.

Related

drools how to fire specified group dynamically

i am using drools 7.x.
my logic looks like following:
if(variableA == 1) {
if(variableA1 == 2) {
....
} else if(variableA1 == 3) {
....
}
}else {
if(variableB1 == 1) {
....
}else if(variableB1 == 2) {
if(variableN == 1) {
}else if(variableN == 2) {
}
}
}
by the way, these variables not in the same class, i intend to insert them as fact in drl.
how can i define the rules? or how can i define rules like :
rule 1
when
then
end
rule 2
when
then
end
rule 1-1
when
then
end
rule 1-2
when
then
end
rule 2-1
when
then
end
rule 2-2
when
then
end
wherein, only one of rules will be fired in rule 1 and rule 2, rule 1-1 and rule 1-2 is group1, rule 2-1 and rule 2-2 is group2.
if rule 1 is fired, then only one of rules is fired in group1, there is no need to test group2. While if rule 2 is fired, then only one of rules is fired in group2, there is no need to test group1.
salience for rules priority
logical insertions to depend on other rule
activation-group for xor logic
rule "1"
salience 1
activation-group "group 0"
when
$model : Model(a == 1)
then
insertLogical(new GroupActivation("group 1", $model));
System.out.println("rule 1");
end
rule "2"
salience 1
activation-group "group 0"
when
$model : Model(b == 1)
then
insertLogical(new GroupActivation("group 2", $model));
System.out.println("rule 2");
end
rule "1-1"
activation-group "group 1"
when
GroupActivation(name == "group 1", $model : model)
Model(this == $model, a1 == 1)
then
System.out.println("rule 1-1");
end
rule "1-2"
activation-group "group 1"
when
GroupActivation(name == "group 1", $model : model)
Model(this == $model, a2 == 1)
then
System.out.println("rule 1-2");
end
rule "2-1"
activation-group "group 2"
when
GroupActivation(name == "group 2", $model : model)
Model(this == $model, b1 == 1)
then
System.out.println("rule 2-1");
end
rule "2-2"
activation-group "group 2"
when
GroupActivation(name == "group 2", $model : model)
Model(this == $model, b2 == 1)
then
System.out.println("rule 2-2");
end
Model.java
public class Model {
private int a;
private int a1;
private int a2;
private int b;
private int b1;
private int b2;
...
GroupActivation.java
public class GroupActivation {
private String name;
private Model model;
...
I'm going to assume all of these variables exist inside of an class which I'm going to call Inputs. You'd call these rules by passing an instance of Inputs into the rules.
I'm also going to assume that the last 'else if' in your example was a type and you're actually checking that variableN == 2.
rule "A1-2"
when
Inputs( variableA == 1,
variableA1 == 2 )
then
// ...
end
rule "A1-3"
when
Inputs( variableA == 1,
variableA1 == 3 )
then
// ...
end
rule "B1"
when
Inputs( variableA != 1,
variableB1 == 1 )
then
// ...
end
rule "B2-N1"
when
Inputs( variableA != 1,
variableB1 == 2,
variableN == 1 )
then
// ...
end
rule "B2-N2"
when
Inputs( variableA != 1,
variableB1 == 2,
variableN == 2 )
then
// ...
end
Rather straight forward. The key is that you need to check that the condition for variableA ==1 is not true for the rules that deal with B. Basically when you're converting an if/elsif/else into rules, you need to negate the conditions from the previous if-clauses on your left-hand-side.
There's no "specified groups" involved. Mostly because there's no explanation about what you mean by these words.
Drools also has inheritance. There's no real reason to use it here, but you could if you wanted to:
rule "A1"
when
$i: Inputs(variableA == 1)
then // notice empty "then"
end
rule "A1 = 2" extends "A1"
when
Inputs( variableA1 == 2 ) from $i
then
//
end

Drools variable binding: reassignment not allowed

Assume that MyObject is a Java Object with an integer property called integerProperty.
I would like to write a Drools rule like the following (syntactically incorrect):
rule "myRule"
when
MyObject( $integerProperty : integerProperty )
accumulate(
$o : MyObject(
integerProperty == $integerProperty + 1,
$integerProperty : this.integerProperty
);
$total : count($o);
$total > 10
)
then
[BLABLABLA]
end
I want to fire my rule when there is a set of more than 10 elements of type MyObject whose integerProperty values form an arithmetic sequence, e.g. 1, 2, 3, … In this case, I will assign a negative value to an HardMediumSoftLongScore object (hoping that information this helps).
I am sure that an MyObject having a given integerProperty is unique (eg. there is just one MyObject having integerProperty equal to, say, 2). The problem is that I'm not allowed to re-assign the variable binding $integerProperty. Is there a way around this?
Try implementing the below example and check whether you are getting your desired output. Before firing the rule insert the previous object as a global variable (prev in the example below) in the drool session.
global MyObject prev;
rule "myRule"
when
$total : Number(doubleValue > 10) from accumulate(
$o : MyObject( prev.getIntegerProperty() != null && $i : integerProperty == prev.getIntegerProperty() + 1),
init( double total = 0;),
action( total += 1; ),
reverse( total -= 1; ),
result( new Double( total ) ) )
then
[BLABLABLA]
end
In your java code before firing the rule set the previous object as a global object. Follow the code below :
kieSession.setGlobal("prev",myprevobject)
kieSession.insert(myobject)
kieSession.fireAllRules()

Get index difference on Optaplanner - Drools

I'm working on a timetable scheduling problem with Drools. I have a rule that force the solver to always have a lecture on the previous timeslot (given a curriculum), at least since the first timeslot (index = 0) onwards. Example: if you have lectures on Monday from the timeslot 0 to 4 consecutively, you're ok; but if, for instance, you don't have a lecture on timeslot 2, then you have a -1 hard score.
// PeriodsWithoutLectures: All the periods must have consecutive lectures, at least since the 0 index timeslot onwards
rule "periodsWithoutLectures"
when
$curriculum : Curriculum()
Lecture(curriculumList contains $curriculum,
$day : day, $timeslotIndex : timeslotIndex, timeslotIndex > 0, period != null)
not Lecture(curriculumList contains $curriculum,
day == $day, timeslotIndex == ($timeslotIndex - 1))
then
scoreHolder.addHardConstraintMatch(kcontext, -1);
end
All of the above works, but what I need to do, is to know how many "missing lectures" I have. Giving the previous example, if I have a lecture on timeslot 0 and on timeslot 4, I'm missing 3 lectures in the middle (on timeslot 1, 2 and 3), so I need a -3 hard score (with my current approach, I'll get a -1 hard score).
Any help will be really useful, thanks!
UPDATE
This is what I have now:
rule "periodsWithoutLectures"
when
$curriculum : Curriculum()
Lecture(curriculumList contains $curriculum,
$day : day, $timeslotIndex1 : timeslotIndex, period != null)
Lecture(curriculumList contains $curriculum,
$day == day, $timeslotIndex2 : timeslotIndex, timeslotIndex > $timeslotIndex1 + 1, period != null)
not Lecture(curriculumList contains $curriculum,
day == $day, timeslotIndex > $timeslotIndex1 && < $timeslotIndex2)
then
scoreHolder.addHardConstraintMatch(kcontext, ($timeslotIndex2 - $timeslotIndex1) - 1);
end
Write a rule that does something like this:
Select a lecture
Select a second lecture with a timeslot on the say day and at a later timeSlotIndex
there is no lecture between those 2 lectures (so with a timeslot index between the first and second lecture)
which then
penalize the timeslotIndex delta between the first and second lecture

Drools - Accumulate logic

I need help in writing the accumulate logic for below requirement:
Requirement: Certain rules will provide the percentage to be applied for global value. Another set of rules should use the aggregate total percentage in determining the result.
For example: 0.75 is the global value passed as input(threshold).
Rule 1 might apply -10% of the fixed value ie. 0.75 - (0.75 * 0.10) = 0.675
Rule2 will apply + 20% off updated value. ie., 0.675 + (0.675 * 0.20) = 0.81
My global value is 0.75 (Threshold)
Using the below rules I am trying to applied the percentage applicable for a fixed value :
//class imports
global Double FIXED_THRESHOLD;
//Rules
rule "Prediction Rule_2"
lock-on-active true
no-loop true
salience (2)
when
LossInput (airBagDeployed == 'Y' , driveable == 'N')
result : RuleResult(predictedTotalsThreshold == 0)
then
insert(new ControlFact( -10.0 ) ); //Reduce -10% to global value 0.75 - 0.75* 0.10 = 0.675
System.err.println("New control fact added to working memory....");
end
rule "Prediction Rule_1"
lock-on-active true
no-loop true
salience (1)
when
LossInput (airBagDeployed == 'Y' , driveable == 'N', make == 'Honda' )
result : RuleResult(predictedTotalsThreshold == 0)
then
insert(new ControlFact( 20.0 ) ); // Add 20% to the updated aggregate (0.20 % of 0.675).
System.err.println("New control fact added to working memory....");
end
I tried the below accumulate logic but obviously it is wrong. It is applying only to fixed value always instead of the updated value.
rule "Aggregate All Threshold"
no-loop true
when
$aggregateTotalsThresholdPercentage : Number() from accumulate(
ControlFact( $totalsThreshold : totalsThresholdPercentage ),
sum( ( FIXED_THRESHOLD + ( FIXED_THRESHOLD * $totalsThreshold ) / 100 ) ) )
ruleResult: RuleResult(predictedTotalsThreshold == 0)
then
ruleResult.setPredictedTotalsThreshold($aggregateTotalsThresholdPercentage.doubleValue());
update(ruleResult);
end
POJO:
public class LossInput{
private String airBagDeployed;
private String driveable;
private String make;
}
public class ControlFact {
public double totalsThresholdPercentage;
}
public class RuleResult {
private double predictedTotalsThreshold;
}
//insert facts in working memory
kieSession.insert(lossInput);
kieSession.insert(ruleResult);
kieSession.setGlobal("FIXED_THRESHOLD", new Double(0.75));
kieSession.fireAllRules();
Please help on the accumulate logic to apply the updated value everytime when percentage threshold to be applied.
You cannot use accumulate/sum this way because you add the FIXED_THRESHOLD for each ControlFact.
Insert ControlFacts as you have in the "Prediction..." rules (without all the rule attributes). Use each ControlFact to update RuleResult's predictedTotalsThreshold. The "Aggregate" rule will fire repeatedly, and therefore you need to make sure to retract the used ControlFact.
rule "Aggregate All Threshold"
when
ControlFact( $ttp: totalsThresholdPercentage );
$res: RuleResult( $ptt: predictedTotalsThreshold)
then
double nt = $ptt + $ptt*$ttp/100;
modify( $res ){ setPredictedTotalsThreshold( $nt ) }
retract( $res );
end

Working with consecutive events in drools

I need help with the following rule:
Accounts with multiple consecutive deposits and immediately after 4 hours, multiple extractions whose sum is equal to the same amount deposited using different ATM in the same bank.
(There aren't intermediate operations between the last deposit and the first extraction)
I have the class Operation: Account, Amount, Operation, ATM, Timestamp.
I have this rule:
declare Operation
#role(event)
#timestamp(Timestamp)
end
rule
when
$op1:Operation($acc:Account,$b:Bank,$amount:Amount,Operation‌​=="CREDIT",$atm:ATM)
not Operation(Account == $acc,ATM != $atm, Bank == $b, this before $op1) accumulate($op2:Operation(Account == $acc,ATM != $atm, Bank == $b,Operation=="CREDIT",$amount1:Amount);$c:count($op2),$d:su‌​m($amount1);$c>=4,$d‌​>=300)
then
System.out.println( "Account: " + $acc + " amount " + ($amount + $d) + " operations " + $c+ " in different banks");
end
The previous rule returns the sum of the deposits of the accounts that have made more than 4 operations in different ATMs of the same bank,my doubt is how to take into account that they are consecutive and that after 4 hours compare the previous sum with the sum of the extractions from that account.
If this rule fires,
rule "set up Monitor"
when
$op1: Operation( $a:account, $b:bank, $amt1:amount, operation‌​=="CREDIT", $atm1: atm )
$op2: Operation( account==$a, bank==$b, $amt2:amount, operation‌​=="CREDIT", atm!= $atm1 )
not Operation(account == $acc, atm != $atm1, bank == $b,
this before $op2, this after $op1 )
not Monitor( bank == $bank, account == $acc )
then
insert( new Monitor( $bank, $acc, $amt1 + $amt2, "CREDIT" );
end
you have a Monitor inserted. The monitor has the obvious fields and a state field, set to "CREDIT".
rule "add deposit"
when
$op1: Operation( $a:account, $b:bank, $amt1:amount, operation‌​=="CREDIT", $atm1: atm )
$mon: Monitor( account==$a, bank==$b, $bal: balance, state == "CREDIT" )
then
// another deposit
modify( $op1 ){ setAmount( $bal + $amt1 ) }
end
With this event, the state changes to withdrawal.
rule "state changes to withdrawal"
when
$op1: Operation( $a:account, $b:bank, $amt1: amount, operation‌​=="WITHDRAW", $atm1: atm )
$mon: Monitor( account==$a, bank==$b, $bal: balance, state == "CREDIT" )
then
// switch to withdraw
modify( $op1 ){ setAmount( $bal + $amt1 ),
setState( "WITHDRAW" ) }
end
And here it begins to get messy. We need to check whether this (first) withdrawal cashes in on all deposits (balance == $amt1) or just reduces the balance or withdraws more than the previous sequence of deposits have made.
I quit here, but I think I've shown that this isn't possible to do with a single rule.