问题描述
我正在使用这个例子。
https://developers.google.com/optimization/routing/vrptw
我试图限制两个节点之间的最长时间。
routing.addDimension(transitCallbackIndex,// transit callback
30,// allow waiting time
30,// vehicle maximum capacities
false,// start cumul to zero
"Time");
我正在尝试更改突出显示的值,但这是车辆可以行驶的最长时间,而不是两个节点之间的最长时间。
有人可以建议我可以限制两个节点之间的最长时间吗?
解决方法
已在 ortools-discuss 邮件列表上回复:https://groups.google.com/g/or-tools-discuss/c/P3RZ_d_BCZ8
- 如果您有多个回调,则需要在每个最长限制时间内注册一个回调。
- 要禁止弧线,只需让您的回调返回一个数字 > 到车辆最大容量,这样求解器就无法拾取,否则将违反车辆最大容量限制;)
这是我如何实现上述目标。
private static LongBinaryOperator distanceCallback(
RoutingIndexManager manager,SolverRequest solverRequest,int vehicleIndex) {
return (firstIndex,secondIndex) -> {
int firstNode = manager.indexToNode(firstIndex);
int secondNode = manager.indexToNode(secondIndex);
Vehicle vehicle = solverRequest.vehicles[vehicleIndex];
long distance = //get the distance between first node and second node for given vehicle.
if (distance > vehicle.getMaxInterNodeDistance().) {
//This will not be accepted.
return vehicle.getMaxTravelDistance()+1;
}
else
return distance;
};
}