[rules-users] Re: Drools-solver performance optimizations?
Geoffrey De Smet
ge0ffrey.spam at gmail.com
Fri Feb 13 09:36:01 EST 2009
In the nqueensScoreRules.drl examples there's an implementation
commented out which tried something like that, it didn't work out well.
Nevertheless I wouldn't be surprised that there's a way to do it right
and get beat the IntConstraintOccurrence pattern :)
// Don't do this. It breaks performance and scalability!
//rule "hardConstraintsBroken"
// when
// $multipleQueensHorizontal : Number()
// from accumulate(
// $q1 : Queen($id : id, $y : y)
// and Queen(id > $id, y == $y),
// count($q1)
// );
// $multipleQueensAscendingDiagonal : Number()
// from accumulate(
// $q1 : Queen($id : id, $ascendingD : ascendingD)
// and Queen(id > $id, ascendingD == $ascendingD),
// count($q1)
// );
// $multipleQueensDescendingDiagonal : Number()
// from accumulate(
// $q1 : Queen($id : id, $descendingD : descendingD)
// and Queen(id > $id, descendingD == $descendingD),
// count($q1)
// );
// then
// scoreCalculator.setScore(- $multipleQueensHorizontal.intValue()
// - $multipleQueensAscendingDiagonal.intValue() -
$multipleQueensDescendingDiagonal.intValue());
//end
With kind regards,
Geoffrey De Smet
Wim Vancroonenburg schreef:
> To be honest, I do not know if I have to insert a new
> IntConstraintOccurrence. I'm kind of following the Drools Solver
> examples and docs here. Also I have to conform to a certain objective
> function (that was defined in literature), and I know that, at the
> moment, my rules calculate the same value for the objective function for
> several given solutions. So if I were to change my rules, they would
> have to generate the same result. And I do not really know how I would
> have to change them exactly using your approach.
>
> Do you have any tips for that? Perhaps a small example or so?
>
> Sincerely
>
> Wim Vancroonenburg
>
> ----- Original Message ----- From: "Greg Barton" <greg_barton at yahoo.com>
> Newsgroups: gmane.comp.java.drools.user
> To: "Rules Users List" <rules-users at lists.jboss.org>
> Sent: Thursday, February 12, 2009 4:31 PM
> Subject: Re: Drools-solver performance optimizations?
>
>
>> Do you need to insert new IntConstraintOccurrence instances? If you
>> had an accumulator object in working memory that you matched on in the
>> rule conditions you could eliminate one object creation and one insert
>> for each rule that matches a **Constraint object. That could free up
>> lots of cpu and memory usage. The calcScore rule would simply match
>> on the accumulator object.
>>
>> --- On Wed, 2/11/09, Wim Vancroonenburg <ikbenvanmars at gmail.com> wrote:
>>
>>> From: Wim Vancroonenburg <ikbenvanmars at gmail.com>
>>> Subject: [rules-users] Drools-solver performance optimizations?
>>> To: rules-users at lists.jboss.org
>>> Date: Wednesday, February 11, 2009, 12:59 PM
>>> Hi,
>>>
>>> I'm a student currently evaluating Drools Solver for my
>>> dissertation. I am
>>> currently trying to solve an optimization problem with two
>>> different solvers
>>> (one of which is Drools Solver) and I am comparing the
>>> results with earlier
>>> obtained results from literature. However I am having some
>>> troubles with the
>>> performance of Drools Solver, and I was hoping if someone
>>> could look at my
>>> rules to see if they could be tuned:
>>>
>>> rule
>>> "patientsToBeAssignedToRoomsOfAppropriateSex"
>>> when
>>> $n : Night();
>>> $room : Room(sexRestriction ==
>>> Sex.Dependent && capacity >
>>> 1);
>>> $genders : ArrayList(size>1) from
>>> collect(
>>> PatientStay(bed.room == $room, night == $n) );
>>> exists PatientStay(bed.room == $room, night
>>> == $n, $a :
>>> admission,
>>> eval(((PatientStay)$genders.get(0)).getAdmission().getPatient().getSex()
>>> !=
>>> $a.getPatient().getSex()));
>>> then
>>> insertLogical(new
>>> IntConstraintOccurrence("patientsToBeAssignedToRoomsOfAppropriateSex",ConstraintType.NEGATIVE_HARD,50,$room,$n));
>>>
>>> end
>>>
>>> rule "hasRequiredRoomProperties"
>>> when
>>> $pr : RequiredRoomPropertiesConstraint($a :
>>> admission, $r :
>>> room, $w : weight );
>>> $ps : PatientStay(admission == $a, bed.room
>>> == $r);
>>> then
>>> insertLogical(new
>>> IntConstraintOccurrence("hasPreferredRoomProperties",ConstraintType.NEGATIVE_SOFT,50*$w,$ps));
>>>
>>> end
>>>
>>> rule "unplannedTransfers"
>>> when
>>> $ps : PatientStay($a : admission, $b : bed,
>>> $n : night);
>>> $ps2 : PatientStay(admission == $a, bed !=
>>> $b, $n2 :
>>> night,eval($n.getIndex()+1 == $n2.getIndex()));
>>> then
>>> insertLogical(new
>>> IntConstraintOccurrence("unplannedTransfers",ConstraintType.NEGATIVE_SOFT,110,$ps,$ps2));
>>>
>>> end
>>>
>>> rule "hasPreferredRoomProperties"
>>> when
>>> $pr : PreferredRoomPropertiesConstraint($a
>>> : admission, $r :
>>> room, $w : weight );
>>> $ps : PatientStay(admission == $a, bed.room
>>> == $r);
>>> then
>>> insertLogical(new
>>> IntConstraintOccurrence("hasPreferredRoomProperties",ConstraintType.NEGATIVE_SOFT,20*$w,$ps));
>>>
>>> end
>>>
>>> rule "meetsRoomPreference"
>>> when
>>> $mr : MeetsRoomPreferenceConstraint($a :
>>> admission, $r :
>>> room);
>>> $ps : PatientStay(admission == $a, bed.room
>>> == $r);
>>> then
>>> insertLogical(new
>>> IntConstraintOccurrence("meetsRoomPreference",ConstraintType.NEGATIVE_SOFT,8,$ps));
>>>
>>> end
>>>
>>> rule "inGoodDepartment"
>>> when
>>> $gd : GoodDepartmentConstraint($a :
>>> admission, $d :
>>> department);
>>> $ps : PatientStay(admission == $a, $b :
>>> bed,
>>> eval($b.getRoom().getDepartment().equals($d)));
>>> then
>>> insertLogical(new
>>> IntConstraintOccurrence("inGoodDepartment",ConstraintType.NEGATIVE_SOFT,10,$ps));
>>>
>>> end
>>>
>>> rule "inGoodRoom"
>>> when
>>> $gr : GoodRoomConstraint($a : admission, $r
>>> : room, $w :
>>> weight);
>>> $ps : PatientStay(admission == $a, bed.room
>>> == $r);
>>> then
>>> insertLogical(new
>>> IntConstraintOccurrence("inGoodRoom",ConstraintType.NEGATIVE_SOFT,10*$w,$ps));
>>>
>>>
>>> end
>>>
>>> rule "calcScore"
>>> salience -10
>>> when
>>> $count : Number() from accumulate(
>>> IntConstraintOccurrence($w : weight) ,
>>>
>>> sum($w) );
>>> then
>>>
>>> scoreCalculator.setScore(-$count.doubleValue());
>>> end
>>>
>>> The classes with **Constraint in it are possible
>>> combinations that cause a
>>> constraint to be violated, and are calculated and inserted
>>> at initialization
>>> time (and are never changed). I know that the rule
>>> "patientsToBeAssignedToRoomsOfAppropriateSex" is
>>> fairly complex, but even
>>> when I remove it, the performance is not fantastic. Is
>>> there anything else I
>>> can do to get better performance? I'm already using JDK
>>> 1.6 and -server
>>> mode. Furthermore, all classes used here have their default
>>> equals and
>>> hashCode methods, so they don't have an impact on
>>> performance.
>>>
>>> Sincerely,
>>>
>>> Wim Vancroonenburg
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
More information about the rules-users
mailing list