32 #define USR_INF 999999999999 44 std::vector<std::pair<int, int>>* match_cps,
45 bool need_reverse =
false);
54 std::set<int> used_u_;
55 std::set<int> used_v_;
57 bool FindCP(
const T& mat,
int i);
61 std::vector<std::pair<int, int>>* match_cps,
64 u_size_ = association_mat.rows();
65 v_size_ = association_mat.cols();
66 if (u_size_ > v_size_)
return false;
67 ex_u_ =
new double[u_size_];
68 ex_v_ =
new double[v_size_];
69 v_matched_ =
new int[v_size_];
70 std::fill(v_matched_, v_matched_ + v_size_, -1);
71 memset(ex_v_, 0, v_size_ *
sizeof(
double));
72 for (
int i = 0; i < u_size_; ++i) {
73 ex_u_[i] = association_mat(i, 0);
74 for (
int j = 1; j < v_size_; ++j) {
75 ex_u_[i] = std::max(static_cast<float>(ex_u_[i]), association_mat(i, j));
78 for (
int i = 0; i < u_size_; ++i) {
81 match_cps->push_back(std::make_pair(-1, i));
83 match_cps->push_back(std::make_pair(i, -1));
86 v_slack_ =
new double[v_size_];
87 std::fill(v_slack_, v_slack_ + v_size_,
USR_INF);
91 if (FindCP(association_mat, i))
break;
93 for (
int j = 0; j < v_size_; ++j)
94 if (used_v_.find(j) == used_v_.end()) d = std::min(d, v_slack_[j]);
95 for (
auto it = used_u_.begin(); it != used_u_.end(); it++) {
98 for (
int j = 0; j < v_size_; ++j) {
99 if (used_v_.find(j) != used_v_.end())
108 for (
int j = 0; j < v_size_; ++j) {
109 if (v_matched_[j] == -1) {
110 match_cps->push_back(std::make_pair(j, -1));
111 }
else if (association_mat(v_matched_[j], j) > 0) {
112 match_cps->push_back(std::make_pair(j, v_matched_[j]));
114 match_cps->push_back(std::make_pair(-1, v_matched_[j]));
115 match_cps->push_back(std::make_pair(j, -1));
119 for (
int j = 0; j < v_size_; ++j) {
120 if (v_matched_[j] == -1) {
121 match_cps->push_back(std::make_pair(-1, j));
122 }
else if (association_mat(v_matched_[j], j) > 0) {
123 match_cps->push_back(std::make_pair(v_matched_[j], j));
125 match_cps->push_back(std::make_pair(v_matched_[j], -1));
126 match_cps->push_back(std::make_pair(-1, j));
136 template <
typename T>
137 bool KMkernal::FindCP(
const T& mat,
int i) {
139 for (
int j = 0; j < v_size_; ++j) {
140 if (used_v_.find(j) != used_v_.end()) {
143 double gap = ex_u_[i] + ex_v_[j] - mat(i, j);
147 bool match_success = v_matched_[j] == -1 || FindCP(mat, v_matched_[j]);
153 v_slack_[j] = std::min(v_slack_[j], gap);
#define USR_INF
Definition: km.h:32
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool GetKMResult(const T &association_mat, std::vector< std::pair< int, int >> *match_cps, bool need_reverse=false)
Definition: km.h:60