Optaplanner将客户从VRP解决方案中移除

Jav*_*yer 13 java optaplanner

基于这个问题,我尝试了以下内容:

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();
}
Run Code Online (Sandbox Code Playgroud)

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

但是我甚至在打电话之前就收到了以下例外 scoreDirector.triggerVariableListeners

java.lang.IllegalStateException:实体(Customer - 9048381398840634905)有一个变量(previousStandstill),其值(Customer - 9070671076516032025)具有sourceVariableName变量(nextCustomer),其值(Customer-8518512081385427431)不是该实体.验证sourceVariableName变量的输入问题的一致性.

另一个问题:

我试图直接删除实体,如下所示:

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();
}
Run Code Online (Sandbox Code Playgroud)

这有效吗?

gru*_*olf 2

此方法与使用简单、增量和 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);
}
Run Code Online (Sandbox Code Playgroud)

编辑:

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

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

}
Run Code Online (Sandbox Code Playgroud)

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