Apollo  6.0
Open source self driving car software
fem_pos_deviation_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 
17 #pragma once
18 
19 #include <utility>
20 #include <vector>
21 
22 #include <adolc/adolc.h>
23 #include <adolc/adolc_sparse.h>
24 #include <adolc/adouble.h>
25 
26 #include <coin/IpIpoptApplication.hpp>
27 #include <coin/IpIpoptCalculatedQuantities.hpp>
28 #include <coin/IpIpoptData.hpp>
29 #include <coin/IpOrigIpoptNLP.hpp>
30 #include <coin/IpSolveStatistics.hpp>
31 #include <coin/IpTNLP.hpp>
32 #include <coin/IpTNLPAdapter.hpp>
33 #include <coin/IpTypes.hpp>
34 
35 #define tag_f 1
36 #define tag_g 2
37 #define tag_L 3
38 #define HPOFF 30
39 
40 namespace apollo {
41 namespace planning {
42 
43 class FemPosDeviationIpoptInterface : public Ipopt::TNLP {
44  public:
45  FemPosDeviationIpoptInterface(std::vector<std::pair<double, double>> points,
46  std::vector<double> bounds);
47 
48  virtual ~FemPosDeviationIpoptInterface() = default;
49 
50  void set_weight_fem_pos_deviation(const double weight_fem_pos_deviation) {
51  weight_fem_pos_deviation_ = weight_fem_pos_deviation;
52  }
53 
54  void set_weight_path_length(const double weight_path_length) {
55  weight_path_length_ = weight_path_length;
56  }
57 
58  void set_weight_ref_deviation(const double weight_ref_deviation) {
59  weight_ref_deviation_ = weight_ref_deviation;
60  }
61 
63  const double weight_curvature_constraint_slack_var) {
64  weight_curvature_constraint_slack_var_ =
65  weight_curvature_constraint_slack_var;
66  }
67 
68  void set_curvature_constraint(const double curvature_constraint) {
69  curvature_constraint_ = curvature_constraint;
70  }
71 
72  void get_optimization_results(std::vector<double>* ptr_x,
73  std::vector<double>* ptr_y) const;
74 
76  bool get_nlp_info(int& n, int& m, int& nnz_jac_g, int& nnz_h_lag,
77  IndexStyleEnum& index_style) override;
78 
80  bool get_bounds_info(int n, double* x_l, double* x_u, int m, double* g_l,
81  double* g_u) override;
82 
84  bool get_starting_point(int n, bool init_x, double* x, bool init_z,
85  double* z_L, double* z_U, int m, bool init_lambda,
86  double* lambda) override;
87 
89  bool eval_f(int n, const double* x, bool new_x, double& obj_value) override;
90 
92  bool eval_grad_f(int n, const double* x, bool new_x, double* grad_f) override;
93 
95  bool eval_g(int n, const double* x, bool new_x, int m, double* g) override;
96 
101  bool eval_jac_g(int n, const double* x, bool new_x, int m, int nele_jac,
102  int* iRow, int* jCol, double* values) override;
103 
109  bool eval_h(int n, const double* x, bool new_x, double obj_factor, int m,
110  const double* lambda, bool new_lambda, int nele_hess, int* iRow,
111  int* jCol, double* values) override;
112 
116  void finalize_solution(Ipopt::SolverReturn status, int n, const double* x,
117  const double* z_L, const double* z_U, int m,
118  const double* g, const double* lambda,
119  double obj_value, const Ipopt::IpoptData* ip_data,
120  Ipopt::IpoptCalculatedQuantities* ip_cq) override;
121 
122  //*************** start ADOL-C part ***********************************
123 
125  template <class T>
126  bool eval_obj(int n, const T* x, T* obj_value);
127 
129  template <class T>
130  bool eval_constraints(int n, const T* x, int m, T* g);
131 
133  virtual void generate_tapes(int n, int m, int* nnz_jac_g, int* nnz_h_lag);
134 
135  //*************** end ADOL-C part ***********************************
136 
137  private:
138  // Reference points and deviation bounds
139  std::vector<std::pair<double, double>> ref_points_;
140  std::vector<double> bounds_around_refs_;
141 
142  // Weights in optimization cost function
143  double weight_fem_pos_deviation_ = 1.0e8;
144  double weight_path_length_ = 1.0;
145  double weight_ref_deviation_ = 1.0;
146  double weight_curvature_constraint_slack_var_ = 1.0e2;
147 
148  double curvature_constraint_ = 0.2;
149 
150  size_t num_of_variables_ = 0;
151  size_t num_of_constraints_ = 0;
152  size_t nnz_jac_g_ = 0;
153  size_t nnz_h_lag_ = 0;
154  size_t num_of_points_ = 0;
155  size_t num_of_slack_var_ = 0;
156  size_t num_of_curvature_constr_ = 0;
157  size_t num_of_slack_constr_ = 0;
158 
159  size_t slack_var_start_index_ = 0;
160  size_t slack_var_end_index_ = 0;
161  size_t curvature_constr_start_index_ = 0;
162  size_t curvature_constr_end_index_ = 0;
163  size_t slack_constr_start_index_ = 0;
164  size_t slack_constr_end_index_ = 0;
165 
166  std::vector<double> opt_x_;
167  std::vector<double> opt_y_;
168 
169  //*************** start ADOL-C part ***********************************
170 
176 
177  std::vector<double> obj_lam_;
178 
179  //** variables for sparsity exploitation
180  unsigned int* rind_g_; /* row indices */
181  unsigned int* cind_g_; /* column indices */
182  double* jacval_; /* values */
183  unsigned int* rind_L_; /* row indices */
184  unsigned int* cind_L_; /* column indices */
185  double* hessval_; /* values */
186 
187  int nnz_jac_ = 0;
188  int nnz_L_ = 0;
189  int options_g_[4];
190  int options_L_[4];
191 
192  //*************** end ADOL-C part ***********************************
193 };
194 } // namespace planning
195 } // namespace apollo
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
Definition: fem_pos_deviation_ipopt_interface.h:43
bool eval_obj(int n, const T *x, T *obj_value)
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
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
FemPosDeviationIpoptInterface(std::vector< std::pair< double, double >> points, std::vector< double > bounds)
bool eval_f(int n, const double *x, bool new_x, double &obj_value) override
Planning module main class. It processes GPS and IMU as input, to generate planning info...
void set_weight_curvature_constraint_slack_var(const double weight_curvature_constraint_slack_var)
Definition: fem_pos_deviation_ipopt_interface.h:62
void set_curvature_constraint(const double curvature_constraint)
Definition: fem_pos_deviation_ipopt_interface.h:68
virtual void generate_tapes(int n, int m, int *nnz_jac_g, int *nnz_h_lag)
void set_weight_path_length(const double weight_path_length)
Definition: fem_pos_deviation_ipopt_interface.h:54
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_weight_ref_deviation(const double weight_ref_deviation)
Definition: fem_pos_deviation_ipopt_interface.h:58
void set_weight_fem_pos_deviation(const double weight_fem_pos_deviation)
Definition: fem_pos_deviation_ipopt_interface.h:50
bool eval_constraints(int n, const T *x, int m, T *g)
bool get_nlp_info(int &n, int &m, int &nnz_jac_g, int &nnz_h_lag, IndexStyleEnum &index_style) override
void get_optimization_results(std::vector< double > *ptr_x, std::vector< double > *ptr_y) const
bool eval_grad_f(int n, const double *x, bool new_x, double *grad_f) override
bool eval_g(int n, const double *x, bool new_x, int m, double *g) override
bool get_bounds_info(int n, double *x_l, double *x_u, int m, double *g_l, double *g_u) override
bool eval_jac_g(int n, const double *x, bool new_x, int m, int nele_jac, int *iRow, int *jCol, double *values) override