Apollo  6.0
Open source self driving car software
piecewise_jerk_speed_nonlinear_ipopt_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 
21 #pragma once
22 
23 #include <unordered_map>
24 #include <utility>
25 #include <vector>
26 
27 #include <coin/IpTNLP.hpp>
28 #include <coin/IpTypes.hpp>
29 
32 
33 namespace apollo {
34 namespace planning {
35 
36 class PiecewiseJerkSpeedNonlinearIpoptInterface : public Ipopt::TNLP {
37  public:
39  const double s_init, const double s_dot_init, const double s_ddot_init,
40  const double delta_t, const int num_of_points, const double s_max_,
41  const double s_dot_max, const double s_ddot_min, const double s_ddot_max,
42  const double s_dddot_min, const double s_dddot_max);
43 
45 
46  void set_warm_start(const std::vector<std::vector<double>> &speed_profile);
47 
48  void set_curvature_curve(const PiecewiseJerkTrajectory1d &curvature_curve);
49 
50  void get_optimization_results(std::vector<double> *ptr_opt_s,
51  std::vector<double> *ptr_opt_v,
52  std::vector<double> *ptr_opt_a);
53 
54  void set_end_state_target(const double s_target, const double v_target,
55  const double a_target);
56 
57  void set_reference_speed(const double s_dot_ref);
58 
59  void set_reference_spatial_distance(const std::vector<double> &s_ref);
60 
61  void set_speed_limit_curve(const PiecewiseJerkTrajectory1d &v_bound_f);
62 
63  void set_safety_bounds(
64  const std::vector<std::pair<double, double>> &safety_bounds);
65 
67  const std::vector<std::pair<double, double>> &soft_safety_bounds);
68 
69  void set_w_target_state(const double w_target_s, const double w_target_v,
70  const double w_target_a);
71 
72  void set_w_overall_a(const double w_overall_a);
73 
74  void set_w_overall_j(const double w_overall_j);
75 
76  void set_w_overall_centripetal_acc(const double w_overall_centripetal_acc);
77 
78  void set_w_reference_speed(const double w_reference_speed);
79 
80  void set_w_reference_spatial_distance(const double w_ref_s);
81 
82  void set_w_soft_s_bound(const double w_soft_s_bound);
83 
85  bool get_nlp_info(int &n, int &m, int &nnz_jac_g, int &nnz_h_lag,
86  IndexStyleEnum &index_style) override;
87 
89  bool get_bounds_info(int n, double *x_l, double *x_u, int m, double *g_l,
90  double *g_u) override;
91 
93  bool get_starting_point(int n, bool init_x, double *x, bool init_z,
94  double *z_L, double *z_U, int m, bool init_lambda,
95  double *lambda) override;
96 
98  bool eval_f(int n, const double *x, bool new_x, double &obj_value) override;
99 
101  bool eval_grad_f(int n, const double *x, bool new_x, double *grad_f) override;
102 
104  bool eval_g(int n, const double *x, bool new_x, int m, double *g) override;
105 
110  bool eval_jac_g(int n, const double *x, bool new_x, int m, int nele_jac,
111  int *iRow, int *jCol, double *values) override;
112 
118  bool eval_h(int n, const double *x, bool new_x, double obj_factor, int m,
119  const double *lambda, bool new_lambda, int nele_hess, int *iRow,
120  int *jCol, double *values) override;
121 
125  void finalize_solution(Ipopt::SolverReturn status, int n, const double *x,
126  const double *z_L, const double *z_U, int m,
127  const double *g, const double *lambda,
128  double obj_value, const Ipopt::IpoptData *ip_data,
129  Ipopt::IpoptCalculatedQuantities *ip_cq) override;
130 
131  private:
132  int to_hash_key(const int i, const int j) const;
133 
134  std::unordered_map<int, int> hessian_mapper_;
135 
136  PiecewiseJerkTrajectory1d curvature_curve_;
137 
138  bool use_v_bound_ = false;
139 
140  bool use_soft_safety_bound_ = false;
141 
142  PiecewiseJerkTrajectory1d v_bound_func_;
143 
144  const double s_init_;
145 
146  const double s_dot_init_;
147 
148  const double s_ddot_init_;
149 
150  const double delta_t_;
151 
152  const int num_of_points_;
153 
154  const double s_max_;
155 
156  const double s_dot_max_;
157 
158  const double s_ddot_min_;
159 
160  const double s_ddot_max_;
161 
162  const double s_dddot_min_;
163 
164  const double s_dddot_max_;
165 
166  const int v_offset_;
167 
168  const int a_offset_;
169 
170  int lower_s_slack_offset_ = 0;
171 
172  int upper_s_slack_offset_ = 0;
173 
174  int num_of_variables_ = 0;
175 
176  int num_of_constraints_ = 0;
177 
178  double w_target_s_ = 10000.0;
179 
180  double w_target_v_ = 10000.0;
181 
182  double w_target_a_ = 10000.0;
183 
184  double w_ref_v_ = 1.0;
185 
186  double w_ref_s_ = 1.0;
187 
188  double w_overall_a_ = 100.0;
189 
190  double w_overall_j_ = 10.0;
191 
192  double w_overall_centripetal_acc_ = 500.0;
193 
194  double w_soft_s_bound_ = 0.0;
195 
196  double v_max_ = 0.0;
197 
198  double s_target_ = 0.0;
199 
200  double v_target_ = 0.0;
201 
202  double a_target_ = 0.0;
203 
204  double v_ref_ = 0.0;
205 
206  std::vector<std::pair<double, double>> safety_bounds_;
207 
208  std::vector<std::pair<double, double>> soft_safety_bounds_;
209 
210  bool has_end_state_target_ = false;
211 
212  std::vector<double> opt_s_;
213 
214  std::vector<double> opt_v_;
215 
216  std::vector<double> opt_a_;
217 
218  std::vector<std::vector<double>> x_warm_start_;
219 
220  std::vector<double> s_ref_;
221 };
222 } // namespace planning
223 } // namespace apollo
void set_curvature_curve(const PiecewiseJerkTrajectory1d &curvature_curve)
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
void set_warm_start(const std::vector< std::vector< double >> &speed_profile)
bool get_nlp_info(int &n, int &m, int &nnz_jac_g, int &nnz_h_lag, IndexStyleEnum &index_style) override
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool eval_jac_g(int n, const double *x, bool new_x, int m, int nele_jac, int *iRow, int *jCol, double *values) override
Planning module main class. It processes GPS and IMU as input, to generate planning info...
Definition: piecewise_jerk_trajectory1d.h:32
void set_end_state_target(const double s_target, const double v_target, const double a_target)
void set_safety_bounds(const std::vector< std::pair< double, double >> &safety_bounds)
bool get_bounds_info(int n, double *x_l, double *x_u, int m, double *g_l, double *g_u) override
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
void set_w_overall_centripetal_acc(const double w_overall_centripetal_acc)
bool eval_g(int n, const double *x, bool new_x, int m, double *g) override
void set_reference_spatial_distance(const std::vector< double > &s_ref)
bool eval_grad_f(int n, const double *x, bool new_x, double *grad_f) override
void set_soft_safety_bounds(const std::vector< std::pair< double, double >> &soft_safety_bounds)
void set_w_target_state(const double w_target_s, const double w_target_v, const double w_target_a)
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
void set_w_reference_speed(const double w_reference_speed)
void set_speed_limit_curve(const PiecewiseJerkTrajectory1d &v_bound_f)
bool eval_f(int n, const double *x, bool new_x, double &obj_value) override
Definition: piecewise_jerk_speed_nonlinear_ipopt_interface.h:36
PiecewiseJerkSpeedNonlinearIpoptInterface(const double s_init, const double s_dot_init, const double s_ddot_init, const double delta_t, const int num_of_points, const double s_max_, const double s_dot_max, const double s_ddot_min, const double s_ddot_max, const double s_dddot_min, const double s_dddot_max)
void get_optimization_results(std::vector< double > *ptr_opt_s, std::vector< double > *ptr_opt_v, std::vector< double > *ptr_opt_a)