Apollo  6.0
Open source self driving car software
cos_theta_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 <cstddef>
20 
21 #include <map>
22 #include <utility>
23 #include <vector>
24 
25 #include <adolc/adolc.h>
26 #include <adolc/adolc_sparse.h>
27 #include <adolc/adouble.h>
28 
29 #include <coin/IpIpoptApplication.hpp>
30 #include <coin/IpIpoptCalculatedQuantities.hpp>
31 #include <coin/IpIpoptData.hpp>
32 #include <coin/IpOrigIpoptNLP.hpp>
33 #include <coin/IpSolveStatistics.hpp>
34 #include <coin/IpTNLP.hpp>
35 #include <coin/IpTNLPAdapter.hpp>
36 #include <coin/IpTypes.hpp>
37 
38 #define tag_f 1
39 #define tag_g 2
40 #define tag_L 3
41 #define HPOFF 30
42 
43 namespace apollo {
44 namespace planning {
45 
46 class CosThetaIpoptInterface : public Ipopt::TNLP {
47  public:
48  CosThetaIpoptInterface(std::vector<std::pair<double, double>> points,
49  std::vector<double> bounds);
50 
51  virtual ~CosThetaIpoptInterface() = default;
52 
53  void set_weight_cos_included_angle(const double weight_cos_included_angle);
54 
55  void set_weight_anchor_points(const double weight_anchor_points);
56 
57  void set_weight_length(const double weight_length);
58 
59  void set_automatic_differentiation_flag(const bool use_ad);
60 
61  void get_optimization_results(std::vector<double>* ptr_x,
62  std::vector<double>* ptr_y) const;
63 
65  bool get_nlp_info(int& n, int& m, int& nnz_jac_g, int& nnz_h_lag,
66  IndexStyleEnum& index_style) override;
67 
69  bool get_bounds_info(int n, double* x_l, double* x_u, int m, double* g_l,
70  double* g_u) override;
71 
73  bool get_starting_point(int n, bool init_x, double* x, bool init_z,
74  double* z_L, double* z_U, int m, bool init_lambda,
75  double* lambda) override;
76 
78  bool eval_f(int n, const double* x, bool new_x, double& obj_value) override;
79 
81  bool eval_grad_f(int n, const double* x, bool new_x, double* grad_f) override;
82 
84  bool eval_g(int n, const double* x, bool new_x, int m, double* g) override;
85 
90  bool eval_jac_g(int n, const double* x, bool new_x, int m, int nele_jac,
91  int* iRow, int* jCol, double* values) override;
92 
98  bool eval_h(int n, const double* x, bool new_x, double obj_factor, int m,
99  const double* lambda, bool new_lambda, int nele_hess, int* iRow,
100  int* jCol, double* values) override;
101 
105  void finalize_solution(Ipopt::SolverReturn status, int n, const double* x,
106  const double* z_L, const double* z_U, int m,
107  const double* g, const double* lambda,
108  double obj_value, const Ipopt::IpoptData* ip_data,
109  Ipopt::IpoptCalculatedQuantities* ip_cq) override;
110 
111  //*************** start ADOL-C part ***********************************
112 
114  template <class T>
115  bool eval_obj(int n, const T* x, T* obj_value);
116 
118  template <class T>
119  bool eval_constraints(int n, const T* x, int m, T* g);
120 
122  virtual void generate_tapes(int n, int m, int* nnz_jac_g, int* nnz_h_lag);
123 
124  //*************** end ADOL-C part ***********************************
125 
126  private:
127  std::vector<std::pair<double, double>> ref_points_;
128 
129  std::vector<double> bounds_;
130 
131  std::vector<double> opt_x_;
132 
133  std::vector<double> opt_y_;
134 
135  size_t num_of_variables_ = 0;
136 
137  size_t num_of_constraints_ = 0;
138 
139  size_t nnz_jac_g_ = 0;
140 
141  size_t nnz_h_lag_ = 0;
142 
143  size_t num_of_points_ = 0;
144 
145  std::map<std::pair<size_t, size_t>, size_t> idx_map_;
146 
147  void hessian_strcuture();
148 
149  double weight_cos_included_angle_ = 0.0;
150 
151  double weight_anchor_points_ = 0.0;
152 
153  double weight_length_ = 0.0;
154 
155  //*************** start ADOL-C part ***********************************
156 
157  bool use_automatic_differentiation_ = false;
162 
163  std::vector<double> obj_lam_;
164 
165  // TODO(Jinyun): Not changed to std::vector yet, need further debug
166  //** variables for sparsity exploitation
167  // std::vector<unsigned int> rind_g_; /* row indices */
168  // std::vector<unsigned int> cind_g_; /* column indices */
169  // std::vector<double> jacval_; /* values */
170  // std::vector<unsigned int> rind_L_; /* row indices */
171  // std::vector<unsigned int> cind_L_; /* column indices */
172  // std::vector<double> hessval_; /* values */
173 
174  //** variables for sparsity exploitation
175  unsigned int* rind_g_; /* row indices */
176  unsigned int* cind_g_; /* column indices */
177  double* jacval_; /* values */
178  unsigned int* rind_L_; /* row indices */
179  unsigned int* cind_L_; /* column indices */
180  double* hessval_; /* values */
181 
182  int nnz_jac_ = 0;
183  int nnz_L_ = 0;
184  int options_g_[4];
185  int options_L_[4];
186 
187  //*************** end ADOL-C part ***********************************
188 };
189 } // namespace planning
190 } // namespace apollo
bool eval_g(int n, const double *x, bool new_x, int m, double *g) override
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 get_optimization_results(std::vector< double > *ptr_x, std::vector< double > *ptr_y) const
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Planning module main class. It processes GPS and IMU as input, to generate planning info...
bool eval_jac_g(int n, const double *x, bool new_x, int m, int nele_jac, int *iRow, int *jCol, double *values) 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
Definition: cos_theta_ipopt_interface.h:46
bool get_bounds_info(int n, double *x_l, double *x_u, int m, double *g_l, double *g_u) override
void set_automatic_differentiation_flag(const bool use_ad)
void set_weight_cos_included_angle(const double weight_cos_included_angle)
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_weight_length(const double weight_length)
bool get_nlp_info(int &n, int &m, int &nnz_jac_g, int &nnz_h_lag, IndexStyleEnum &index_style) override
bool eval_f(int n, const double *x, bool new_x, double &obj_value) override
void set_weight_anchor_points(const double weight_anchor_points)
bool eval_constraints(int n, const T *x, int m, T *g)
bool eval_grad_f(int n, const double *x, bool new_x, double *grad_f) override
virtual void generate_tapes(int n, int m, int *nnz_jac_g, int *nnz_h_lag)
bool eval_obj(int n, const T *x, T *obj_value)
CosThetaIpoptInterface(std::vector< std::pair< double, double >> points, std::vector< double > bounds)