gpt4 book ai didi

python - 不同车速的车辆路线(谷歌或工具)

转载 作者:行者123 更新时间:2023-12-04 09:19:37 27 4
gpt4 key购买 nike

目前,我正在研究不同车速的车辆路径问题。
我的方法是对每辆车使用速度调整比(这意味着我有默认速度),来建立我使用代码的时间限制:
( data['travel_service'] 是时间矩阵 + 服务时间,data['travel_service'] 仅是服务时间)

data['vehicle_speed_adjustment'] = [1.5, 1.3, 1.2, 1]
def create_vehicle_travel_service_time_callback(travel_service_matrix, service_time, speed_adjust_ratio):
def time_callback(from_index, to_index):
"""Returns the travel time between the two nodes + the service time of starting node."""
# Convert from routing variable Index to time matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
if travel_service_matrix[from_node][to_node] == sys.maxsize or travel_service_matrix[from_node][to_node] == service_time[from_node]:
return travel_service_matrix[from_node][to_node]
else:
return int((travel_service_matrix[from_node][to_node]-service_time[from_node])/speed_adjust_ratio) + service_time[from_node]
return time_callback

# time_callback_index = []
for vehicle_id in range(data['num_vehicles']):
time_callback = create_vehicle_travel_service_time_callback(data['travel_service'], data['service_time'], data['vehicle_speed_adjustment'][vehicle_id])
time_callback_index = routing.RegisterTransitCallback(time_callback)
print(time_callback_index)
routing.AddDimension(
time_callback_index,
max_waiting_time, # allow waiting time
max_operation_time, # maximum time per vehicle
False,
'Time')

time_dimension = routing.GetDimensionOrDie('Time')
问题是,当我尝试运行该程序时,该程序只能识别 data['vehicle_speed_adjustment'] 的第一个值。列表是 1.5 并将其应用于每辆车。
有人可以解释为什么会发生这种情况吗?
data['travel_service'] = [[0, 4, 22, 21, 48, 28, 8, 23, 16, 14, 23, 37], [59, 0, 79, 78, 104, 84, 63, 76, 68, 66, 78, 91], [32, 34, 0, 12, 38, 32, 31, 14, 41, 40, 41, 52], [27, 29, 8, 0, 35, 28, 27, 11, 36, 35, 37, 47], [58, 59, 38, 39, 0, 39, 52, 41, 62, 61, 60, 60], [36, 37, 30, 30, 37, 0, 32, 30, 41, 40, 39, 34], [18, 18, 31, 31, 52, 34, 0, 32, 20, 19, 22, 39], [33, 31, 14, 15, 41, 32, 32, 0, 42, 41, 43, 54], [44, 41, 59, 58, 80, 61, 38, 60, 0, 31, 36, 53], [69, 66, 85, 84, 106, 87, 64, 86, 58, 0, 65, 83], [33, 33, 41, 41, 60, 41, 22, 43, 18, 20, 0, 31], [45, 44, 50, 49, 58, 34, 37, 52, 33, 36, 29, 0]]

data['travel_service'] = [0, 55, 10, 6, 10, 8, 10, 10, 28, 55, 10, 8]

data['num_vehicles'] = 4

最佳答案

几乎不错,您只需要创建一个回调索引数组,然后将该数组传递给 AddDimension ...
注意:数组长度必须等于车辆编号。
注意2:您可以为少数车辆重复使用相同的注册回调索引
例如你有两辆自行车,三辆汽车按这个顺序你可以传递一个数组 [1, 1, 2, 2, 2]假设 RegisterTransitCallback 返回 12 ...
引用:https://github.com/google/or-tools/blob/45770b833997f827d322e929b1ed4781c4e60d44/ortools/constraint_solver/routing.h#L416-L418

time_callback_indices = []
for vehicle_id in range(data['num_vehicles']):
time_callback = create_vehicle_travel_service_time_callback(data['travel_service'], data['service_time'], data['vehicle_speed_adjustment'][vehicle_id])
time_callback_indices.append(routing.RegisterTransitCallback(time_callback))
#print(time_callback_index)

routing.AddDimensionWithVehicleTransits(
time_callback_indices,
max_waiting_time, # allow waiting time
max_operation_time, # maximum time per vehicle
False,
'Time')

关于python - 不同车速的车辆路线(谷歌或工具),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63112611/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com