Cloud Balancing Optaplanner:实施过度约束规划

Cloud Balancing Optaplanner: implement an overconstrained planning

我正在尝试使用带有 Optaplanner 的 Optaplanner 和 java 实现一个简单的云平衡系统,该系统具有过度约束的规划。

收藏 我正在尝试使用 Java 的 Optaplanner 库实现一个带有过度约束规划的简单云平衡系统。 我将模型映射到我的问题(车辆和资产),执行变量替换 cpuPower -> 重量、内存 -> 体积。 在不超过这 2 个变量中的任何一个的规则定义之后,drl 文件:

package org.optaplanner.examples.cloudbalancing.solver;
    dialect "java"

import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScoreHolder;

import org.optaplanner.examples.cloudbalancing.domain.Mission;
import org.optaplanner.examples.cloudbalancing.domain.Vehicle;
import org.optaplanner.examples.cloudbalancing.domain.Asset;

global HardSoftScoreHolder scoreHolder;

// ############################################################################
// Hard constraints
// ############################################################################

rule "requiredVolumeTotal"
    dialect "mvel"
    when
        $vehicle : Vehicle($maxVolume : maxVolume)
        Number( $totalReqVolume : intValue() > $maxVolume ) from accumulate 
            ( Asset( vehicle == $vehicle , $volume : volume),
            sum($volume)) 
    then
        scoreHolder.addHardConstraintMatch(kcontext, -($totalReqVolume -$maxVolume));
end

rule "requiredWeightTotal"
    dialect "mvel"
    when
        $vehicle : Vehicle($maxWeight : maxWeight)
        Number( $totalReqWeight : intValue() > $maxWeight ) from accumulate 
            ( Asset( vehicle == $vehicle , $weight : weight),
            sum($weight)) 
    then
        scoreHolder.addHardConstraintMatch(kcontext, -($totalReqWeight -$maxWeight));
end

如果我使用这个简单的例子,我会收到一个分配了所有进程的响应,尽管其中一些不能分配给计算机。对于这个问题,optaplanner 的目的是将实现更改为overconstained plannig。文档说:

  • Add a additional score level (usually a medium level between the hard and soft level) by switching Score type.
  • Make the planning variable nullable.
  • Add a score constraint on the new level (so usually a medium constraint) to penalize the number of unassigned entities (or a weighted sum of them).

我正在执行可空注释和比较方法中的检查:

 @PlanningVariable(valueRangeProviderRefs = {"computerRange"},
        strengthComparatorClass = CloudComputerStrengthComparator.class,
        nullable = true)
public CloudComputer getComputer() {
    return computer;
}

_

@Override
public int compare(CloudComputer a, CloudComputer b) {
     if (a == null || b == null)
         return 0;
    return new CompareToBuilder()
            .append(a.getMultiplicand(), b.getMultiplicand())
            .append(b.getCost(), a.getCost()) // Descending (but this is debatable)
            .append(a.getId(), b.getId())
            .toComparison();
}

这会在解决方法全部未分配且解决方案不正确后更改进程,因为永远是 0soft/0hard。

如何定义一个新的约束(中等级别)来惩罚未分配实体的数量?

首先在您的 DRL 中使用 HardMediumSoftScoreHolder,在您的域中使用 HardMediumSoftScore。查看 DRL 规则的医院床位规划示例。