【发布时间】:2021-05-22 09:28:46
【问题描述】:
我正在尝试解决容量路由问题,其中我有一组需要不同数量和不同类型项目的节点。
另外我想允许节点丢弃,因为所有节点都带有一种物品可能仍然超过车辆容量,因此会导致没有解决方案。
然而,最终应该为所有节点提供服务,因此我使用迭代方法,将每个项目类型视为单独的路由问题。
但我想知道是否可以使用析取或类似的东西来解决“全局”路由问题。任何有关这是否可能的帮助表示赞赏。
Example:
Node 1 - item A - demand 10
Node 2 - item A - demand 10
Node 3 - item A - demand 12
Node 4 - item B - demand 10
Node 5 - item B - demand 10
vehicle I - capacity 20
vehicle II - capacity 10
我的方法:
首先解决项目 A:车辆 I 服务于节点 1 和 2,节点 3 被丢弃,保存丢弃的节点以供以后迭代
然后求解 B 项:车辆 I 服务于节点 4 和 5,车辆 II 空闲
求解剩余节点 3:车辆 I 服务于节点 3
编辑 我调整了我的方法以适应@mizux 的答案。代码下方:
EDIT2 修复了第一个循环中的需求回调函数仍会引用 product_index 变量并因此返回错误需求的错误。使用functools.partial修复。
import functools
from ortools.constraint_solver import pywrapcp, routing_enums_pb2
class CVRP():
def __init__(self, data):
# assert all(data['demands'] < max(data['vehicle_capacities'])) # if any demand exceeds cap no solution possible
self.data = data
self.vehicle_names_internal = [f'{i}:{j}' for j in data['products'] for i in data['vehicle_names']]
self.manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']), len(self.vehicle_names_internal), data['depot'])
self.routing = pywrapcp.RoutingModel(self.manager)
transit_callback_id = self.routing.RegisterTransitCallback(self._dist_callback)
self.routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_id)
# set up dimension for each product type for vehicle capacity constraint
for product_index, product in enumerate(data['products']):
dem_product_callback = functools.partial(self._dem_callback_generic, product_index=product_index)
dem_callback_id = self.routing.RegisterUnaryTransitCallback(dem_product_callback)
vehicle_product_capacity = [0 for i in range(len(self.vehicle_names_internal))]
vehicle_product_capacity[product_index*data['num_vehicles']:product_index*data['num_vehicles']+data['num_vehicles']] = data['vehicle_capacities']
print(product_index, product)
print(self.vehicle_names_internal)
print(vehicle_product_capacity)
self.routing.AddDimensionWithVehicleCapacity(
dem_callback_id,
0,
vehicle_product_capacity,
True,
f'capacity_{product}',
)
# disjunction (allow node drops)
penalty = int(self.data['distance_matrix'].sum()+1) # penalty needs to be higher than total travel distance in order to only drop locations if not other feasible solution
for field_pos_idx_arr in self.data['disjunctions']:
self.routing.AddDisjunction([self.manager.NodeToIndex(i) for i in field_pos_idx_arr], penalty)
def _dist_callback(self, i, j):
return self.data['distance_matrix'][self.manager.IndexToNode(i)][self.manager.IndexToNode(j)]
def _dem_callback_generic(self, i, product_index):
node = self.manager.IndexToNode(i)
if node == self.data['depot']:
return 0
else:
return self.data['demands'][node, product_index]
def solve(self, verbose=False):
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.AUTOMATIC)
search_parameters.local_search_metaheuristic = (
routing_enums_pb2.LocalSearchMetaheuristic.AUTOMATIC)
search_parameters.time_limit.FromSeconds(30)
self.solution = self.routing.SolveWithParameters(search_parameters)
【问题讨论】:
-
在您的示例中,总需求超过了车辆的总容量。您不会找到服务于所有节点的解决方案。如果可以多程派车,建议复制现有车辆增加车辆数量。您能否添加有关您期望的“析取”的更多信息,例如每辆车只有一种物品?
-
我知道,这就是为什么我允许删除节点(有惩罚)并为剩余的未访问节点设置一个新问题。关于分离:每辆车只能装载一种类型的物品,并且不能有一条路线需要为不同的物品提供服务。但是我不知道如何用代码来准确表示。
标签: python optimization or-tools vehicle-routing