Apollo 10.0
自动驾驶开放平台
apollo::planning::DistanceApproachIPOPTFixedDualInterface类 参考

#include <distance_approach_ipopt_fixed_dual_interface.h>

类 apollo::planning::DistanceApproachIPOPTFixedDualInterface 继承关系图:
apollo::planning::DistanceApproachIPOPTFixedDualInterface 的协作图:

Public 成员函数

 DistanceApproachIPOPTFixedDualInterface (const size_t horizon, const double ts, const Eigen::MatrixXd &ego, const Eigen::MatrixXd &xWS, const Eigen::MatrixXd &uWS, const Eigen::MatrixXd &l_warm_up, const Eigen::MatrixXd &n_warm_up, const Eigen::MatrixXd &x0, const Eigen::MatrixXd &xf, const Eigen::MatrixXd &last_time_u, const std::vector< double > &XYbounds, const Eigen::MatrixXi &obstacles_edges_num, const size_t obstacles_num, const Eigen::MatrixXd &obstacles_A, const Eigen::MatrixXd &obstacles_b, const PlannerOpenSpaceConfig &planner_open_space_config)
 
virtual ~DistanceApproachIPOPTFixedDualInterface ()=default
 
bool get_nlp_info (int &n, int &m, int &nnz_jac_g, int &nnz_h_lag, IndexStyleEnum &index_style) override
 Method to return some info about the nlp
 
bool get_bounds_info (int n, double *x_l, double *x_u, int m, double *g_l, double *g_u) override
 Method to return the bounds for my problem
 
bool get_starting_point (int n, bool init_x, double *x, bool init_z, double *z_L, double *z_U, int m, bool init_lambda, double *lambda) override
 Method to return the starting point for the algorithm
 
bool eval_f (int n, const double *x, bool new_x, double &obj_value) override
 Method to return the objective value
 
bool eval_grad_f (int n, const double *x, bool new_x, double *grad_f) override
 Method to return the gradient of the objective
 
bool eval_g (int n, const double *x, bool new_x, int m, double *g) override
 Method to return the constraint residuals
 
bool check_g (int n, const double *x, int m, const double *g)
 Check unfeasible constraints for further study
 
bool eval_jac_g (int n, const double *x, bool new_x, int m, int nele_jac, int *iRow, int *jCol, double *values) override
 Method to return: 1) The structure of the jacobian (if "values" is nullptr) 2) The values of the jacobian (if "values" is not nullptr)
 
bool eval_jac_g_ser (int n, const double *x, bool new_x, int m, int nele_jac, int *iRow, int *jCol, double *values) override
 
bool eval_h (int n, const double *x, bool new_x, double obj_factor, int m, const double *lambda, bool new_lambda, int nele_hess, int *iRow, int *jCol, double *values) override
 Method to return: 1) The structure of the hessian of the lagrangian (if "values" is nullptr) 2) The values of the hessian of the lagrangian (if "values" is not nullptr)
 
- Public 成员函数 继承自 apollo::planning::DistanceApproachInterface
virtual ~DistanceApproachInterface ()=default
 

Solution Methods

void finalize_solution (Ipopt::SolverReturn status, int n, const double *x, const double *z_L, const double *z_U, int m, const double *g, const double *lambda, double obj_value, const Ipopt::IpoptData *ip_data, Ipopt::IpoptCalculatedQuantities *ip_cq) override
 This method is called when the algorithm is complete so the TNLP can store/write the solution
 
void get_optimization_results (Eigen::MatrixXd *state_result, Eigen::MatrixXd *control_result, Eigen::MatrixXd *time_result, Eigen::MatrixXd *dual_l_result, Eigen::MatrixXd *dual_n_result) const override
 
template<class T >
void eval_obj (int n, const T *x, T *obj_value)
 Template to return the objective value
 
template<class T >
void eval_constraints (int n, const T *x, int m, T *g)
 Template to compute constraints
 
void generate_tapes (int n, int m, int *nnz_jac_g, int *nnz_h_lag)
 Method to generate the required tapes by ADOL-C
 
 DistanceApproachIPOPTFixedDualInterface (const size_t horizon, const double ts, const Eigen::MatrixXd &ego, const Eigen::MatrixXd &xWS, const Eigen::MatrixXd &uWS, const Eigen::MatrixXd &l_warm_up, const Eigen::MatrixXd &n_warm_up, const Eigen::MatrixXd &x0, const Eigen::MatrixXd &xf, const Eigen::MatrixXd &last_time_u, const std::vector< double > &XYbounds, const Eigen::MatrixXi &obstacles_edges_num, const size_t obstacles_num, const Eigen::MatrixXd &obstacles_A, const Eigen::MatrixXd &obstacles_b, const DistanceApproachConfig &distance_approach_config)
 

详细描述

构造及析构函数说明

◆ DistanceApproachIPOPTFixedDualInterface() [1/2]

apollo::planning::DistanceApproachIPOPTFixedDualInterface::DistanceApproachIPOPTFixedDualInterface ( const size_t  horizon,
const double  ts,
const Eigen::MatrixXd &  ego,
const Eigen::MatrixXd &  xWS,
const Eigen::MatrixXd &  uWS,
const Eigen::MatrixXd &  l_warm_up,
const Eigen::MatrixXd &  n_warm_up,
const Eigen::MatrixXd &  x0,
const Eigen::MatrixXd &  xf,
const Eigen::MatrixXd &  last_time_u,
const std::vector< double > &  XYbounds,
const Eigen::MatrixXi &  obstacles_edges_num,
const size_t  obstacles_num,
const Eigen::MatrixXd &  obstacles_A,
const Eigen::MatrixXd &  obstacles_b,
const PlannerOpenSpaceConfig &  planner_open_space_config 
)

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 24 行定义.

34 : ts_(ts),
35 ego_(ego),
36 xWS_(xWS),
37 uWS_(uWS),
38 l_warm_up_(l_warm_up),
39 n_warm_up_(n_warm_up),
40 x0_(x0),
41 xf_(xf),
42 last_time_u_(last_time_u),
43 XYbounds_(XYbounds),
44 obstacles_edges_num_(obstacles_edges_num),
45 obstacles_A_(obstacles_A),
46 obstacles_b_(obstacles_b) {
47 ACHECK(horizon < std::numeric_limits<int>::max())
48 << "Invalid cast on horizon in open space planner";
49 horizon_ = static_cast<int>(horizon);
50 ADEBUG << "horizon length: " << horizon_;
51 ACHECK(obstacles_num < std::numeric_limits<int>::max())
52 << "Invalid cast on obstacles_num in open space planner";
53
54 obstacles_num_ = static_cast<int>(obstacles_num);
55 w_ev_ = ego_(1, 0) + ego_(3, 0);
56 l_ev_ = ego_(0, 0) + ego_(2, 0);
57 g_ = {l_ev_ / 2, w_ev_ / 2, l_ev_ / 2, w_ev_ / 2};
58 offset_ = (ego_(0, 0) + ego_(2, 0)) / 2 - ego_(2, 0);
59 obstacles_edges_sum_ = obstacles_edges_num_.sum();
60 state_result_ = Eigen::MatrixXd::Zero(4, horizon_ + 1);
61 dual_l_result_ = Eigen::MatrixXd::Zero(obstacles_edges_sum_, horizon_ + 1);
62 dual_n_result_ = Eigen::MatrixXd::Zero(4 * obstacles_num_, horizon_ + 1);
63 control_result_ = Eigen::MatrixXd::Zero(2, horizon_);
64 time_result_ = Eigen::MatrixXd::Zero(1, horizon_);
65
66 state_start_index_ = 0;
67 control_start_index_ = 4 * (horizon_ + 1);
68 time_start_index_ = control_start_index_ + 2 * horizon_;
69 l_start_index_ = time_start_index_ + (horizon_ + 1);
70 n_start_index_ = l_start_index_ + obstacles_edges_sum_ * (horizon_ + 1);
71
72 distance_approach_config_ =
73 planner_open_space_config.distance_approach_config();
74 weight_state_x_ = distance_approach_config_.weight_x();
75 weight_state_y_ = distance_approach_config_.weight_y();
76 weight_state_phi_ = distance_approach_config_.weight_phi();
77 weight_state_v_ = distance_approach_config_.weight_v();
78 weight_input_steer_ = distance_approach_config_.weight_steer();
79 weight_input_a_ = distance_approach_config_.weight_a();
80 weight_rate_steer_ = distance_approach_config_.weight_steer_rate();
81 weight_rate_a_ = distance_approach_config_.weight_a_rate();
82 weight_stitching_steer_ = distance_approach_config_.weight_steer_stitching();
83 weight_stitching_a_ = distance_approach_config_.weight_a_stitching();
84 weight_first_order_time_ =
85 distance_approach_config_.weight_first_order_time();
86 weight_second_order_time_ =
87 distance_approach_config_.weight_second_order_time();
88 min_safety_distance_ = distance_approach_config_.min_safety_distance();
89 max_steer_angle_ =
90 vehicle_param_.max_steer_angle() / vehicle_param_.steer_ratio();
91 max_speed_forward_ = distance_approach_config_.max_speed_forward();
92 max_speed_reverse_ = distance_approach_config_.max_speed_reverse();
93 max_acceleration_forward_ =
94 distance_approach_config_.max_acceleration_forward();
95 max_acceleration_reverse_ =
96 distance_approach_config_.max_acceleration_reverse();
97 min_time_sample_scaling_ =
98 distance_approach_config_.min_time_sample_scaling();
99 max_time_sample_scaling_ =
100 distance_approach_config_.max_time_sample_scaling();
101 max_steer_rate_ =
102 vehicle_param_.max_steer_angle_rate() / vehicle_param_.steer_ratio();
103 use_fix_time_ = distance_approach_config_.use_fix_time();
104 wheelbase_ = vehicle_param_.wheel_base();
105 enable_constraint_check_ =
106 distance_approach_config_.enable_constraint_check();
107 enable_jacobian_ad_ = distance_approach_config_.enable_jacobian_ad();
108}
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41

◆ ~DistanceApproachIPOPTFixedDualInterface()

virtual apollo::planning::DistanceApproachIPOPTFixedDualInterface::~DistanceApproachIPOPTFixedDualInterface ( )
virtualdefault

◆ DistanceApproachIPOPTFixedDualInterface() [2/2]

apollo::planning::DistanceApproachIPOPTFixedDualInterface::DistanceApproachIPOPTFixedDualInterface ( const size_t  horizon,
const double  ts,
const Eigen::MatrixXd &  ego,
const Eigen::MatrixXd &  xWS,
const Eigen::MatrixXd &  uWS,
const Eigen::MatrixXd &  l_warm_up,
const Eigen::MatrixXd &  n_warm_up,
const Eigen::MatrixXd &  x0,
const Eigen::MatrixXd &  xf,
const Eigen::MatrixXd &  last_time_u,
const std::vector< double > &  XYbounds,
const Eigen::MatrixXi &  obstacles_edges_num,
const size_t  obstacles_num,
const Eigen::MatrixXd &  obstacles_A,
const Eigen::MatrixXd &  obstacles_b,
const DistanceApproachConfig &  distance_approach_config 
)

成员函数说明

◆ check_g()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::check_g ( int  n,
const double *  x,
int  m,
const double *  g 
)
virtual

Check unfeasible constraints for further study

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 799 行定义.

800 {
801 int kN = n;
802 int kM = m;
803 double x_u_tmp[kN];
804 double x_l_tmp[kN];
805 double g_u_tmp[kM];
806 double g_l_tmp[kM];
807
808 get_bounds_info(n, x_l_tmp, x_u_tmp, m, g_l_tmp, g_u_tmp);
809
810 const double delta_v = 1e-4;
811 for (int idx = 0; idx < n; ++idx) {
812 x_u_tmp[idx] = x_u_tmp[idx] + delta_v;
813 x_l_tmp[idx] = x_l_tmp[idx] - delta_v;
814 if (x[idx] > x_u_tmp[idx] || x[idx] < x_l_tmp[idx]) {
815 AINFO << "x idx unfeasible: " << idx << ", x: " << x[idx]
816 << ", lower: " << x_l_tmp[idx] << ", upper: " << x_u_tmp[idx];
817 }
818 }
819
820 // m1 : dynamics constatins
821 int m1 = 4 * horizon_;
822
823 // m2 : control rate constraints (only steering)
824 int m2 = m1 + horizon_;
825
826 // m3 : sampling time equality constraints
827 int m3 = m2 + horizon_;
828
829 // m4 : obstacle constraints
830 // int m4 = m3 + 4 * obstacles_num_ * (horizon_ + 1);
831
832 // 5. load variable bounds as constraints
833 // start configuration
834 int m5 = m3 + 3 + 1;
835
836 // constraints on x,y,v
837 int m6 = m5 + 3 * (horizon_ - 1);
838
839 // end configuration
840 int m7 = m6 + 3 + 1;
841
842 // control variable bnd
843 int m8 = m7 + 2 * horizon_;
844
845 // time interval variable bnd
846 int m9 = m8 + (horizon_ + 1);
847
848 // lambda_horizon_
849 // int m10 = m9 + lambda_horizon_;
850
851 // miu_horizon_
852 // int m11 = m10 + miu_horizon_;
853
854 CHECK_EQ(m9, num_of_constraints_);
855
856 AINFO << "dynamics constatins to: " << m1;
857 AINFO << "control rate constraints (only steering) to: " << m2;
858 AINFO << "sampling time equality constraints to: " << m3;
859 AINFO << "start conf constraints to: " << m5;
860 AINFO << "constraints on x,y,v to: " << m6;
861 AINFO << "end constraints to: " << m7;
862 AINFO << "control bnd to: " << m8;
863 AINFO << "time interval constraints to: " << m9;
864 AINFO << "total constraints: " << num_of_constraints_;
865
866 for (int idx = 0; idx < m; ++idx) {
867 if (g[idx] > g_u_tmp[idx] + delta_v || g[idx] < g_l_tmp[idx] - delta_v) {
868 AINFO << "constratins idx unfeasible: " << idx << ", g: " << g[idx]
869 << ", lower: " << g_l_tmp[idx] << ", upper: " << g_u_tmp[idx];
870 }
871 }
872 return true;
873}
bool get_bounds_info(int n, double *x_l, double *x_u, int m, double *g_l, double *g_u) override
Method to return the bounds for my problem
#define AINFO
Definition log.h:42

◆ eval_constraints()

template<class T >
void apollo::planning::DistanceApproachIPOPTFixedDualInterface::eval_constraints ( int  n,
const T *  x,
int  m,
T *  g 
)

Template to compute constraints

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 654 行定义.

656 {
657 // state start index
658 int state_index = state_start_index_;
659
660 // control start index.
661 int control_index = control_start_index_;
662
663 // time start index
664 int time_index = time_start_index_;
665
666 int constraint_index = 0;
667
668 // 1. state constraints 4 * [0, horizons-1]
669 for (int i = 0; i < horizon_; ++i) {
670 // x1
671 g[constraint_index] =
672 x[state_index + 4] -
673 (x[state_index] +
674 ts_ * x[time_index] *
675 (x[state_index + 3] +
676 ts_ * x[time_index] * 0.5 * x[control_index + 1]) *
677 cos(x[state_index + 2] + ts_ * x[time_index] * 0.5 *
678 x[state_index + 3] *
679 tan(x[control_index]) / wheelbase_));
680
681 // x2
682 g[constraint_index + 1] =
683 x[state_index + 5] -
684 (x[state_index + 1] +
685 ts_ * x[time_index] *
686 (x[state_index + 3] +
687 ts_ * x[time_index] * 0.5 * x[control_index + 1]) *
688 sin(x[state_index + 2] + ts_ * x[time_index] * 0.5 *
689 x[state_index + 3] *
690 tan(x[control_index]) / wheelbase_));
691
692 // x3
693 g[constraint_index + 2] =
694 x[state_index + 6] -
695 (x[state_index + 2] +
696 ts_ * x[time_index] *
697 (x[state_index + 3] +
698 ts_ * x[time_index] * 0.5 * x[control_index + 1]) *
699 tan(x[control_index]) / wheelbase_);
700
701 // x4
702 g[constraint_index + 3] =
703 x[state_index + 7] -
704 (x[state_index + 3] + ts_ * x[time_index] * x[control_index + 1]);
705
706 control_index += 2;
707 constraint_index += 4;
708 time_index++;
709 state_index += 4;
710 }
711
712 ADEBUG << "constraint_index after adding Euler forward dynamics constraints "
713 "updated: "
714 << constraint_index;
715
716 // 2. Control rate limit constraints, 1 * [0, horizons-1], only apply
717 // steering rate as of now
718 control_index = control_start_index_;
719 time_index = time_start_index_;
720
721 // First rate is compare first with stitch point
722 g[constraint_index] =
723 (x[control_index] - last_time_u_(0, 0)) / x[time_index] / ts_;
724 control_index += 2;
725 constraint_index++;
726 time_index++;
727
728 for (int i = 1; i < horizon_; ++i) {
729 g[constraint_index] =
730 (x[control_index] - x[control_index - 2]) / x[time_index] / ts_;
731 constraint_index++;
732 control_index += 2;
733 time_index++;
734 }
735
736 // 3. Time constraints 1 * [0, horizons-1]
737 time_index = time_start_index_;
738 for (int i = 0; i < horizon_; ++i) {
739 g[constraint_index] = x[time_index + 1] - x[time_index];
740 constraint_index++;
741 time_index++;
742 }
743
744 ADEBUG << "constraint_index after adding time constraints "
745 "updated: "
746 << constraint_index;
747
748 // 4. Three obstacles related equal constraints, one equality constraints,
749 // [0, horizon_] * [0, obstacles_num_-1] * 4
750 state_index = state_start_index_;
751
752 // 5. load variable bounds as constraints
753 state_index = state_start_index_;
754 control_index = control_start_index_;
755 time_index = time_start_index_;
756
757 // start configuration
758 g[constraint_index] = x[state_index];
759 g[constraint_index + 1] = x[state_index + 1];
760 g[constraint_index + 2] = x[state_index + 2];
761 g[constraint_index + 3] = x[state_index + 3];
762 constraint_index += 4;
763 state_index += 4;
764
765 // constraints on x,y,v
766 for (int i = 1; i < horizon_; ++i) {
767 g[constraint_index] = x[state_index];
768 g[constraint_index + 1] = x[state_index + 1];
769 g[constraint_index + 2] = x[state_index + 3];
770 constraint_index += 3;
771 state_index += 4;
772 }
773
774 // end configuration
775 g[constraint_index] = x[state_index];
776 g[constraint_index + 1] = x[state_index + 1];
777 g[constraint_index + 2] = x[state_index + 2];
778 g[constraint_index + 3] = x[state_index + 3];
779 constraint_index += 4;
780 state_index += 4;
781
782 for (int i = 0; i < horizon_; ++i) {
783 g[constraint_index] = x[control_index];
784 g[constraint_index + 1] = x[control_index + 1];
785 constraint_index += 2;
786 control_index += 2;
787 }
788
789 for (int i = 0; i < horizon_ + 1; ++i) {
790 g[constraint_index] = x[time_index];
791 constraint_index++;
792 time_index++;
793 }
794
795 ADEBUG << "constraint_index after adding variable box constraints updated: "
796 << constraint_index;
797}
float cos(Angle16 a)
Definition angle.cc:42
float sin(Angle16 a)
Definition angle.cc:25
float tan(Angle16 a)
Definition angle.cc:47

◆ eval_f()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::eval_f ( int  n,
const double *  x,
bool  new_x,
double &  obj_value 
)
overridevirtual

Method to return the objective value

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 360 行定义.

362 {
363 eval_obj(n, x, &obj_value);
364 return true;
365}
void eval_obj(int n, const T *x, T *obj_value)
Template to return the objective value

◆ eval_g()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::eval_g ( int  n,
const double *  x,
bool  new_x,
int  m,
double *  g 
)
overridevirtual

Method to return the constraint residuals

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 375 行定义.

377 {
378 eval_constraints(n, x, m, g);
379 // if (enable_constraint_check_) check_g(n, x, m, g);
380 return true;
381}
void eval_constraints(int n, const T *x, int m, T *g)
Template to compute constraints

◆ eval_grad_f()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::eval_grad_f ( int  n,
const double *  x,
bool  new_x,
double *  grad_f 
)
overridevirtual

Method to return the gradient of the objective

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 367 行定义.

370 {
371 gradient(tag_f, n, x, grad_f);
372 return true;
373}
#define tag_f

◆ eval_h()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::eval_h ( int  n,
const double *  x,
bool  new_x,
double  obj_factor,
int  m,
const double *  lambda,
bool  new_lambda,
int  nele_hess,
int *  iRow,
int *  jCol,
double *  values 
)
overridevirtual

Method to return: 1) The structure of the hessian of the lagrangian (if "values" is nullptr) 2) The values of the hessian of the lagrangian (if "values" is not nullptr)

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 413 行定义.

416 {
417 if (values == nullptr) {
418 // return the structure. This is a symmetric matrix, fill the lower left
419 // triangle only.
420
421 for (int idx = 0; idx < nnz_L; idx++) {
422 iRow[idx] = rind_L[idx];
423 jCol[idx] = cind_L[idx];
424 }
425 } else {
426 // return the values. This is a symmetric matrix, fill the lower left
427 // triangle only
428
429 obj_lam[0] = obj_factor;
430
431 for (int idx = 0; idx < m; idx++) {
432 obj_lam[1 + idx] = lambda[idx];
433 }
434
435 set_param_vec(tag_L, m + 1, obj_lam);
436 sparse_hess(tag_L, n, 1, const_cast<double*>(x), &nnz_L, &rind_L, &cind_L,
437 &hessval, options_L);
438
439 for (int idx = 0; idx < nnz_L; idx++) {
440 values[idx] = hessval[idx];
441 }
442 }
443
444 return true;
445}
#define tag_L

◆ eval_jac_g()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::eval_jac_g ( int  n,
const double *  x,
bool  new_x,
int  m,
int  nele_jac,
int *  iRow,
int *  jCol,
double *  values 
)
overridevirtual

Method to return: 1) The structure of the jacobian (if "values" is nullptr) 2) The values of the jacobian (if "values" is not nullptr)

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 383 行定义.

387 {
388 if (values == nullptr) {
389 ADEBUG << "nnz_jac: " << nnz_jac;
390 // return the structure of the jacobian
391 for (int idx = 0; idx < nnz_jac; idx++) {
392 iRow[idx] = rind_g[idx];
393 jCol[idx] = cind_g[idx];
394 }
395 } else {
396 // return the values of the jacobian of the constraints
397 sparse_jac(tag_g, m, n, 1, x, &nnz_jac, &rind_g, &cind_g, &jacval,
398 options_g);
399 for (int idx = 0; idx < nnz_jac; idx++) {
400 values[idx] = jacval[idx];
401 }
402 }
403 return true;
404}
#define tag_g

◆ eval_jac_g_ser()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::eval_jac_g_ser ( int  n,
const double *  x,
bool  new_x,
int  m,
int  nele_jac,
int *  iRow,
int *  jCol,
double *  values 
)
overridevirtual

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 406 行定义.

408 {
409 AERROR << "not ready ATM!";
410 return false;
411}
#define AERROR
Definition log.h:44

◆ eval_obj()

template<class T >
void apollo::planning::DistanceApproachIPOPTFixedDualInterface::eval_obj ( int  n,
const T *  x,
T *  obj_value 
)

Template to return the objective value

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 583 行定义.

584 {
585 // Objective is :
586 // min control inputs
587 // min input rate
588 // min time (if the time step is not fixed)
589 // regularization wrt warm start trajectory
590 DCHECK(ts_ != 0) << "ts in distance_approach_ is 0";
591 int control_index = control_start_index_;
592 int time_index = time_start_index_;
593 int state_index = state_start_index_;
594
595 // TODO(QiL): Initial implementation towards earlier understanding and debug
596 // purpose, later code refine towards improving efficiency
597
598 *obj_value = 0.0;
599 // 1. objective to minimize state diff to warm up
600 for (int i = 0; i < horizon_ + 1; ++i) {
601 T x1_diff = x[state_index] - xWS_(0, i);
602 T x2_diff = x[state_index + 1] - xWS_(1, i);
603 T x3_diff = x[state_index + 2] - xWS_(2, i);
604 T x4_abs = x[state_index + 3];
605 *obj_value += weight_state_x_ * x1_diff * x1_diff +
606 weight_state_y_ * x2_diff * x2_diff +
607 weight_state_phi_ * x3_diff * x3_diff +
608 weight_state_v_ * x4_abs * x4_abs;
609 state_index += 4;
610 }
611
612 // 2. objective to minimize u square
613 for (int i = 0; i < horizon_; ++i) {
614 *obj_value += weight_input_steer_ * x[control_index] * x[control_index] +
615 weight_input_a_ * x[control_index + 1] * x[control_index + 1];
616 control_index += 2;
617 }
618
619 // 3. objective to minimize input change rate for first horizon
620 control_index = control_start_index_;
621 T last_time_steer_rate =
622 (x[control_index] - last_time_u_(0, 0)) / x[time_index] / ts_;
623 T last_time_a_rate =
624 (x[control_index + 1] - last_time_u_(1, 0)) / x[time_index] / ts_;
625 *obj_value +=
626 weight_stitching_steer_ * last_time_steer_rate * last_time_steer_rate +
627 weight_stitching_a_ * last_time_a_rate * last_time_a_rate;
628
629 // 4. objective to minimize input change rates, [0- horizon_ -2]
630 time_index++;
631 for (int i = 0; i < horizon_ - 1; ++i) {
632 T steering_rate =
633 (x[control_index + 2] - x[control_index]) / x[time_index] / ts_;
634 T a_rate =
635 (x[control_index + 3] - x[control_index + 1]) / x[time_index] / ts_;
636 *obj_value += weight_rate_steer_ * steering_rate * steering_rate +
637 weight_rate_a_ * a_rate * a_rate;
638 control_index += 2;
639 time_index++;
640 }
641
642 // 5. objective to minimize total time [0, horizon_]
643 time_index = time_start_index_;
644 for (int i = 0; i < horizon_ + 1; ++i) {
645 T first_order_penalty = weight_first_order_time_ * x[time_index];
646 T second_order_penalty =
647 weight_second_order_time_ * x[time_index] * x[time_index];
648 *obj_value += first_order_penalty + second_order_penalty;
649 time_index++;
650 }
651}

◆ finalize_solution()

void apollo::planning::DistanceApproachIPOPTFixedDualInterface::finalize_solution ( Ipopt::SolverReturn  status,
int  n,
const double *  x,
const double *  z_L,
const double *  z_U,
int  m,
const double *  g,
const double *  lambda,
double  obj_value,
const Ipopt::IpoptData *  ip_data,
Ipopt::IpoptCalculatedQuantities *  ip_cq 
)
overridevirtual

This method is called when the algorithm is complete so the TNLP can store/write the solution

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 447 行定义.

451 {
452 int state_index = state_start_index_;
453 int control_index = control_start_index_;
454 int time_index = time_start_index_;
455 int dual_l_index = l_start_index_;
456 int dual_n_index = n_start_index_;
457
458 // enable_constraint_check_: for debug only
459 if (enable_constraint_check_) {
460 ADEBUG << "final resolution constraint checking";
461 check_g(n, x, m, g);
462 }
463 // 1. state variables, 4 * [0, horizon]
464 // 2. control variables, 2 * [0, horizon_-1]
465 // 3. sampling time variables, 1 * [0, horizon_]
466 // 4. dual_l obstacles_edges_sum_ * [0, horizon]
467 // 5. dual_n obstacles_num * [0, horizon]
468 for (int i = 0; i < horizon_; ++i) {
469 state_result_(0, i) = x[state_index];
470 state_result_(1, i) = x[state_index + 1];
471 state_result_(2, i) = x[state_index + 2];
472 state_result_(3, i) = x[state_index + 3];
473 control_result_(0, i) = x[control_index];
474 control_result_(1, i) = x[control_index + 1];
475 time_result_(0, i) = ts_ * x[time_index];
476 for (int j = 0; j < obstacles_edges_sum_; ++j) {
477 dual_l_result_(j, i) = x[dual_l_index + j];
478 }
479 for (int k = 0; k < 4 * obstacles_num_; k++) {
480 dual_n_result_(k, i) = x[dual_n_index + k];
481 }
482 state_index += 4;
483 control_index += 2;
484 time_index++;
485 dual_l_index += obstacles_edges_sum_;
486 dual_n_index += 4 * obstacles_num_;
487 }
488 state_result_(0, 0) = x0_(0, 0);
489 state_result_(1, 0) = x0_(1, 0);
490 state_result_(2, 0) = x0_(2, 0);
491 state_result_(3, 0) = x0_(3, 0);
492 // push back last horizon for states
493 state_result_(0, horizon_) = xf_(0, 0);
494 state_result_(1, horizon_) = xf_(1, 0);
495 state_result_(2, horizon_) = xf_(2, 0);
496 state_result_(3, horizon_) = xf_(3, 0);
497
498 for (int j = 0; j < obstacles_edges_sum_; ++j) {
499 dual_l_result_(j, horizon_) = 0.0;
500 }
501 for (int k = 0; k < 4 * obstacles_num_; k++) {
502 dual_n_result_(k, horizon_) = 0.0;
503 }
504 // memory deallocation of ADOL-C variables
505 delete[] obj_lam;
506
507 free(rind_g);
508 free(cind_g);
509 free(jacval);
510
511 free(rind_L);
512 free(cind_L);
513 free(hessval);
514}
bool check_g(int n, const double *x, int m, const double *g)
Check unfeasible constraints for further study

◆ generate_tapes()

void apollo::planning::DistanceApproachIPOPTFixedDualInterface::generate_tapes ( int  n,
int  m,
int *  nnz_jac_g,
int *  nnz_h_lag 
)

Method to generate the required tapes by ADOL-C

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 875 行定义.

877 {
878 std::vector<double> xp(n);
879 std::vector<double> lamp(m);
880 std::vector<double> zl(m);
881 std::vector<double> zu(m);
882 std::vector<adouble> xa(n);
883 std::vector<adouble> g(m);
884 std::vector<double> lam(m);
885
886 double sig = 0.0;
887 adouble obj_value;
888 double dummy = 0.0;
889 obj_lam = new double[m + 1];
890 get_starting_point(n, 1, &xp[0], 0, &zl[0], &zu[0], m, 0, &lamp[0]);
891
892 trace_on(tag_f);
893 for (int idx = 0; idx < n; idx++) {
894 xa[idx] <<= xp[idx];
895 }
896 eval_obj(n, &xa[0], &obj_value);
897 obj_value >>= dummy;
898 trace_off();
899
900 trace_on(tag_g);
901 for (int idx = 0; idx < n; idx++) {
902 xa[idx] <<= xp[idx];
903 }
904 eval_constraints(n, &xa[0], m, &g[0]);
905 for (int idx = 0; idx < m; idx++) {
906 g[idx] >>= dummy;
907 }
908 trace_off();
909
910 trace_on(tag_L);
911 for (int idx = 0; idx < n; idx++) {
912 xa[idx] <<= xp[idx];
913 }
914 for (int idx = 0; idx < m; idx++) {
915 lam[idx] = 1.0;
916 }
917 sig = 1.0;
918 eval_obj(n, &xa[0], &obj_value);
919 obj_value *= mkparam(sig);
920 eval_constraints(n, &xa[0], m, &g[0]);
921 for (int idx = 0; idx < m; idx++) {
922 obj_value += g[idx] * mkparam(lam[idx]);
923 }
924 obj_value >>= dummy;
925
926 trace_off();
927
928 rind_g = nullptr;
929 cind_g = nullptr;
930 jacval = nullptr;
931 options_g[0] = 0; /* sparsity pattern by index domains (default) */
932 options_g[1] = 0; /* safe mode (default) */
933 options_g[2] = 0;
934 options_g[3] = 0; /* column compression (default) */
935
936 sparse_jac(tag_g, m, n, 0, &xp[0], &nnz_jac, &rind_g, &cind_g, &jacval,
937 options_g);
938 *nnz_jac_g = nnz_jac;
939
940 rind_L = nullptr;
941 cind_L = nullptr;
942 hessval = nullptr;
943 options_L[0] = 0;
944 options_L[1] = 1;
945
946 sparse_hess(tag_L, n, 0, &xp[0], &nnz_L, &rind_L, &cind_L, &hessval,
947 options_L);
948 *nnz_h_lag = nnz_L;
949}
bool get_starting_point(int n, bool init_x, double *x, bool init_z, double *z_L, double *z_U, int m, bool init_lambda, double *lambda) override
Method to return the starting point for the algorithm

◆ get_bounds_info()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::get_bounds_info ( int  n,
double *  x_l,
double *  x_u,
int  m,
double *  g_l,
double *  g_u 
)
overridevirtual

Method to return the bounds for my problem

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 153 行定义.

154 {
155 ADEBUG << "get_bounds_info";
156 ACHECK(XYbounds_.size() == 4)
157 << "XYbounds_ size is not 4, but" << XYbounds_.size();
158
159 // Variables: includes state, u, sample time and lagrange multipliers
160 // 1. state variables, 4 * [0, horizon]
161 // start point pose
162 int variable_index = 0;
163 for (int i = 0; i < 4; ++i) {
164 x_l[i] = -2e19;
165 x_u[i] = 2e19;
166 }
167 variable_index += 4;
168
169 // During horizons, 2 ~ N-1
170 for (int i = 1; i < horizon_; ++i) {
171 // x
172 x_l[variable_index] = -2e19;
173 x_u[variable_index] = 2e19;
174
175 // y
176 x_l[variable_index + 1] = -2e19;
177 x_u[variable_index + 1] = 2e19;
178
179 // phi
180 x_l[variable_index + 2] = -2e19;
181 x_u[variable_index + 2] = 2e19;
182
183 // v
184 x_l[variable_index + 3] = -2e19;
185 x_u[variable_index + 3] = 2e19;
186
187 variable_index += 4;
188 }
189
190 // end point pose
191 for (int i = 0; i < 4; ++i) {
192 x_l[variable_index + i] = -2e19;
193 x_u[variable_index + i] = 2e19;
194 }
195 variable_index += 4;
196 ADEBUG << "variable_index after adding state variables : " << variable_index;
197
198 // 2. control variables, 2 * [0, horizon_-1]
199 for (int i = 0; i < horizon_; ++i) {
200 // steer
201 x_l[variable_index] = -2e19;
202 x_u[variable_index] = 2e19;
203
204 // a
205 x_l[variable_index + 1] = -2e19;
206 x_u[variable_index + 1] = 2e19;
207
208 variable_index += 2;
209 }
210 ADEBUG << "variable_index after adding control variables : "
211 << variable_index;
212
213 // 3. sampling time variables, 1 * [0, horizon_]
214 for (int i = 0; i < horizon_ + 1; ++i) {
215 x_l[variable_index] = -2e19;
216 x_u[variable_index] = 2e19;
217 ++variable_index;
218 }
219 ADEBUG << "variable_index after adding sample time : " << variable_index;
220 ADEBUG << "sample time fix time status is : " << use_fix_time_;
221
222 // Constraints: includes four state Euler forward constraints, three
223 // Obstacle related constraints
224
225 // 1. dynamics constraints 4 * [0, horizons-1]
226 int constraint_index = 0;
227 for (int i = 0; i < 4 * horizon_; ++i) {
228 g_l[i] = 0.0;
229 g_u[i] = 0.0;
230 }
231 constraint_index += 4 * horizon_;
232
233 ADEBUG << "constraint_index after adding Euler forward dynamics constraints: "
234 << constraint_index;
235
236 // 2. Control rate limit constraints, 1 * [0, horizons-1], only apply
237 // steering rate as of now
238 for (int i = 0; i < horizon_; ++i) {
239 g_l[constraint_index] = -max_steer_rate_;
240 g_u[constraint_index] = max_steer_rate_;
241 ++constraint_index;
242 }
243
244 ADEBUG << "constraint_index after adding steering rate constraints: "
245 << constraint_index;
246
247 // 3. Time constraints 1 * [0, horizons-1]
248 for (int i = 0; i < horizon_; ++i) {
249 g_l[constraint_index] = 0.0;
250 g_u[constraint_index] = 0.0;
251 ++constraint_index;
252 }
253
254 ADEBUG << "constraint_index after adding time constraints: "
255 << constraint_index;
256
257 // 4. Three obstacles related equal constraints, one equality constraints,
258 // [0, horizon_] * [0, obstacles_num_-1] * 4
259
260 // 5. load variable bounds as constraints
261 // start configuration
262 g_l[constraint_index] = x0_(0, 0);
263 g_u[constraint_index] = x0_(0, 0);
264 g_l[constraint_index + 1] = x0_(1, 0);
265 g_u[constraint_index + 1] = x0_(1, 0);
266 g_l[constraint_index + 2] = x0_(2, 0);
267 g_u[constraint_index + 2] = x0_(2, 0);
268 g_l[constraint_index + 3] = x0_(3, 0);
269 g_u[constraint_index + 3] = x0_(3, 0);
270 constraint_index += 4;
271
272 for (int i = 1; i < horizon_; ++i) {
273 g_l[constraint_index] = XYbounds_[0];
274 g_u[constraint_index] = XYbounds_[1];
275 g_l[constraint_index + 1] = XYbounds_[2];
276 g_u[constraint_index + 1] = XYbounds_[3];
277 g_l[constraint_index + 2] = -max_speed_reverse_;
278 g_u[constraint_index + 2] = max_speed_forward_;
279 constraint_index += 3;
280 }
281
282 // end configuration
283 g_l[constraint_index] = xf_(0, 0);
284 g_u[constraint_index] = xf_(0, 0);
285 g_l[constraint_index + 1] = xf_(1, 0);
286 g_u[constraint_index + 1] = xf_(1, 0);
287 g_l[constraint_index + 2] = xf_(2, 0);
288 g_u[constraint_index + 2] = xf_(2, 0);
289 g_l[constraint_index + 3] = xf_(3, 0);
290 g_u[constraint_index + 3] = xf_(3, 0);
291 constraint_index += 4;
292
293 for (int i = 0; i < horizon_; ++i) {
294 g_l[constraint_index] = -max_steer_angle_;
295 g_u[constraint_index] = max_steer_angle_;
296 g_l[constraint_index + 1] = -max_acceleration_reverse_;
297 g_u[constraint_index + 1] = max_acceleration_forward_;
298 constraint_index += 2;
299 }
300
301 for (int i = 0; i < horizon_ + 1; ++i) {
302 if (!use_fix_time_) {
303 g_l[constraint_index] = min_time_sample_scaling_;
304 g_u[constraint_index] = max_time_sample_scaling_;
305 } else {
306 g_l[constraint_index] = 1.0;
307 g_u[constraint_index] = 1.0;
308 }
309 constraint_index++;
310 }
311
312 ADEBUG << "constraint_index after adding obstacles constraints: "
313 << constraint_index;
314 ADEBUG << "get_bounds_info_ out";
315 return true;
316}

◆ get_nlp_info()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::get_nlp_info ( int &  n,
int &  m,
int &  nnz_jac_g,
int &  nnz_h_lag,
IndexStyleEnum &  index_style 
)
overridevirtual

Method to return some info about the nlp

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 110 行定义.

112 {
113 ADEBUG << "get_nlp_info";
114 // n1 : states variables, 4 * (N+1)
115 int n1 = 4 * (horizon_ + 1);
116 ADEBUG << "n1: " << n1;
117 // n2 : control inputs variables
118 int n2 = 2 * horizon_;
119 ADEBUG << "n2: " << n2;
120 // n3 : sampling time variables
121 int n3 = horizon_ + 1;
122 ADEBUG << "n3: " << n3;
123 // n4 : dual multiplier associated with obstacle shape
124 lambda_horizon_ = 0;
125 // n5 : dual multipier associated with car shape, obstacles_num*4 * (N+1)
126 miu_horizon_ = 0;
127
128 // m1 : dynamics constatins
129 int m1 = 4 * horizon_;
130 // m2 : control rate constraints (only steering)
131 int m2 = horizon_;
132 // m3 : sampling time equality constraints
133 int m3 = horizon_;
134 // m4 : obstacle constraints
135 // int m4 = 4 * obstacles_num_ * (horizon_ + 1);
136
137 num_of_variables_ = n1 + n2 + n3;
138 num_of_constraints_ = m1 + m2 + m3 + (num_of_variables_ - (horizon_ + 1) + 2);
139
140 // number of variables
141 n = num_of_variables_;
142 ADEBUG << "num_of_variables_ " << num_of_variables_;
143 // number of constraints
144 m = num_of_constraints_;
145 ADEBUG << "num_of_constraints_ " << num_of_constraints_;
146
147 generate_tapes(n, m, &nnz_jac_g, &nnz_h_lag);
148
149 index_style = IndexStyleEnum::C_STYLE;
150 return true;
151}
void generate_tapes(int n, int m, int *nnz_jac_g, int *nnz_h_lag)
Method to generate the required tapes by ADOL-C

◆ get_optimization_results()

void apollo::planning::DistanceApproachIPOPTFixedDualInterface::get_optimization_results ( Eigen::MatrixXd *  state_result,
Eigen::MatrixXd *  control_result,
Eigen::MatrixXd *  time_result,
Eigen::MatrixXd *  dual_l_result,
Eigen::MatrixXd *  dual_n_result 
) const
overridevirtual

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 516 行定义.

519 {
520 ADEBUG << "get_optimization_results";
521 *state_result = state_result_;
522 *control_result = control_result_;
523 *time_result = time_result_;
524 *dual_l_result = dual_l_result_;
525 *dual_n_result = dual_n_result_;
526
527 if (!distance_approach_config_.enable_initial_final_check()) {
528 return;
529 }
530 CHECK_EQ(state_result_.cols(), xWS_.cols());
531 CHECK_EQ(state_result_.rows(), xWS_.rows());
532 double state_diff_max = 0.0;
533 for (int i = 0; i < horizon_ + 1; ++i) {
534 for (int j = 0; j < 4; ++j) {
535 state_diff_max =
536 std::max(std::abs(xWS_(j, i) - state_result_(j, i)), state_diff_max);
537 }
538 }
539
540 // 2. control variable initialization, 2 * horizon_
541 CHECK_EQ(control_result_.cols(), uWS_.cols());
542 CHECK_EQ(control_result_.rows(), uWS_.rows());
543 double control_diff_max = 0.0;
544 for (int i = 0; i < horizon_; ++i) {
545 control_diff_max = std::max(std::abs(uWS_(0, i) - control_result_(0, i)),
546 control_diff_max);
547 control_diff_max = std::max(std::abs(uWS_(1, i) - control_result_(1, i)),
548 control_diff_max);
549 }
550
551 // 2. time scale variable initialization, horizon_ + 1
552
553 // 3. lagrange constraint l, obstacles_edges_sum_ * (horizon_+1)
554 CHECK_EQ(dual_l_result_.cols(), l_warm_up_.cols());
555 CHECK_EQ(dual_l_result_.rows(), l_warm_up_.rows());
556 double l_diff_max = 0.0;
557 for (int i = 0; i < horizon_ + 1; ++i) {
558 for (int j = 0; j < obstacles_edges_sum_; ++j) {
559 l_diff_max = std::max(std::abs(l_warm_up_(j, i) - dual_l_result_(j, i)),
560 l_diff_max);
561 }
562 }
563
564 // 4. lagrange constraint m, 4*obstacles_num * (horizon_+1)
565 CHECK_EQ(n_warm_up_.cols(), dual_n_result_.cols());
566 CHECK_EQ(n_warm_up_.rows(), dual_n_result_.rows());
567 double n_diff_max = 0.0;
568 for (int i = 0; i < horizon_ + 1; ++i) {
569 for (int j = 0; j < 4 * obstacles_num_; ++j) {
570 n_diff_max = std::max(std::abs(n_warm_up_(j, i) - dual_n_result_(j, i)),
571 n_diff_max);
572 }
573 }
574
575 ADEBUG << "state_diff_max: " << state_diff_max;
576 ADEBUG << "control_diff_max: " << control_diff_max;
577 ADEBUG << "dual_l_diff_max: " << l_diff_max;
578 ADEBUG << "dual_n_diff_max: " << n_diff_max;
579}

◆ get_starting_point()

bool apollo::planning::DistanceApproachIPOPTFixedDualInterface::get_starting_point ( int  n,
bool  init_x,
double *  x,
bool  init_z,
double *  z_L,
double *  z_U,
int  m,
bool  init_lambda,
double *  lambda 
)
overridevirtual

Method to return the starting point for the algorithm

实现了 apollo::planning::DistanceApproachInterface.

在文件 distance_approach_ipopt_fixed_dual_interface.cc 第 318 行定义.

320 {
321 ADEBUG << "get_starting_point";
322 ACHECK(init_x) << "Warm start init_x setting failed";
323
324 CHECK_EQ(horizon_, uWS_.cols());
325 CHECK_EQ(horizon_ + 1, xWS_.cols());
326
327 // 1. state variables 4 * (horizon_ + 1)
328 for (int i = 0; i < horizon_ + 1; ++i) {
329 int index = i * 4;
330 for (int j = 0; j < 4; ++j) {
331 x[index + j] = xWS_(j, i);
332 }
333 }
334
335 // 2. control variable initialization, 2 * horizon_
336 for (int i = 0; i < horizon_; ++i) {
337 int index = i * 2;
338 x[control_start_index_ + index] = uWS_(0, i);
339 x[control_start_index_ + index + 1] = uWS_(1, i);
340 }
341
342 // 3. time scale variable initialization, horizon_ + 1
343 for (int i = 0; i < horizon_ + 1; ++i) {
344 x[time_start_index_ + i] =
345 0.5 * (min_time_sample_scaling_ + max_time_sample_scaling_);
346 }
347
348 // check initial states
349 if (distance_approach_config_.enable_check_initial_state()) {
350 int kM = m;
351 double g[kM];
352 eval_g(n, x, true, m, g);
353 check_g(n, x, m, g);
354 }
355
356 ADEBUG << "get_starting_point out";
357 return true;
358}
bool eval_g(int n, const double *x, bool new_x, int m, double *g) override
Method to return the constraint residuals

该类的文档由以下文件生成: