<p>IIRC,已在邮件列表中回复。<br/>
这里有一个要点和一个示例:<a href="https://gist.github.com/Mizux/d616f001e4bf1f7b38ead595a46c40bb" rel="nofollow noreferrer">Mizux/vrp_collect_deliver.py</a></p>
<p>基本上你需要两个维度</p>
<ul>
<li>一种只跟踪货物交付的方法</li>
<li>一个用于跟踪总负载(提取和交付)</li>
<li>开始节点上的一个约束使两个维度的累积变量相等</李>
</ul>
<p>主要思想:
使用两个维度而不是一个维度,将避免解算器使用拾取货物来执行交付</p>
<div class="s-table-container">
^{tb1}$
</div>
<pre class="lang-py prettyprint-override"><code>deliveries = [0, -10, -10, 0]
total = [0, +1, -1, 0]
...
# Add Deliveries constraint.
def delivery_callback(from_index):
"""Returns the demand of the node."""
# Convert from routing variable Index to demands NodeIndex.
from_node = manager.IndexToNode(from_index)
return deliveries[from_node]
delivery_callback_index = routing.RegisterUnaryTransitCallback(delivery_callback)
routing.AddDimensionWithVehicleCapacity(
delivery_callback_index,
0, # null capacity slack
20, # vehicle maximum capacities
False, # start_cumul_to_zero=False since we start full of goods to deliver
'Deliveries')
# Add Load constraint.
def load_callback(from_index):
"""Returns the load of the node."""
# Convert from routing variable Index to demands NodeIndex.
from_node = manager.IndexToNode(from_index)
return total[from_node]
load_callback_index = routing.RegisterUnaryTransitCallback(load_callback)
routing.AddDimensionWithVehicleCapacity(
load_callback_index,
0, # null capacity slack
20, # vehicle maximum capacities
False, # start_cumul_to_zero=False
'Loads')
# Add Constraint Both cumulVar are identical at start
deliveries_dimension = routing.GetDimensionOrDie('Deliveries')
loads_dimension = routing.GetDimensionOrDie('Loads')
for vehicle_id in range(manager.GetNumberOfVehicles()):
index = routing.Start(vehicle_id)
routing.solver().Add(
deliveries_dimension.CumulVar(index) == loads_dimension.CumulVar(index))
...
</code></pre>