Jack Kass
Jack Kass

Reputation: 55

Optaplanner : Using loop for score calculation?

I'm working on a task scheduling problem. I'd like implement a rule to make sure that at any time, the whole process doesn't use more resources that available. To do this, i thought about looping over each second of the total time process and calculate the sum of the resources used at each second like this :

accumulate(Task($sec <=endTime, $sec>= startTime, $res : resources);
                $sum : sum($res);
                $sum> Global.getInstance().getAvailableResources())

"$sec" represent a second to check.

how can i loop over each second using drools ?

Is there any equivalent for this :

for ($sec= 0; $sec<$totalTime; $sec++)  {...}

Upvotes: 0

Views: 241

Answers (1)

Geoffrey De Smet
Geoffrey De Smet

Reputation: 27327

Checking that for every second individually is likely to slow down your score calculation a lot.

An alternative that might work better: I 'd write a rule to logically insert a Mark every second when at least 1 task starts or ends.

when
   Task($startTime : startTime, $endTime : endTime)
then
   insertLogical(new Mark($startTime));
   insertLogical(new Mark($endTime));
// Important: 2 Mark instances with the same time
//            are equals() true (and therefore have the same hashCode()).

Then it's just a matter of accumulating between every 2 marks

when
   Mark($startTime: time)
   Mark(time > $startTime, $endTime : time)
   not Mark(time > $startTime, time < $endTime)
   $total : ... accumulate( ... over every Task between $startTime and $endTime ...)
then
   scoreHolder.addHardConstraintMatch(($available - $total) * ($endTime - $startTime));

Upvotes: 1

Related Questions