简体   繁体   中英

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++) {...} 

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM