OptaPlanner:使计划实体链接且可为空

时间:2018-07-09 07:37:45

标签: java optaplanner

尊敬的OptaPlanner社区

对于OptaPlanner框架的特定用例,我想使用链式数据结构,如“车辆路由”示例中所使用的那样。在我们的案例中,问题在于有很多客户,并且并非所有客户都能在特定的计划周期内得到服务。因此,我认为使用可为空的计划变量可能会很有用,因此并非所有任务都需要分配,同时仍具有有效的供应商链。 我的问题是,我该如何解决这个问题?有未分配任务的额外链条吗?还有另一种方法可以规避此问题? 问候 拉斐尔

2 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,我需要允许某些客户跳过并最大程度地减少使用的车辆数量。我使用“ Ghost”载具修改了标准的Optaplanner(7.12.0)VRP示例,如下所示:

@XStreamAlias("VrpGhostVehicle")
public class GhostVehicle extends Vehicle {
    @Override
    public Depot getDepot() {
        return null;
    }

    @Override
    public Long getId() {
        return Long.valueOf(0);
    }

    @Override
    public boolean isGhost() {
        return true;
    }

    @Override
    public int getCapacity() {
        return Integer.MAX_VALUE;
    }
}

要建模不能被跳过的客户,我向Customer.java添加了一个“不可跳过的”布尔字段。然后,在您的vehicleRoutingScoreRules.drl中,我调整并添加了以下约束:

rule "vehicleCapacity"
    when
        $vehicle : Vehicle(ghost == false, $capacity : capacity)
        accumulate(
            Customer(
                vehicle == $vehicle,
                $demand : demand);
            $demandTotal : sum($demand);
            $demandTotal > $capacity
        )
    then
        scoreHolder.addHardConstraintMatch(kcontext, $capacity - $demandTotal);
end

// ############################################################################
// Soft distance constraints
// ############################################################################

rule "distanceToPreviousStandstill"
    when
        $vehicle : Vehicle(ghost == false)
        $customer : Customer(previousStandstill != null, vehicle == $vehicle, $distanceFromPreviousStandstill : distanceFromPreviousStandstill)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, - $distanceFromPreviousStandstill);
end

rule "distanceFromLastCustomerToDepot"
    when
        $vehicle : Vehicle(ghost == false)
        $customer : Customer(previousStandstill != null, vehicle == $vehicle)
        not Customer(previousStandstill == $customer)
    then
        Vehicle vehicle = $customer.getVehicle();
        scoreHolder.addSoftConstraintMatch(kcontext, - $customer.getDistanceTo(vehicle));
end


// ############################################################################
// Time window constraints 
// ############################################################################

rule "arrivalAfterDueTime"
    when
        $vehicle : Vehicle(ghost == false)
        TimeWindowedCustomer(dueTime < arrivalTime, vehicle == $vehicle, $dueTime : dueTime, $arrivalTime : arrivalTime)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, -1000 * Math.abs($dueTime - $arrivalTime.longValue()));
end

rule "arrivalBeforeReadyTime"
    when
        $vehicle : Vehicle(ghost == false)
        TimeWindowedCustomer(readyTime > arrivalTime, vehicle == $vehicle, $readyTime : readyTime, $arrivalTime : arrivalTime)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, -1000 * Math.abs($readyTime - $arrivalTime.longValue()));
end


// ############################################################################
// Constraints pertaining to station skipping and vehicle usage
// ############################################################################

rule "skippedCustomer"
    when
        $vehicle : Vehicle(ghost == true)
        $customer : Customer(vehicle == $vehicle, $unskippable : unskippable, $demand : demand)
    then
        if ($unskippable) {
            scoreHolder.addHardConstraintMatch(kcontext, -10000 * $demand);
        } else {
            scoreHolder.addSoftConstraintMatch(kcontext, -10000 * $demand);
        }
end

rule "usedTooManyVehicles"
    when
        $vehicle : Vehicle(nextCustomer != null, ghost == false)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, -500000);
end

最下面的三个规则是我添加到示例中的约束,并且在仅适用于实际车辆的规则中添加了对ghost == false的进一步检查。注意,对于不可跳过的客户,我们设置了硬约束,而对于可跳过的客户,我们设置了软约束。跳过客户或使用车辆的相对权重当然是特定于应用程序的。

答案 1 :(得分:0)

链式计划变量尚不支持nullable=true(至少在7.8版中)。

无论如何,我都会应用连续计划并开始计划第二天的工作-第二天的决定可能会影响第一天的决定(例如,假设您有100包需要在第二天之前送达,但是在第二天,一半的司机正在休假,您没有足够的时间单独在第二天送达所有司机,因此您已经在第一天送达了。)

因此,我将创建具有固定的VehicleDayVehicle的{​​{1}}-并在示例中使用Day的地方使用它。

相关问题