Apollo  6.0
Open source self driving car software
distance_approach_ipopt_relax_end_slack_interface.h
Go to the documentation of this file.
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 <algorithm>
24 #include <limits>
25 #include <vector>
26 
27 #include <omp.h>
28 
29 #include <adolc/adolc.h>
30 #include <adolc/adolc_openmp.h>
31 #include <adolc/adolc_sparse.h>
32 #include <adolc/adouble.h>
33 
34 #include <coin/IpTNLP.hpp>
35 #include <coin/IpTypes.hpp>
36 
37 #include "Eigen/Dense"
38 
39 #include "cyber/common/log.h"
40 #include "cyber/common/macros.h"
41 #include "modules/common/configs/proto/vehicle_config.pb.h"
47 #include "modules/planning/proto/planner_open_space_config.pb.h"
48 
49 #define tag_f 1
50 #define tag_g 2
51 #define tag_L 3
52 #define HPOFF 30
53 
54 namespace apollo {
55 namespace planning {
56 
58  : public DistanceApproachInterface {
59  public:
61  const size_t horizon, const double ts, const Eigen::MatrixXd& ego,
62  const Eigen::MatrixXd& xWS, const Eigen::MatrixXd& uWS,
63  const Eigen::MatrixXd& l_warm_up, const Eigen::MatrixXd& n_warm_up,
64  const Eigen::MatrixXd& s_warm_up, const Eigen::MatrixXd& x0,
65  const Eigen::MatrixXd& xf, const Eigen::MatrixXd& last_time_u,
66  const std::vector<double>& XYbounds,
67  const Eigen::MatrixXi& obstacles_edges_num, const size_t obstacles_num,
68  const Eigen::MatrixXd& obstacles_A, const Eigen::MatrixXd& obstacles_b,
69  const PlannerOpenSpaceConfig& planner_open_space_config);
70 
72 
74  bool get_nlp_info(int& n, int& m, int& nnz_jac_g, int& nnz_h_lag, // NOLINT
75  IndexStyleEnum& index_style) override; // NOLINT
76 
78  bool get_bounds_info(int n, double* x_l, double* x_u, int m, double* g_l,
79  double* g_u) override;
80 
82  bool get_starting_point(int n, bool init_x, double* x, bool init_z,
83  double* z_L, double* z_U, int m, bool init_lambda,
84  double* lambda) override;
85 
87  bool eval_f(int n, const double* x, bool new_x, double& obj_value) override;
88 
90  bool eval_grad_f(int n, const double* x, bool new_x, double* grad_f) override;
91 
93  bool eval_g(int n, const double* x, bool new_x, int m, double* g) override;
94 
96  bool check_g(int n, const double* x, int m, const double* g);
97 
102  bool eval_jac_g(int n, const double* x, bool new_x, int m, int nele_jac,
103  int* iRow, int* jCol, double* values) override;
104 
105  // sequential implementation to jac_g
106  bool eval_jac_g_ser(int n, const double* x, bool new_x, int m, int nele_jac,
107  int* iRow, int* jCol, double* values) override;
108 
114  bool eval_h(int n, const double* x, bool new_x, double obj_factor, int m,
115  const double* lambda, bool new_lambda, int nele_hess, int* iRow,
116  int* jCol, double* values) override;
117 
121  void finalize_solution(Ipopt::SolverReturn status, int n, const double* x,
122  const double* z_L, const double* z_U, int m,
123  const double* g, const double* lambda,
124  double obj_value, const Ipopt::IpoptData* ip_data,
125  Ipopt::IpoptCalculatedQuantities* ip_cq) override;
126 
127  void get_optimization_results(Eigen::MatrixXd* state_result,
128  Eigen::MatrixXd* control_result,
129  Eigen::MatrixXd* time_result,
130  Eigen::MatrixXd* dual_l_result,
131  Eigen::MatrixXd* dual_n_result) const override;
132 
133  //*************** start ADOL-C part ***********************************
135  template <class T>
136  void eval_obj(int n, const T* x, T* obj_value);
137 
139  template <class T>
140  void eval_constraints(int n, const T* x, int m, T* g);
141 
143  void generate_tapes(int n, int m, int* nnz_jac_g, int* nnz_h_lag);
144  //*************** end ADOL-C part ***********************************
145 
146  private:
147  int num_of_variables_ = 0;
148  int num_of_constraints_ = 0;
149  int horizon_ = 0;
150  int lambda_horizon_ = 0;
151  int miu_horizon_ = 0;
152  int slack_horizon_ = 0;
153  double ts_ = 0.0;
154 
155  Eigen::MatrixXd ego_;
156  Eigen::MatrixXd xWS_;
157  Eigen::MatrixXd uWS_;
158  Eigen::MatrixXd l_warm_up_;
159  Eigen::MatrixXd n_warm_up_;
160  Eigen::MatrixXd slack_warm_up_;
161  Eigen::MatrixXd x0_;
162  Eigen::MatrixXd xf_;
163  Eigen::MatrixXd last_time_u_;
164  std::vector<double> XYbounds_;
165 
166  // debug flag
167  bool enable_constraint_check_;
168 
169  // penalty
170  double weight_state_x_ = 0.0;
171  double weight_state_y_ = 0.0;
172  double weight_state_phi_ = 0.0;
173  double weight_state_v_ = 0.0;
174  double weight_input_steer_ = 0.0;
175  double weight_input_a_ = 0.0;
176  double weight_rate_steer_ = 0.0;
177  double weight_rate_a_ = 0.0;
178  double weight_stitching_steer_ = 0.0;
179  double weight_stitching_a_ = 0.0;
180  double weight_first_order_time_ = 0.0;
181  double weight_second_order_time_ = 0.0;
182  double weight_end_state_ = 0.0;
183  double weight_slack_ = 0.0;
184 
185  double w_ev_ = 0.0;
186  double l_ev_ = 0.0;
187  std::vector<double> g_;
188  double offset_ = 0.0;
189  Eigen::MatrixXi obstacles_edges_num_;
190  int obstacles_num_ = 0;
191  int obstacles_edges_sum_ = 0;
192  double wheelbase_ = 0.0;
193 
194  Eigen::MatrixXd state_result_;
195  Eigen::MatrixXd dual_l_result_;
196  Eigen::MatrixXd dual_n_result_;
197  Eigen::MatrixXd control_result_;
198  Eigen::MatrixXd time_result_;
199  Eigen::MatrixXd slack_result_;
200 
201  // obstacles_A
202  Eigen::MatrixXd obstacles_A_;
203 
204  // obstacles_b
205  Eigen::MatrixXd obstacles_b_;
206 
207  // whether to use fix time
208  bool use_fix_time_ = false;
209 
210  // state start index
211  int state_start_index_ = 0;
212 
213  // control start index.
214  int control_start_index_ = 0;
215 
216  // time start index
217  int time_start_index_ = 0;
218 
219  // lagrangian l start index
220  int l_start_index_ = 0;
221 
222  // lagrangian n start index
223  int n_start_index_ = 0;
224 
225  // slack s start index
226  int slack_index_ = 0;
227 
228  double min_safety_distance_ = 0.0;
229 
230  double max_safety_distance_ = 0.0;
231 
232  double max_steer_angle_ = 0.0;
233 
234  double max_speed_forward_ = 0.0;
235 
236  double max_speed_reverse_ = 0.0;
237 
238  double max_acceleration_forward_ = 0.0;
239 
240  double max_acceleration_reverse_ = 0.0;
241 
242  double min_time_sample_scaling_ = 0.0;
243 
244  double max_time_sample_scaling_ = 0.0;
245 
246  double max_steer_rate_ = 0.0;
247 
248  double max_lambda_ = 0.0;
249 
250  double max_miu_ = 0.0;
251 
252  bool enable_jacobian_ad_ = false;
253 
254  private:
255  DistanceApproachConfig distance_approach_config_;
256  const common::VehicleParam vehicle_param_ =
257  common::VehicleConfigHelper::GetConfig().vehicle_param();
258 
259  private:
260  //*************** start ADOL-C part ***********************************
261  double* obj_lam;
262  //** variables for sparsity exploitation
263  unsigned int* rind_g; /* row indices */
264  unsigned int* cind_g; /* column indices */
265  double* jacval; /* values */
266  unsigned int* rind_L; /* row indices */
267  unsigned int* cind_L; /* column indices */
268  double* hessval; /* values */
269  int nnz_jac;
270  int nnz_L;
271  int options_g[4];
272  int options_L[4];
273  //*************** end ADOL-C part ***********************************
274 };
275 
276 } // namespace planning
277 } // namespace apollo
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
bool get_bounds_info(int n, double *x_l, double *x_u, int m, double *g_l, double *g_u) override
void eval_constraints(int n, const T *x, int m, T *g)
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 check_g(int n, const double *x, int m, const double *g)
bool eval_grad_f(int n, const double *x, bool new_x, double *grad_f) override
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool eval_g(int n, const double *x, bool new_x, int m, double *g) override
Planning module main class. It processes GPS and IMU as input, to generate planning info...
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
Definition: distance_approach_interface.h:50
void generate_tapes(int n, int m, int *nnz_jac_g, int *nnz_h_lag)
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
bool get_nlp_info(int &n, int &m, int &nnz_jac_g, int &nnz_h_lag, IndexStyleEnum &index_style) override
static const VehicleConfig & GetConfig()
Get the current vehicle configuration.
Math-related util functions.
bool eval_jac_g(int n, const double *x, bool new_x, int m, int nele_jac, int *iRow, int *jCol, double *values) override
bool eval_f(int n, const double *x, bool new_x, double &obj_value) 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
Some util functions.
Definition: distance_approach_ipopt_relax_end_slack_interface.h:57
DistanceApproachIPOPTRelaxEndSlackInterface(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 &s_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)