33 size_t horizon,
double ts,
const Eigen::MatrixXd& ego,
34 const Eigen::MatrixXi& obstacles_edges_num,
const size_t obstacles_num,
35 const Eigen::MatrixXd& obstacles_A,
const Eigen::MatrixXd& obstacles_b,
36 const Eigen::MatrixXd& xWS,
40 obstacles_edges_num_(obstacles_edges_num),
41 obstacles_A_(obstacles_A),
42 obstacles_b_(obstacles_b),
44 ACHECK(horizon < std::numeric_limits<int>::max())
45 <<
"Invalid cast on horizon in open space planner";
46 horizon_ =
static_cast<int>(horizon);
47 ACHECK(obstacles_num < std::numeric_limits<int>::max())
48 <<
"Invalid cast on obstacles_num in open space planner";
49 obstacles_num_ =
static_cast<int>(obstacles_num);
50 w_ev_ = ego_(1, 0) + ego_(3, 0);
51 l_ev_ = ego_(0, 0) + ego_(2, 0);
52 g_ = {l_ev_ / 2, w_ev_ / 2, l_ev_ / 2, w_ev_ / 2};
53 offset_ = (ego_(0, 0) + ego_(2, 0)) / 2 - ego_(2, 0);
54 obstacles_edges_sum_ = obstacles_edges_num_.sum();
56 n_start_index_ = l_start_index_ + obstacles_edges_sum_ * (horizon_ + 1);
57 l_warm_up_ = Eigen::MatrixXd::Zero(obstacles_edges_sum_, horizon_ + 1);
58 n_warm_up_ = Eigen::MatrixXd::Zero(4 * obstacles_num_, horizon_ + 1);
59 min_safety_distance_ =
65 int& n,
int& m,
int& nnz_jac_g,
int& nnz_h_lag,
66 IndexStyleEnum& index_style) {
67 lambda_horizon_ = obstacles_edges_sum_ * (horizon_ + 1);
69 miu_horizon_ = obstacles_num_ * 4 * (horizon_ + 1);
71 dual_formulation_horizon_ = obstacles_num_ * (horizon_ + 1);
73 num_of_variables_ = lambda_horizon_ + miu_horizon_;
75 num_of_constraints_ = 3 * obstacles_num_ * (horizon_ + 1) + num_of_variables_;
78 n = num_of_variables_;
81 m = num_of_constraints_;
87 for (
int i = 0; i < horizon_ + 1; ++i) {
88 for (
int j = 0; j < obstacles_num_; ++j) {
89 int current_edges_num = obstacles_edges_num_(j, 0);
90 tmp += current_edges_num * 3 + 2 + 2 + 4;
93 nnz_jac_g = tmp + num_of_variables_;
95 ADEBUG <<
"nnz_jac_g : " << nnz_jac_g;
96 index_style = IndexStyleEnum::C_STYLE;
137 int variable_index = 0;
140 for (
int i = 0; i < horizon_ + 1; ++i) {
141 for (
int j = 0; j < obstacles_edges_sum_; ++j) {
142 x_l[variable_index] = -2e19;
143 x_u[variable_index] = 2e19;
147 ADEBUG <<
"variable_index after adding lagrange l : " << variable_index;
150 for (
int i = 0; i < horizon_ + 1; ++i) {
151 for (
int j = 0; j < 4 * obstacles_num_; ++j) {
152 x_l[variable_index] = -2e19;
153 x_u[variable_index] = 2e19;
157 ADEBUG <<
"variable_index after adding lagrange n : " << variable_index;
159 int constraint_index = 0;
160 for (
int i = 0; i < horizon_ + 1; ++i) {
161 for (
int j = 0; j < obstacles_num_; ++j) {
163 g_l[constraint_index] = 0.0;
164 g_u[constraint_index] = 0.0;
165 g_l[constraint_index + 1] = 0.0;
166 g_u[constraint_index + 1] = 0.0;
169 g_l[constraint_index + 2] = min_safety_distance_;
170 g_u[constraint_index + 2] = 2e19;
171 constraint_index += 3;
175 int l_index = l_start_index_;
176 int n_index = n_start_index_;
178 for (
int i = 0; i < lambda_horizon_; ++i) {
179 g_l[constraint_index] = 0.0;
180 g_u[constraint_index] = 2e19;
184 for (
int i = 0; i < miu_horizon_; ++i) {
185 g_l[constraint_index] = 0.0;
186 g_u[constraint_index] = 2e19;
191 ADEBUG <<
"constraint_index after adding obstacles constraints: "
207 std::fill(grad_f, grad_f + n, 0.0);
208 int l_index = l_start_index_;
209 for (
int i = 0; i < horizon_ + 1; ++i) {
210 int edges_counter = 0;
212 for (
int j = 0; j < obstacles_num_; ++j) {
213 int current_edges_num = obstacles_edges_num_(j, 0);
215 obstacles_A_.block(edges_counter, 0, current_edges_num, 2);
217 obstacles_b_.block(edges_counter, 0, current_edges_num, 1);
222 for (
int k = 0; k < current_edges_num; ++k) {
223 tmp1 += Aj(k, 0) * x[l_index + k];
224 tmp2 += Aj(k, 1) * x[l_index + k];
226 for (
int k = 0; k < current_edges_num; ++k) {
227 grad_f[l_index + k] += 2.0 * tmp1 * Aj(k, 0) + 2.0 * tmp2 * Aj(k, 1);
231 edges_counter += current_edges_num;
232 l_index += current_edges_num;
248 int nele_jac,
int* iRow,
253 if (values ==
nullptr) {
255 int constraint_index = 0;
260 int l_index = l_start_index_;
261 int n_index = n_start_index_;
262 for (
int i = 0; i < horizon_ + 1; ++i) {
263 for (
int j = 0; j < obstacles_num_; ++j) {
264 int current_edges_num = obstacles_edges_num_(j, 0);
268 for (
int k = 0; k < current_edges_num; ++k) {
269 iRow[nz_index] = constraint_index;
270 jCol[nz_index] = l_index + k;
275 iRow[nz_index] = constraint_index;
276 jCol[nz_index] = n_index;
279 iRow[nz_index] = constraint_index;
280 jCol[nz_index] = n_index + 2;
285 for (
int k = 0; k < current_edges_num; ++k) {
286 iRow[nz_index] = constraint_index + 1;
287 jCol[nz_index] = l_index + k;
292 iRow[nz_index] = constraint_index + 1;
293 jCol[nz_index] = n_index + 1;
296 iRow[nz_index] = constraint_index + 1;
297 jCol[nz_index] = n_index + 3;
302 for (
int k = 0; k < current_edges_num; ++k) {
303 iRow[nz_index] = constraint_index + 2;
304 jCol[nz_index] = l_index + k;
309 for (
int k = 0; k < 4; ++k) {
310 iRow[nz_index] = constraint_index + 2;
311 jCol[nz_index] = n_index + k;
316 l_index += current_edges_num;
318 constraint_index += 3;
322 l_index = l_start_index_;
323 n_index = n_start_index_;
324 for (
int i = 0; i < lambda_horizon_; ++i) {
325 iRow[nz_index] = constraint_index;
326 jCol[nz_index] = l_index;
331 for (
int i = 0; i < miu_horizon_; ++i) {
332 iRow[nz_index] = constraint_index;
333 jCol[nz_index] = n_index;
339 CHECK_EQ(constraint_index, m) <<
"No. of constraints wrong in eval_jac_g.";
341 ADEBUG <<
"nz_index here : " << nz_index <<
" nele_jac is : " << nele_jac;
343 std::fill(values, values + nele_jac, 0.0);
349 int l_index = l_start_index_;
350 int n_index = n_start_index_;
352 for (
int i = 0; i < horizon_ + 1; ++i) {
353 int edges_counter = 0;
354 for (
int j = 0; j < obstacles_num_; ++j) {
355 int current_edges_num = obstacles_edges_num_(j, 0);
357 obstacles_A_.block(edges_counter, 0, current_edges_num, 2);
359 obstacles_b_.block(edges_counter, 0, current_edges_num, 1);
363 for (
int k = 0; k < current_edges_num; ++k) {
365 tmp1 += Aj(k, 0) * x[l_index + k];
366 tmp2 += Aj(k, 1) * x[l_index + k];
371 for (
int k = 0; k < current_edges_num; ++k) {
372 values[nz_index] = std::cos(xWS_(2, i)) * Aj(k, 0) +
373 std::sin(xWS_(2, i)) * Aj(k, 1);
378 values[nz_index] = 1.0;
381 values[nz_index] = -1.0;
384 ADEBUG <<
"eval_jac_g, after adding part 2";
388 for (
int k = 0; k < current_edges_num; ++k) {
389 values[nz_index] = -std::sin(xWS_(2, i)) * Aj(k, 0) +
390 std::cos(xWS_(2, i)) * Aj(k, 1);
395 values[nz_index] = 1.0;
398 values[nz_index] = -1.0;
405 for (
int k = 0; k < 4; ++k) {
406 tmp3 += -g_[k] * x[n_index + k];
409 for (
int k = 0; k < current_edges_num; ++k) {
410 tmp4 += bj(k, 0) * x[l_index + k];
414 for (
int k = 0; k < current_edges_num; ++k) {
416 (xWS_(0, i) + std::cos(xWS_(2, i)) * offset_) * Aj(k, 0) +
417 (xWS_(1, i) + std::sin(xWS_(2, i)) * offset_) * Aj(k, 1) -
423 for (
int k = 0; k < 4; ++k) {
424 values[nz_index] = -g_[k];
429 edges_counter += current_edges_num;
430 l_index += current_edges_num;
435 for (
int i = 0; i < lambda_horizon_; ++i) {
436 values[nz_index] = 1.0;
439 for (
int i = 0; i < miu_horizon_; ++i) {
440 values[nz_index] = 1.0;
444 ADEBUG <<
"eval_jac_g, fulfilled obstacle constraint values";
445 CHECK_EQ(nz_index, nele_jac);
448 ADEBUG <<
"eval_jac_g done";
521 const Eigen::MatrixXd& l_warm_up,
const Eigen::MatrixXd& n_warm_up) {
523 int kNumVariables = num_of_variables_;
524 double x[kNumVariables];
525 int variable_index = 0;
528 for (
int i = 0; i < horizon_ + 1; ++i) {
529 for (
int j = 0; j < obstacles_edges_sum_; ++j) {
530 x[variable_index] = l_warm_up(j, i);
536 for (
int i = 0; i < horizon_ + 1; ++i) {
537 for (
int j = 0; j < 4 * obstacles_num_; ++j) {
538 x[variable_index] = n_warm_up(j, i);
544 int kNumConstraint = num_of_constraints_;
545 double g[kNumConstraint];
546 eval_g(num_of_variables_, x,
true, num_of_constraints_, g);
549 double x_l[kNumVariables];
550 double x_u[kNumVariables];
551 double g_l[kNumConstraint];
552 double g_u[kNumConstraint];
553 get_bounds_info(num_of_variables_, x_l, x_u, num_of_constraints_, g_l, g_u);
556 int constraint_index = 0;
558 for (
int i = 0; i < horizon_ + 1; ++i) {
559 for (
int j = 0; j < obstacles_num_; ++j) {
561 if (g_l[constraint_index + 0] > g[constraint_index + 0] ||
562 g_u[constraint_index + 0] < g[constraint_index + 0]) {
563 AERROR <<
"G' * mu + R' * A * lambda == 0 constraint fails, "
564 <<
"constraint_index: " << constraint_index + 0
565 <<
", g: " << g[constraint_index + 0];
568 if (g_l[constraint_index + 1] > g[constraint_index + 1] ||
569 g_u[constraint_index + 1] < g[constraint_index + 1]) {
570 AERROR <<
"G' * mu + R' * A * lambda == 0 constraint fails, "
571 <<
"constraint_index: " << constraint_index + 1
572 <<
", g: " << g[constraint_index + 1];
576 if (g_l[constraint_index + 2] > g[constraint_index + 2] ||
577 g_u[constraint_index + 2] < g[constraint_index + 2]) {
578 AERROR <<
"-g' * mu + (A * t - b) * lambda) >= d_min constraint fails, "
579 <<
"constraint_index: " << constraint_index + 2
580 <<
", g: " << g[constraint_index + 2];
584 constraint_index += 3;
588 for (
int i = 0; i < lambda_horizon_; ++i) {
589 if (g_l[constraint_index] > g[constraint_index] ||
590 g_u[constraint_index] < g[constraint_index]) {
591 AERROR <<
"lambda box constraint fails, "
592 <<
"constraint_index: " << constraint_index
593 <<
", g: " << g[constraint_index];
599 for (
int i = 0; i < miu_horizon_; ++i) {
600 if (g_l[constraint_index] > g[constraint_index] ||
601 g_u[constraint_index] < g[constraint_index]) {
602 AERROR <<
"miu box constraint fails, "
603 <<
"constraint_index: " << constraint_index
604 <<
", g: " << g[constraint_index];
617 int l_index = l_start_index_;
618 for (
int i = 0; i < horizon_ + 1; ++i) {
619 int edges_counter = 0;
621 for (
int j = 0; j < obstacles_num_; ++j) {
622 int current_edges_num = obstacles_edges_num_(j, 0);
624 obstacles_A_.block(edges_counter, 0, current_edges_num, 2);
626 obstacles_b_.block(edges_counter, 0, current_edges_num, 1);
631 for (
int k = 0; k < current_edges_num; ++k) {
632 tmp1 += Aj(k, 0) * x[l_index + k];
633 tmp2 += Aj(k, 1) * x[l_index + k];
635 *obj_value += tmp1 * tmp1 + tmp2 * tmp2;
638 edges_counter += current_edges_num;
639 l_index += current_edges_num;
650 ADEBUG <<
"eval_constraints";
656 int l_index = l_start_index_;
657 int n_index = n_start_index_;
658 int constraint_index = 0;
660 for (
int i = 0; i < horizon_ + 1; ++i) {
661 int edges_counter = 0;
663 for (
int j = 0; j < obstacles_num_; ++j) {
664 int current_edges_num = obstacles_edges_num_(j, 0);
666 obstacles_A_.block(edges_counter, 0, current_edges_num, 2);
668 obstacles_b_.block(edges_counter, 0, current_edges_num, 1);
673 for (
int k = 0; k < current_edges_num; ++k) {
674 tmp1 += Aj(k, 0) * x[l_index + k];
675 tmp2 += Aj(k, 1) * x[l_index + k];
679 g[constraint_index + 0] = x[n_index] - x[n_index + 2] +
680 cos(xWS_(2, i)) * tmp1 + sin(xWS_(2, i)) * tmp2;
682 g[constraint_index + 1] = x[n_index + 1] - x[n_index + 3] -
683 sin(xWS_(2, i)) * tmp1 + cos(xWS_(2, i)) * tmp2;
687 for (
int k = 0; k < 4; ++k) {
688 tmp3 += g_[k] * x[n_index + k];
692 for (
int k = 0; k < current_edges_num; ++k) {
693 tmp4 += bj(k, 0) * x[l_index + k];
696 g[constraint_index + 2] =
697 -tmp3 + (xWS_(0, i) + cos(xWS_(2, i)) * offset_) * tmp1 +
698 (xWS_(1, i) + sin(xWS_(2, i)) * offset_) * tmp2 - tmp4;
701 edges_counter += current_edges_num;
702 l_index += current_edges_num;
704 constraint_index += 3;
707 l_index = l_start_index_;
708 n_index = n_start_index_;
709 for (
int i = 0; i < lambda_horizon_; ++i) {
710 g[constraint_index] = x[l_index];
714 for (
int i = 0; i < miu_horizon_; ++i) {
715 g[constraint_index] = x[n_index];
720 CHECK_EQ(constraint_index, m)
721 <<
"No. of constraints wrong in eval_g. n : " << n;
728 std::vector<double> xp(n);
729 std::vector<double> lamp(m);
730 std::vector<double> zl(m);
731 std::vector<double> zu(m);
733 std::vector<adouble> xa(n);
734 std::vector<adouble> g(m);
735 std::vector<double> lam(m);
742 obj_lam =
new double[m + 1];
768 for (
int idx = 0; idx < n; idx++) {
771 for (
int idx = 0; idx < m; idx++) {
778 obj_value *= mkparam(sig);
781 for (
int idx = 0; idx < m; idx++) {
782 obj_value += g[idx] * mkparam(lam[idx]);
797 sparse_hess(
tag_L, n, 0, &xp[0], &nnz_L, &rind_L, &cind_L, &hessval,