lap.lapjv函数cost_limit参数

该博客介绍了如何利用lapjv算法解决线性分配问题,特别是在目标跟踪场景下。通过设置cost_limit参数,可以控制代价矩阵中元素的最大值,避免错误匹配或漏掉匹配目标。实例展示了cost_limit对匹配结果的影响,帮助理解其在实际应用中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查了不少资料,最后还是看函数文档看懂了。

cost_limit 是代价矩阵中每个元素的上限值,超过cost_limit的元素就不参与分配了。

Docstring:
Solve linear assignment problem using Jonker-Volgenant algorithm.

cost: an N x N matrix containing the assignment costs. Entry cost[i, j] is
  the cost of assigning row i to column j.
extend_cost: whether or not extend a non-square matrix [default: False]
cost_limit: an upper limit for a cost of a single assignment
            [default: np.inf]
return_cost: whether or not to return the assignment cost

Returns (opt, x, y) where:
  opt: cost of the assignment, not returned if return_cost is False.
  x: a size-N array specifying to which column each row is assigned.
  y: a size-N array specifying to which row each column is assigned.

When extend_cost and/or cost_limit is set, all unmatched entries will be
marked by -1 in x/y.
Type:      builtin_function_or_method

用视频跟踪举个例子:

如图为kilman预测和检测框计算出来的代价矩阵,一般计算方式为1-iou。

然后把代价矩阵送入lapjv,以总体最小代价(分配后元素和最小)实现任务分配。

K1K2K3
D1

0.5

0.60.7
D20.40.70.5
from lap import lapjv
import numpy as np

a = np.array([[0.5,0.6,0.7],[0.4,0.7,0.5]])
 
c,x,y = lapjv(a,extend_cost=True,cost_limit=0.4)
 
print(c,x,y)

output:0.4 [-1  0] [ 1 -1 -1]

c为求得的最小代价之和,x和y为分配结果,一个是将y分配给x,一个是将x分配个y,是一样的; 以x为例,-1代表未被匹配。 所以[-1,0]代表D1没有匹配项,D2匹配到了K1; cost_limit为代价矩阵中每个元素可以取值的上限不能超过0.4。

cost_limit改为0.6看一下:

from lap import lapjv
import numpy as np

a = np.array([[0.5,0.6,0.7],[0.4,0.7,0.5]])
 
c,x,y = lapjv(a,extend_cost=True,cost_limit=0.6)
 
print(c,x,y)

output: 1.0 [0 2] [ 0 -1  1]

这回两个目标都匹配上了,x=[0,2]代表D1匹配了K1,D2匹配了K3。

在目标跟踪里, cost_limit设置过小,会漏掉能匹配上的目标,设置过大,会增加错误的匹配。

### lap.lapjv与匈牙利算法的关系 lap.lapjv 实现了一种称为 Jonker-Volgenant 的算法,这种算法用于求解线性分配问题。该类问题旨在通过最小化总成本来寻找两组元素之间的最优配对方案[^3]。 #### 匈牙利算法概述 匈牙利算法同样致力于解决类似的线性分配挑战,其核心在于构建一个表示不同实体间关系的成本矩阵,并基于此矩阵计算出使整体代价最低的最佳匹配路径。然而,在效率方面,Hungarian method 存在一定的局限性,尤其是在面对较大规模的数据集时性能表现不佳[^2]。 #### Jonker-Volgenant (LAPJV) 算法特点 相比之下,Jonker-Volgenant 方法不仅继承了匈牙利算法的核心理念——即确保每次迭代都能保持当前解决方案的有效性和可行性;更重要的是,它引入了一系列优化措施以加速收敛过程并减少不必要的计算开销。因此,在实际应用中往往能够提供更快速度下的相同质量的结果[^4]。 ```python import numpy as np from lap import lapjv cost_matrix = np.array([ [0.1, 0.6, 0.3], [0.2, 0.1, 0.6], [0.5, 0.2, 0.9] ]) total_cost, row_indicators, col_indicators = lapjv(cost_matrix) print(f"Total Cost: {total_cost}") print("Row Indicators:", row_indicators) print("Column Indicators:", col_indicators) ``` 这段代码展示了如何利用 `lap.lapjv` 函数处理给定的成本矩阵,从而获得最优分配方案及其对应的总费用。这里所使用的正是改进后的 Jonker-Volgenant 版本而非传统的 Hungarian Method。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值