java - Optaplanner 从工作的 VRP 解决方案中删除客户

标签 java optaplanner

基于此question我尝试了以下方法:

public void doFactChange() {
    Location toBeRemovedLocation = customerToBeRemoved.getLocation();
    Location lookUpWorkingObject = (Location) scoreDirector.lookUpWorkingObject(toBeRemovedLocation);
    scoreDirector.beforeProblemFactRemoved(lookUpWorkingObject);
    routingSolution.getLocationList().remove(lookUpWorkingObject);
    scoreDirector.afterProblemFactRemoved(lookUpWorkingObject);
    Customer workingCustomer = (Customer) scoreDirector.lookUpWorkingObject(customerToBeRemoved);

    for (Customer customer : routingSolution.getCustomerList()) {
        while (customer != null) {
            if (customer == workingCustomer) {
                if (customer.getPreviousStandstill() != null) {
                    scoreDirector.beforeVariableChanged(customer, "previousStandstill");
                    customer.getPreviousStandstill().setNextCustomer(customer.getNextCustomer());

                    scoreDirector.afterVariableChanged(customer, "previousStandstill");
                }

                scoreDirector.beforeVariableChanged(customer, "nextCustomer");
                customer.getNextCustomer().setPreviousStandstill(customer.getPreviousStandstill());
                scoreDirector.afterVariableChanged(customer, "nextCustomer");
            }
            customer = customer.getNextCustomer();
        }
    }

    scoreDirector.beforeEntityRemoved(workingCustomer);
    routingSolution.getCustomerList().remove(workingCustomer);
    scoreDirector.afterEntityRemoved(workingCustomer);
    scoreDirector.triggerVariableListeners();
}

注意:customerToBeRemoved 是在调用 doFactChange() 之前创建的实例对象

但我什至在调用 scoreDirector.triggerVariableListeners 之前就收到了以下异常

java.lang.IllegalStateException: The entity (Customer--9048381398840634905) has a variable (previousStandstill) with value (Customer--9070671076516032025) which has a sourceVariableName variable (nextCustomer) with a value (Customer-8518512081385427431) which is not that entity. Verify the consistency of your input problem for that sourceVariableName variable.

另一个问题:

我试过直接删除实体如下:

public void doFactChange() {
    Location toBeRemovedLocation = customerToBeRemoved.getLocation();
    Location lookUpWorkingObject = (Location) scoreDirector.lookUpWorkingObject(toBeRemovedLocation);
    scoreDirector.beforeProblemFactRemoved(lookUpWorkingObject);
    routingSolution.getLocationList().remove(lookUpWorkingObject);
    scoreDirector.afterProblemFactRemoved(lookUpWorkingObject);
    Customer workingCustomer = (Customer) scoreDirector.lookUpWorkingObject(customerToBeRemoved);

    scoreDirector.beforeEntityRemoved(workingCustomer);
    routingSolution.getCustomerList().remove(workingCustomer);
    scoreDirector.afterEntityRemoved(workingCustomer);
    scoreDirector.triggerVariableListeners();
}

这有效吗?

最佳答案

此方法适用于使用简单、增量和 drl 分数计算器的 optaplanner VRP 示例:

public void removeRandomCustomer()
{
    doProblemFactChange(scoreDirector -> {
        VehicleRoutingSolution solution = scoreDirector.getWorkingSolution();
        int rnd = 4; //select a random customer
        if (solution.getCustomerList().size() > rnd)
        {
            Customer customer = solution.getCustomerList().get(rnd);
            scoreDirector.beforeEntityRemoved(customer);
            removeCustomer(solution, customer);
            scoreDirector.afterEntityRemoved(customer);
            scoreDirector.triggerVariableListeners();
        }
    });
}

private void removeCustomer(VehicleRoutingSolution solution, Customer customer)
{
    Standstill anchor = customer.getPreviousStandstill();
    Customer nextCustomer = customer.getNextCustomer();
    //anchor shouldn't be null in an initialized solution
    if (anchor != null)
        anchor.setNextCustomer(nextCustomer);
    if (nextCustomer != null)
        nextCustomer.setPreviousStandstill(anchor);
    solution.getCustomerList().remove(customer);
}

编辑:

具有简单/增量评分的影子变量的替代方法:

private void removeCustomer(ScoreDirector<VehicleRoutingSolution> scoreDirector, VehicleRoutingSolution solution, Customer removeCustomer)
{
    final Customer customer = scoreDirector.lookUpWorkingObject(removeCustomer);
    Standstill anchor = customer.getPreviousStandstill();
    Customer nextCustomer = customer.getNextCustomer();

    //scoreDirector.beforeVariableChanged(anchor, "nextCustomer");
    scoreDirector.beforeVariableChanged(customer, "previousStandstill");    //sets anchor.nextCustomer=null
    customer.setPreviousStandstill(null);
    scoreDirector.afterVariableChanged(customer, "previousStandstill");
    //scoreDirector.afterVariableChanged(anchor, "nextCustomer");
    if(nextCustomer!=null)
    {
        //scoreDirector.beforeVariableChanged(customer, "nextCustomer");
        scoreDirector.beforeVariableChanged(nextCustomer, "previousStandstill");  //sets customer.nextCustomer=null
        nextCustomer.setPreviousStandstill(anchor);
        scoreDirector.afterVariableChanged(nextCustomer, "previousStandstill");
        //scoreDirector.afterVariableChanged(customer, "nextCustomer");
    }

    scoreDirector.beforeEntityRemoved(customer);
    //clone customer list
    ArrayList<Customer> changedList = new ArrayList<>(solution.getCustomerList());
    solution.setCustomerList(changedList);
    solution.getCustomerList().remove(customer);
    scoreDirector.afterEntityRemoved(customer);
    scoreDirector.triggerVariableListeners();  //sets customer.vehicle=null, sets all nextCustomer.vehicle=null

}

解决方案现在应该处于有效状态,但您可能必须修复增量评分(简单评分很好,因为 doProblemFactChange() 会自动重新计算),也许通过删除VehicleRoutingIncrementalScoreCalculator.beforeEntityRemoved()。如果一切都失败了,您可以先将客户移动到链的末端并将其从那里移除。

关于java - Optaplanner 从工作的 VRP 解决方案中删除客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47943037/

相关文章:

Java lwjgl 和 slick 不显示我的纹理

java - 我如何开始使用 JFreeChart?

java - 如何有效地调整图像处理算法的参数?

java - 时间窗口变量的解释,尤其是到达时间

java - OptaPlanner - 在同一规划实体中使用 @PlanningListVariable 和 @PlanningVariable 时出现问题

java - JPA中两个关系中的一个参数

java - Android:在 Facebook 上发布图像时出现 NullPointerException

java - OptaPlanner - 从另一个规则调用时全局变量为空

java - OptaPlanner 中具有优先级和车辆故障的 CVRPTW

java - 如何在 Java 中运行 OptaPlanner CVRP