您的位置:首页 > 教育 > 锐评 > 多目标跟踪中用到的求解线性分配问题(Linear Assignment Problem,LAP)C++

多目标跟踪中用到的求解线性分配问题(Linear Assignment Problem,LAP)C++

2024/10/7 6:42:34 来源:https://blog.csdn.net/flyfish1986/article/details/139755009  浏览:    关键词:多目标跟踪中用到的求解线性分配问题(Linear Assignment Problem,LAP)C++

多目标跟踪中用到的求解线性分配问题(Linear Assignment Problem,LAP)C++

flyfish

python实现,说的比这里详细

lapjv.h和lapjv.cpp代码在https://github.com/shaoshengsong/DeepSORT

C++代码调用

#include <iostream>
#include <vector>
#include "lapjv.h"// 打印分配结果
void print_assignment(const std::vector<std::vector<int>>& cost_matrix, const std::vector<int>& assignment) {int n = cost_matrix.size();std::cout << "Custom LAPJV Assignment:" << std::endl;for (int i = 0; i < n; ++i) {std::cout << "Worker " << i + 1 << " is assigned to Job " << assignment[i] + 1 << std::endl;}
}int main() {// 成本矩阵std::vector<std::vector<int>> cost_matrix = {{9, 11, 14, 11, 7},{6, 15, 13, 13, 10},{12, 13, 6, 8, 8},{11, 9, 10, 12, 9},{7, 12, 14, 10, 14}};// 将成本矩阵转换为cost_t类型的矩阵int n = cost_matrix.size();std::vector<std::vector<cost_t>> cost_matrix_cost_t(n, std::vector<cost_t>(n));for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {cost_matrix_cost_t[i][j] = static_cast<cost_t>(cost_matrix[i][j]);}}// 将成本矩阵转化为指针数组以适应lapjv函数std::vector<cost_t*> cost_matrix_ptrs(n);for (int i = 0; i < n; ++i) {cost_matrix_ptrs[i] = cost_matrix_cost_t[i].data();}// 调用lapjv_internal函数解决线性分配问题std::vector<int_t> x(n), y(n);lapjv_internal(n, cost_matrix_ptrs.data(), x.data(), y.data());// 打印分配结果print_assignment(cost_matrix, x);return 0;
}
Custom LAPJV Assignment:
Worker 1 is assigned to Job 5
Worker 2 is assigned to Job 1
Worker 3 is assigned to Job 3
Worker 4 is assigned to Job 2
Worker 5 is assigned to Job 4

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com