Search code examples
droolsoptaplanner

Optaplanner with Drools score director -- why not empty solution


I've been looking at the Cloud Balancing demo of OptaPlanner with Drools implementation.

There are 2 rules for this demo (there actually are 4, but 3 of those rules are of the same logic) --

rule "requiredCpuPowerTotal"
when
    $computer : CloudComputer($cpuPower : cpuPower)
    accumulate(
        CloudProcess(
            computer == $computer,
            $requiredCpuPower : requiredCpuPower);
        $requiredCpuPowerTotal : sum($requiredCpuPower);
        $requiredCpuPowerTotal > $cpuPower
    )
then
    scoreHolder.addHardConstraintMatch(kcontext, $cpuPower - $requiredCpuPowerTotal);
end

and

rule "computerCost"  // "Minimize the total maintenance cost."
when
    $computer : CloudComputer($cost : cost)
    exists CloudProcess(computer == $computer)
then
    scoreHolder.addSoftConstraintMatch(kcontext, - $cost);
end

Going by these rules, the optimum solution is NOT to assign ANY of the processes to anyone of the computers. This will satisfy the first rule perfectly-- the cpuPower capacity is never exhausted, let alone gone over. And no computers will be used and thus no maintenance cost.

But the system finds a solution with these 2 rules only-- suboptimizes to assign the processes there are to the computers.

what am i missing, where?

Where is OptaPlanner/Drools told to allocate as many processes as possible?

new to Drools. excuse the naive question, if it is.

//--- UPDATE

the @PlanningEntity() instances to be initialized and put on @PlanningSolution() is the key I think. either explicitly in the code, or by a Construction Heuristic configuration as @yurloc suggested, or .. {don't know what else here if any}


Solution

  • https://docs.optaplanner.org/latest/optaplanner-docs/html_single/#localSearchOverview:

    Local Search needs to start from an initialized solution, therefore it’s usually required to configure a Construction Heuristic phase before it.

    By default, Local Search expects that all entities are initialized and is not allowed to uninitialize them when making changes on the solution. The answer to your last question is that the Construction Heuristic phase assigns a computer to each process even though it actually worsens the score. The Local Search will improve the score in the next phase.

    Thanks to this default behavior you don't have to write rules that penalize processes that haven't been assigned any computer.

    For completeness, note that you can make planning variables nullable but that is an advanced topic. Nullable planning variables are usually only useful for over-constrained planning.