【发布时间】:2019-07-29 06:53:47
【问题描述】:
我正在为 vrp 问题实现自定义克隆器。
文档概述了以下内容:
用链式变量克隆实体是不正当的:实体 A 的变量可能指向另一个实体 B。如果克隆了 A,则其变量必须指向 B 的克隆,而不是原始 B。
因此,如果我们要克隆带有计划变量 previousStandstill 的 Customer,我们需要执行以下操作:
public Customer safeClone() {
Customer clonedCustomer = new Customer(<CONSTRUCTOR_ARGS>);
if (previousStandstill != null) {
if (previousStandstill instanceof Vehicle) {
clonedCustomer.setPreviousStandstill( ((Vehicle)previousStandstill).safeClone();
} else if (previousStandstill instanceof Customer) {
clonedCustomer.setPreviousStandstill( ((Customer)previousStandstill).safeClone();
}
}
// What to do with shadow variables ?
return clonedCustomer;
}
Vehicle.safeClone()
Vehicle clonedVehicle = new Vehicle(<CONSTRUCTOR_ARGS>);
// clone shadow variables ?
clonedVehicle.setNextCustomer(customer.safeClone);
但是,上面的示例不起作用,因为克隆的解决方案不再相同。有关如何安全地克隆链式计划实体的任何指示?我需要深度克隆它的计划变量吗?以及如何处理影子变量?这些也需要深度克隆吗?
【问题讨论】:
-
为什么您要实现自定义克隆器?
@DeepPlanningClone可以影响默认克隆器的行为。
标签: java optaplanner