【发布时间】:2022-01-25 03:18:47
【问题描述】:
尝试按照 Scipy documentation 将 linear_sum_assignment 函数应用于我的代码,其中我试图为每个披萨分配 1 个机器人,以便机器人的总行程时间尽可能短。
Robots 是 6 个机器人对象的列表,我故意忽略其中的第一个机器人。 SwapTargets 是 5 个 Pizza 对象的列表
newlist = []
for j in range(1,len(Robots)):
for i in range(0,len(SwapTargets)):
ref_x = SwapTargets[i].coordinates[0]
ref_y = SwapTargets[i].coordinates[1]
value = ((ref_x - Robots[j].x)**2) + (ref_y - Robots[j].y)**2
newlist.append(value)
myarray = np.array(newlist).reshape(len(Robots[1:]),len(SwapTargets))
from scipy.optimize import linear_sum_assignment
row_ind, col_ind = linear_sum_assignment(myarray)
PizzaList = np.array([SwapTargets])[row_ind]
RobotList = np.array([Robots[1:]])[col_ind]
result = dict(zip(PizzaList, RobotList))
print(result)
PizzaList = np.array([SwapTargets])[row_ind]
我收到一个错误IndexError: index 1 is out of bounds for axis 0 with size 1
作为一个测试,如果我在PizzaList = np.array([SwapTargets])[row_ind] 中将[SwapTargets] 替换为["A,"B","C","D","E"],则不会出现错误,但不明白为什么我的5 个对象列表不起作用。
感谢python noobie
【问题讨论】: