Apollo 11.0
自动驾驶开放平台
apollo::planning::FemPosDeviationIpoptInterface类 参考

#include <fem_pos_deviation_ipopt_interface.h>

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

Public 成员函数

 FemPosDeviationIpoptInterface (std::vector< std::pair< double, double > > points, std::vector< double > bounds)
 
virtual ~FemPosDeviationIpoptInterface ()=default
 
void set_weight_fem_pos_deviation (const double weight_fem_pos_deviation)
 
void set_weight_path_length (const double weight_path_length)
 
void set_weight_ref_deviation (const double weight_ref_deviation)
 
void set_weight_curvature_constraint_slack_var (const double weight_curvature_constraint_slack_var)
 
void set_curvature_constraint (const double curvature_constraint)
 
void get_optimization_results (std::vector< double > *ptr_x, std::vector< double > *ptr_y) const
 
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 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_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)
 

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
 
template<class T >
bool eval_obj (int n, const T *x, T *obj_value)
 Template to return the objective value
 
template<class T >
bool eval_constraints (int n, const T *x, int m, T *g)
 Template to compute contraints
 
virtual void generate_tapes (int n, int m, int *nnz_jac_g, int *nnz_h_lag)
 Method to generate the required tapes
 

详细描述

在文件 fem_pos_deviation_ipopt_interface.h 第 42 行定义.

构造及析构函数说明

◆ FemPosDeviationIpoptInterface()

apollo::planning::FemPosDeviationIpoptInterface::FemPosDeviationIpoptInterface ( std::vector< std::pair< double, double > >  points,
std::vector< double >  bounds 
)

在文件 fem_pos_deviation_ipopt_interface.cc 第 28 行定义.

29 {
30 CHECK_GT(points.size(), 1U);
31 CHECK_GT(bounds.size(), 1U);
32 bounds_around_refs_ = std::move(bounds);
33 ref_points_ = std::move(points);
34 num_of_points_ = ref_points_.size();
35}

◆ ~FemPosDeviationIpoptInterface()

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

成员函数说明

◆ eval_constraints()

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

Template to compute contraints

在文件 fem_pos_deviation_ipopt_interface.cc 第 314 行定义.

315 {
316 // a. positional deviation constraints
317 for (size_t i = 0; i < num_of_points_; ++i) {
318 size_t index = i * 2;
319 g[index] = x[index];
320 g[index + 1] = x[index + 1];
321 }
322
323 // b. curvature constraints
324 for (size_t i = 0; i + 2 < num_of_points_; ++i) {
325 size_t findex = i * 2;
326 size_t mindex = findex + 2;
327 size_t lindex = mindex + 2;
328 g[curvature_constr_start_index_ + i] =
329 (((x[findex] + x[lindex]) - 2.0 * x[mindex]) *
330 ((x[findex] + x[lindex]) - 2.0 * x[mindex]) +
331 ((x[findex + 1] + x[lindex + 1]) - 2.0 * x[mindex + 1]) *
332 ((x[findex + 1] + x[lindex + 1]) - 2.0 * x[mindex + 1])) -
333 x[slack_var_start_index_ + i];
334 }
335
336 // c. slack var constraints
337 size_t slack_var_index = 0;
338 for (size_t i = slack_constr_start_index_; i < slack_constr_end_index_; ++i) {
339 g[i] = x[slack_var_start_index_ + slack_var_index];
340 ++slack_var_index;
341 }
342 return true;
343}

◆ eval_f()

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

Method to return the objective value

在文件 fem_pos_deviation_ipopt_interface.cc 第 164 行定义.

165 {
166 CHECK_EQ(static_cast<size_t>(n), num_of_variables_);
167
168 eval_obj(n, x, &obj_value);
169 return true;
170}
bool eval_obj(int n, const T *x, T *obj_value)
Template to return the objective value

◆ eval_g()

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

Method to return the constraint residuals

在文件 fem_pos_deviation_ipopt_interface.cc 第 180 行定义.

181 {
182 CHECK_EQ(static_cast<size_t>(n), num_of_variables_);
183 CHECK_EQ(static_cast<size_t>(m), num_of_constraints_);
184
185 eval_constraints(n, x, m, g);
186 return true;
187}
bool eval_constraints(int n, const T *x, int m, T *g)
Template to compute contraints

◆ eval_grad_f()

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

Method to return the gradient of the objective

在文件 fem_pos_deviation_ipopt_interface.cc 第 172 行定义.

173 {
174 CHECK_EQ(static_cast<size_t>(n), num_of_variables_);
175
176 gradient(tag_f, n, x, grad_f);
177 return true;
178}
#define tag_f

◆ eval_h()

bool apollo::planning::FemPosDeviationIpoptInterface::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)

在文件 fem_pos_deviation_ipopt_interface.cc 第 213 行定义.

218 {
219 if (values == nullptr) {
220 // return the structure. This is a symmetric matrix, fill the lower left
221 // triangle only.
222 for (int idx = 0; idx < nnz_L_; idx++) {
223 iRow[idx] = rind_L_[idx];
224 jCol[idx] = cind_L_[idx];
225 }
226 } else {
227 // return the values. This is a symmetric matrix, fill the lower left
228 // triangle only
229 obj_lam_[0] = obj_factor;
230 for (int idx = 0; idx < m; idx++) {
231 obj_lam_[1 + idx] = lambda[idx];
232 }
233 set_param_vec(tag_L, m + 1, &obj_lam_[0]);
234 sparse_hess(tag_L, n, 1, const_cast<double*>(x), &nnz_L_, &rind_L_,
235 &cind_L_, &hessval_, options_L_);
236
237 for (int idx = 0; idx < nnz_L_; idx++) {
238 values[idx] = hessval_[idx];
239 }
240 }
241 return true;
242}
#define tag_L

◆ eval_jac_g()

bool apollo::planning::FemPosDeviationIpoptInterface::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)

在文件 fem_pos_deviation_ipopt_interface.cc 第 189 行定义.

192 {
193 CHECK_EQ(static_cast<size_t>(n), num_of_variables_);
194 CHECK_EQ(static_cast<size_t>(m), num_of_constraints_);
195
196 if (values == nullptr) {
197 // return the structure of the jacobian
198 for (int idx = 0; idx < nnz_jac_; idx++) {
199 iRow[idx] = rind_g_[idx];
200 jCol[idx] = cind_g_[idx];
201 }
202 } else {
203 // return the values of the jacobian of the constraints
204 sparse_jac(tag_g, m, n, 1, x, &nnz_jac_, &rind_g_, &cind_g_, &jacval_,
205 options_g_);
206 for (int idx = 0; idx < nnz_jac_; idx++) {
207 values[idx] = jacval_[idx];
208 }
209 }
210 return true;
211}
#define tag_g

◆ eval_obj()

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

Template to return the objective value

在文件 fem_pos_deviation_ipopt_interface.cc 第 268 行定义.

268 {
269 *obj_value = 0.0;
270
271 // Distance to refs
272 for (size_t i = 0; i < num_of_points_; ++i) {
273 size_t index = i * 2;
274 *obj_value +=
275 weight_ref_deviation_ *
276 ((x[index] - ref_points_[i].first) * (x[index] - ref_points_[i].first) +
277 (x[index + 1] - ref_points_[i].second) *
278 (x[index + 1] - ref_points_[i].second));
279 }
280
281 // Fem_pos_deviation
282 for (size_t i = 0; i + 2 < num_of_points_; ++i) {
283 size_t findex = i * 2;
284 size_t mindex = findex + 2;
285 size_t lindex = mindex + 2;
286
287 *obj_value += weight_fem_pos_deviation_ *
288 (((x[findex] + x[lindex]) - 2.0 * x[mindex]) *
289 ((x[findex] + x[lindex]) - 2.0 * x[mindex]) +
290 ((x[findex + 1] + x[lindex + 1]) - 2.0 * x[mindex + 1]) *
291 ((x[findex + 1] + x[lindex + 1]) - 2.0 * x[mindex + 1]));
292 }
293
294 // Total length
295 for (size_t i = 0; i + 1 < num_of_points_; ++i) {
296 size_t findex = i * 2;
297 size_t nindex = findex + 2;
298 *obj_value +=
299 weight_path_length_ *
300 ((x[findex] - x[nindex]) * (x[findex] - x[nindex]) +
301 (x[findex + 1] - x[nindex + 1]) * (x[findex + 1] - x[nindex + 1]));
302 }
303
304 // Slack variable minimization
305 for (size_t i = slack_var_start_index_; i < slack_var_end_index_; ++i) {
306 *obj_value += weight_curvature_constraint_slack_var_ * x[i];
307 }
308
309 return true;
310}

◆ finalize_solution()

void apollo::planning::FemPosDeviationIpoptInterface::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

在文件 fem_pos_deviation_ipopt_interface.cc 第 244 行定义.

248 {
249 opt_x_.reserve(num_of_points_);
250 opt_y_.reserve(num_of_points_);
251 for (size_t i = 0; i < num_of_points_; ++i) {
252 size_t index = i * 2;
253 opt_x_.emplace_back(x[index]);
254 opt_y_.emplace_back(x[index + 1]);
255 }
256 free(rind_g_);
257 free(cind_g_);
258 free(rind_L_);
259 free(cind_L_);
260 free(jacval_);
261 free(hessval_);
262}

◆ generate_tapes()

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

Method to generate the required tapes

在文件 fem_pos_deviation_ipopt_interface.cc 第 346 行定义.

347 {
348 std::vector<double> xp(n, 0.0);
349 std::vector<double> lamp(m, 0.0);
350 std::vector<double> zl(m, 0.0);
351 std::vector<double> zu(m, 0.0);
352 std::vector<adouble> xa(n, 0.0);
353 std::vector<adouble> g(m, 0.0);
354 std::vector<double> lam(m, 0.0);
355
356 double sig = 0.0;
357 adouble obj_value;
358
359 double dummy = 0.0;
360 obj_lam_.clear();
361 obj_lam_.resize(m + 1, 0.0);
362 get_starting_point(n, 1, &xp[0], 0, &zl[0], &zu[0], m, 0, &lamp[0]);
363
364 // Trace on Objectives
365 trace_on(tag_f);
366 for (int idx = 0; idx < n; idx++) {
367 xa[idx] <<= xp[idx];
368 }
369 eval_obj(n, &xa[0], &obj_value);
370 obj_value >>= dummy;
371 trace_off();
372
373 // Trace on Jacobian
374 trace_on(tag_g);
375 for (int idx = 0; idx < n; idx++) {
376 xa[idx] <<= xp[idx];
377 }
378 eval_constraints(n, &xa[0], m, &g[0]);
379 for (int idx = 0; idx < m; idx++) {
380 g[idx] >>= dummy;
381 }
382 trace_off();
383
384 // Trace on Hessian
385 trace_on(tag_L);
386 for (int idx = 0; idx < n; idx++) {
387 xa[idx] <<= xp[idx];
388 }
389 for (int idx = 0; idx < m; idx++) {
390 lam[idx] = 1.0;
391 }
392 sig = 1.0;
393 eval_obj(n, &xa[0], &obj_value);
394 obj_value *= mkparam(sig);
395 eval_constraints(n, &xa[0], m, &g[0]);
396 for (int idx = 0; idx < m; idx++) {
397 obj_value += g[idx] * mkparam(lam[idx]);
398 }
399 obj_value >>= dummy;
400 trace_off();
401
402 rind_g_ = nullptr;
403 cind_g_ = nullptr;
404 rind_L_ = nullptr;
405 cind_L_ = nullptr;
406
407 options_g_[0] = 0; /* sparsity pattern by index domains (default) */
408 options_g_[1] = 0; /* safe mode (default) */
409 options_g_[2] = 0;
410 options_g_[3] = 0; /* column compression (default) */
411
412 jacval_ = nullptr;
413 hessval_ = nullptr;
414
415 sparse_jac(tag_g, m, n, 0, &xp[0], &nnz_jac_, &rind_g_, &cind_g_, &jacval_,
416 options_g_);
417 *nnz_jac_g = nnz_jac_;
418 options_L_[0] = 0;
419 options_L_[1] = 1;
420 sparse_hess(tag_L, n, 0, &xp[0], &nnz_L_, &rind_L_, &cind_L_, &hessval_,
421 options_L_);
422 *nnz_h_lag = nnz_L_;
423}
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::FemPosDeviationIpoptInterface::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

在文件 fem_pos_deviation_ipopt_interface.cc 第 79 行定义.

81 {
82 CHECK_EQ(static_cast<size_t>(n), num_of_variables_);
83 CHECK_EQ(static_cast<size_t>(m), num_of_constraints_);
84 // variables
85 // a. for x, y
86 for (size_t i = 0; i < num_of_points_; ++i) {
87 size_t index = i * 2;
88 // x
89 x_l[index] = -1e20;
90 x_u[index] = 1e20;
91
92 // y
93 x_l[index + 1] = -1e20;
94 x_u[index + 1] = 1e20;
95 }
96 // b. for slack var
97 for (size_t i = slack_var_start_index_; i < slack_var_end_index_; ++i) {
98 x_l[i] = -1e20;
99 x_u[i] = 1e20;
100 }
101
102 // constraints
103 // a. positional deviation constraints
104 for (size_t i = 0; i < num_of_points_; ++i) {
105 size_t index = i * 2;
106 // x
107 g_l[index] = ref_points_[i].first - bounds_around_refs_[i];
108 g_u[index] = ref_points_[i].first + bounds_around_refs_[i];
109
110 // y
111 g_l[index + 1] = ref_points_[i].second - bounds_around_refs_[i];
112 g_u[index + 1] = ref_points_[i].second + bounds_around_refs_[i];
113 }
114
115 // b. curvature constraints
116 double ref_total_length = 0.0;
117 auto pre_point = ref_points_.front();
118 for (size_t i = 1; i < num_of_points_; ++i) {
119 auto cur_point = ref_points_[i];
120 double x_diff = cur_point.first - pre_point.first;
121 double y_diff = cur_point.second - pre_point.second;
122 ref_total_length += std::sqrt(x_diff * x_diff + y_diff * y_diff);
123 pre_point = cur_point;
124 }
125 double average_delta_s =
126 ref_total_length / static_cast<double>(num_of_points_ - 1);
127 double curvature_constr_upper =
128 average_delta_s * average_delta_s * curvature_constraint_;
129
130 for (size_t i = curvature_constr_start_index_;
131 i < curvature_constr_end_index_; ++i) {
132 g_l[i] = -1e20;
133 g_u[i] = curvature_constr_upper * curvature_constr_upper;
134 }
135
136 // c. slack var constraints
137 for (size_t i = slack_constr_start_index_; i < slack_constr_end_index_; ++i) {
138 g_l[i] = 0.0;
139 g_u[i] = 1e20;
140 }
141
142 return true;
143}

◆ get_nlp_info()

bool apollo::planning::FemPosDeviationIpoptInterface::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

在文件 fem_pos_deviation_ipopt_interface.cc 第 43 行定义.

45 {
46 CHECK_GT(num_of_points_, 3U);
47 // Number of variables
48 // Variables include 2D points and curvature constraints slack variable
49 num_of_slack_var_ = num_of_points_ - 2;
50 n = static_cast<int>(num_of_points_ * 2 + num_of_slack_var_);
51 num_of_variables_ = n;
52
53 // Number of constraints
54 // Constraints includes positional constraints, curvature constraints and
55 // slack variable constraints
56 num_of_curvature_constr_ = num_of_points_ - 2;
57 num_of_slack_constr_ = num_of_points_ - 2;
58 m = static_cast<int>(num_of_points_ * 2 + num_of_curvature_constr_ +
59 num_of_slack_constr_);
60 num_of_constraints_ = m;
61
62 // Indexes for variables and constraints,
63 // Start index is actual first index and end index is one index after the
64 // actual final index
65 slack_var_start_index_ = num_of_points_ * 2;
66 slack_var_end_index_ = slack_var_start_index_ + num_of_slack_var_;
67 curvature_constr_start_index_ = num_of_points_ * 2;
68 curvature_constr_end_index_ =
69 curvature_constr_start_index_ + num_of_curvature_constr_;
70 slack_constr_start_index_ = curvature_constr_end_index_;
71 slack_constr_end_index_ = slack_constr_start_index_ + num_of_slack_constr_;
72
73 generate_tapes(n, m, &nnz_jac_g, &nnz_h_lag);
74
75 index_style = IndexStyleEnum::C_STYLE;
76 return true;
77}
virtual void generate_tapes(int n, int m, int *nnz_jac_g, int *nnz_h_lag)
Method to generate the required tapes

◆ get_optimization_results()

void apollo::planning::FemPosDeviationIpoptInterface::get_optimization_results ( std::vector< double > *  ptr_x,
std::vector< double > *  ptr_y 
) const

在文件 fem_pos_deviation_ipopt_interface.cc 第 37 行定义.

38 {
39 *ptr_x = opt_x_;
40 *ptr_y = opt_y_;
41}

◆ get_starting_point()

bool apollo::planning::FemPosDeviationIpoptInterface::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

在文件 fem_pos_deviation_ipopt_interface.cc 第 145 行定义.

149 {
150 CHECK_EQ(static_cast<size_t>(n), num_of_variables_);
151 for (size_t i = 0; i < num_of_points_; ++i) {
152 size_t index = i * 2;
153 x[index] = ref_points_[i].first;
154 x[index + 1] = ref_points_[i].second;
155 }
156
157 for (size_t i = slack_var_start_index_; i < slack_var_end_index_; ++i) {
158 x[i] = 0.0;
159 }
160
161 return true;
162}

◆ set_curvature_constraint()

void apollo::planning::FemPosDeviationIpoptInterface::set_curvature_constraint ( const double  curvature_constraint)
inline

在文件 fem_pos_deviation_ipopt_interface.h 第 67 行定义.

67 {
68 curvature_constraint_ = curvature_constraint;
69 }

◆ set_weight_curvature_constraint_slack_var()

void apollo::planning::FemPosDeviationIpoptInterface::set_weight_curvature_constraint_slack_var ( const double  weight_curvature_constraint_slack_var)
inline

在文件 fem_pos_deviation_ipopt_interface.h 第 61 行定义.

62 {
63 weight_curvature_constraint_slack_var_ =
64 weight_curvature_constraint_slack_var;
65 }

◆ set_weight_fem_pos_deviation()

void apollo::planning::FemPosDeviationIpoptInterface::set_weight_fem_pos_deviation ( const double  weight_fem_pos_deviation)
inline

在文件 fem_pos_deviation_ipopt_interface.h 第 49 行定义.

49 {
50 weight_fem_pos_deviation_ = weight_fem_pos_deviation;
51 }

◆ set_weight_path_length()

void apollo::planning::FemPosDeviationIpoptInterface::set_weight_path_length ( const double  weight_path_length)
inline

在文件 fem_pos_deviation_ipopt_interface.h 第 53 行定义.

53 {
54 weight_path_length_ = weight_path_length;
55 }

◆ set_weight_ref_deviation()

void apollo::planning::FemPosDeviationIpoptInterface::set_weight_ref_deviation ( const double  weight_ref_deviation)
inline

在文件 fem_pos_deviation_ipopt_interface.h 第 57 行定义.

57 {
58 weight_ref_deviation_ = weight_ref_deviation;
59 }

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