drools得分计算逻辑的代码

时间:2014-08-25 11:19:45

标签: drools optaplanner

我想使用optaplanner安排事件,其中所需的资源按事件是软约束。

我在java代码中进行了分数计算,如:

int resources = 0;
int quantity = 0;
for(ResourceAvailability availability : eventDate.getResourceAvailabilities()){
    for(ResourceAvailability required : event.getResourceAvailabilities()){
         if(availability.equals(required)){
              resources += required.getQuantity();
              quantity += availability.getQuantity();
         }
    }
}
// Soft constraints
int resourcesAvailable = quantity - resources;
if (resourcesAvailable < 0) {
    softScore += resourcesAvailable;
}

这是有效但速度很慢,我希望得到这个软分数计算逻辑的等效drools代码。

那么如何为此编写drools代码?

我检查了机器重新分配问题的drools文件,但无法将其与我的问题联系起来。

此处Event是我的计划实体,EventDate是计划变量。

我正在写drools规则:

rule "requiredResources"
    when
        $eventDate : EventDate()
        $requiredResources : Number(intValue > 1) from accumulate(
            Event(
                eventDate == $eventDate,
                //here logic for comparing required and available resource and then find
                //sum of required resources and available resoulces
                //add difference to score
            ),
            sum($quantity)
        )
    then
        System.out.println($requiredResources+$availableResources);
end

1 个答案:

答案 0 :(得分:0)

来自the machine reassignment example

rule "machineUsage"
  when
    $machineCapacity : MrMachineCapacity($machine : machine, $resource : resource)
    accumulate(
        $processAssignment : MrProcessAssignment(machine == $machine),
        $usageTotal : sum($processAssignment.getUsage($resource))
    )
  then
    scoreHolder.addHardConstraintMatch($capacity - $usage); // see docs chapter score calculation
end