此方法与使用简单、增量和 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);
}
Edit:
具有简单/增量评分的影子变量的替代方法:
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()
。如果其他一切都失败了,您可以首先将客户移至链的末端并将其从那里删除。