Apollo 11.0
自动驾驶开放平台
distance_approach_ipopt_cuda_interface.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17/*
18 * @file
19 */
20
21#pragma once
22
23#include <vector>
24
25#include <omp.h>
26
27#include <adolc/adolc.h>
28#include <adolc/adolc_openmp.h>
29#include <adolc/adolc_sparse.h>
30#include <adolc/adouble.h>
31
32#include <coin/IpTNLP.hpp>
33#include <coin/IpTypes.hpp>
34
35#include "Eigen/Dense"
36
37#include "modules/common_msgs/config_msgs/vehicle_config.pb.h"
38#include "modules/planning/planning_open_space/proto/planner_open_space_config.pb.h"
39
40#include "cyber/common/log.h"
41#include "cyber/common/macros.h"
47
48#define tag_f 1
49#define tag_g 2
50#define tag_L 3
51#define HPOFF 30
52
53namespace apollo {
54namespace planning {
55
57public:
59 const size_t horizon,
60 const double ts,
61 const Eigen::MatrixXd& ego,
62 const Eigen::MatrixXd& xWS,
63 const Eigen::MatrixXd& uWS,
64 const Eigen::MatrixXd& l_warm_up,
65 const Eigen::MatrixXd& n_warm_up,
66 const Eigen::MatrixXd& x0,
67 const Eigen::MatrixXd& xf,
68 const Eigen::MatrixXd& last_time_u,
69 const std::vector<double>& XYbounds,
70 const Eigen::MatrixXi& obstacles_edges_num,
71 const size_t obstacles_num,
72 const Eigen::MatrixXd& obstacles_A,
73 const Eigen::MatrixXd& obstacles_b,
74 const PlannerOpenSpaceConfig& planner_open_space_config);
75
77
79 bool get_nlp_info(
80 int& n,
81 int& m,
82 int& nnz_jac_g,
83 int& nnz_h_lag, // NOLINT
84 IndexStyleEnum& index_style) override; // NOLINT
85
87 bool get_bounds_info(int n, double* x_l, double* x_u, int m, double* g_l, double* g_u) override;
88
91 int n,
92 bool init_x,
93 double* x,
94 bool init_z,
95 double* z_L,
96 double* z_U,
97 int m,
98 bool init_lambda,
99 double* lambda) override;
100
102 bool eval_f(int n, const double* x, bool new_x, double& obj_value) override;
103
105 bool eval_grad_f(int n, const double* x, bool new_x, double* grad_f) override;
106 // eval_grad_f by hand.
107 bool eval_grad_f_hand(int n, const double* x, bool new_x, double* grad_f);
108
110 bool eval_g(int n, const double* x, bool new_x, int m, double* g) override;
111
113 bool check_g(int n, const double* x, int m, const double* g);
114
119 bool eval_jac_g(int n, const double* x, bool new_x, int m, int nele_jac, int* iRow, int* jCol, double* values)
120 override;
121 // sequential implementation to jac_g
122 bool eval_jac_g_ser(int n, const double* x, bool new_x, int m, int nele_jac, int* iRow, int* jCol, double* values);
123 // parallel implementation to jac_g
124 bool eval_jac_g_par(int n, const double* x, bool new_x, int m, int nele_jac, int* iRow, int* jCol, double* values);
125
131 bool eval_h(
132 int n,
133 const double* x,
134 bool new_x,
135 double obj_factor,
136 int m,
137 const double* lambda,
138 bool new_lambda,
139 int nele_hess,
140 int* iRow,
141 int* jCol,
142 double* values) override;
143
148 Ipopt::SolverReturn status,
149 int n,
150 const double* x,
151 const double* z_L,
152 const double* z_U,
153 int m,
154 const double* g,
155 const double* lambda,
156 double obj_value,
157 const Ipopt::IpoptData* ip_data,
158 Ipopt::IpoptCalculatedQuantities* ip_cq) override;
159
161 Eigen::MatrixXd* state_result,
162 Eigen::MatrixXd* control_result,
163 Eigen::MatrixXd* time_result,
164 Eigen::MatrixXd* dual_l_result,
165 Eigen::MatrixXd* dual_n_result) const;
166
167 //*************** start ADOL-C part ***********************************
169 template <class T>
170 bool eval_obj(int n, const T* x, T* obj_value);
171
173 template <class T>
174 bool eval_constraints(int n, const T* x, int m, T* g);
175
177 void generate_tapes(int n, int m, int* nnz_h_lag);
178 //*************** end ADOL-C part ***********************************
179
180private:
181 int num_of_variables_ = 0;
182 int num_of_constraints_ = 0;
183 int horizon_ = 0;
184 int lambda_horizon_ = 0;
185 int miu_horizon_ = 0;
186 double ts_ = 0.0;
187 Eigen::MatrixXd ego_;
188 Eigen::MatrixXd xWS_;
189 Eigen::MatrixXd uWS_;
190 Eigen::MatrixXd l_warm_up_;
191 Eigen::MatrixXd n_warm_up_;
192 Eigen::MatrixXd x0_;
193 Eigen::MatrixXd xf_;
194 Eigen::MatrixXd last_time_u_;
195 std::vector<double> XYbounds_;
196
197 // debug flag
198 bool enable_constraint_check_;
199
200 // penalty
201 double weight_state_x_ = 0.0;
202 double weight_state_y_ = 0.0;
203 double weight_state_phi_ = 0.0;
204 double weight_state_v_ = 0.0;
205 double weight_input_steer_ = 0.0;
206 double weight_input_a_ = 0.0;
207 double weight_rate_steer_ = 0.0;
208 double weight_rate_a_ = 0.0;
209 double weight_stitching_steer_ = 0.0;
210 double weight_stitching_a_ = 0.0;
211 double weight_first_order_time_ = 0.0;
212 double weight_second_order_time_ = 0.0;
213
214 double w_ev_ = 0.0;
215 double l_ev_ = 0.0;
216 std::vector<double> g_;
217 double offset_ = 0.0;
218 Eigen::MatrixXi obstacles_edges_num_;
219 int obstacles_num_ = 0;
220 int obstacles_edges_sum_ = 0;
221 double wheelbase_ = 0.0;
222
223 Eigen::MatrixXd state_result_;
224 Eigen::MatrixXd dual_l_result_;
225 Eigen::MatrixXd dual_n_result_;
226 Eigen::MatrixXd control_result_;
227 Eigen::MatrixXd time_result_;
228
229 // obstacles_A
230 Eigen::MatrixXd obstacles_A_;
231
232 // obstacles_b
233 Eigen::MatrixXd obstacles_b_;
234
235 // whether to use fix time
236 bool use_fix_time_ = false;
237
238 // state start index
239 int state_start_index_ = 0;
240
241 // control start index.
242 int control_start_index_ = 0;
243
244 // time start index
245 int time_start_index_ = 0;
246
247 // lagrangian l start index
248 int l_start_index_ = 0;
249
250 // lagrangian n start index
251 int n_start_index_ = 0;
252
253 double min_safety_distance_ = 0.0;
254
255 double max_safety_distance_ = 0.0;
256
257 double max_steer_angle_ = 0.0;
258
259 double max_speed_forward_ = 0.0;
260
261 double max_speed_reverse_ = 0.0;
262
263 double max_acceleration_forward_ = 0.0;
264
265 double max_acceleration_reverse_ = 0.0;
266
267 double min_time_sample_scaling_ = 0.0;
268
269 double max_time_sample_scaling_ = 0.0;
270
271 double max_steer_rate_ = 0.0;
272
273 double max_lambda_ = 0.0;
274
275 double max_miu_ = 0.0;
276
277private:
278 DistanceApproachConfig distance_approach_config_;
279 PlannerOpenSpaceConfig planner_open_space_config_;
281
282private:
283 //*************** start ADOL-C part ***********************************
284 double* obj_lam;
285 unsigned int* rind_L; /* row indices */
286 unsigned int* cind_L; /* column indices */
287 double* hessval; /* values */
288 int nnz_L = 0;
289 int options_L[4];
290 //*************** end ADOL-C part ***********************************
291
292 // park generic
293public:
295 const size_t horizon,
296 const double ts,
297 const Eigen::MatrixXd& ego,
298 const Eigen::MatrixXd& xWS,
299 const Eigen::MatrixXd& uWS,
300 const Eigen::MatrixXd& l_warm_up,
301 const Eigen::MatrixXd& n_warm_up,
302 const Eigen::MatrixXd& x0,
303 const Eigen::MatrixXd& xf,
304 const Eigen::MatrixXd& last_time_u,
305 const std::vector<double>& XYbounds,
306 const Eigen::MatrixXi& obstacles_edges_num,
307 const size_t obstacles_num,
308 const Eigen::MatrixXd& obstacles_A,
309 const Eigen::MatrixXd& obstacles_b,
310 const DistanceApproachConfig& distance_approach_config);
311};
312
313} // namespace planning
314} // namespace apollo
static const VehicleConfig & GetConfig()
Get the current vehicle configuration.
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_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 jaco...
bool check_g(int n, const double *x, int m, const double *g)
Check unfeasible constraints for further study
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
bool eval_jac_g_par(int n, const double *x, bool new_x, int m, int nele_jac, int *iRow, int *jCol, double *values)
bool eval_jac_g_ser(int n, const double *x, bool new_x, int m, int nele_jac, int *iRow, int *jCol, double *values)
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 eval_grad_f_hand(int n, const double *x, bool new_x, double *grad_f)
bool eval_g(int n, const double *x, bool new_x, int m, double *g) override
Method to return the constraint residuals
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
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_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 v...
bool eval_obj(int n, const T *x, T *obj_value)
Template to return the objective value
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 eval_f(int n, const double *x, bool new_x, double &obj_value) override
Method to return the objective value
void generate_tapes(int n, int m, int *nnz_h_lag)
Method to generate the required tapes by ADOL-C
bool eval_constraints(int n, const T *x, int m, T *g)
Template to compute constraints
Planning module main class.
Math-related util functions.
Some util functions.
class register implement
Definition arena_queue.h:37
optional VehicleParam vehicle_param