Apollo  6.0
Open source self driving car software
piecewise_jerk_problem.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 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 <tuple>
24 #include <utility>
25 #include <vector>
26 
27 #include "osqp/osqp.h"
28 
29 namespace apollo {
30 namespace planning {
31 
32 /*
33  * @brief:
34  * This class solve an optimization problem:
35  * x
36  * |
37  * | P(s1, x1) P(s2, x2)
38  * | P(s0, x0) ... P(s(k-1), x(k-1))
39  * |P(start)
40  * |
41  * |________________________________________________________ s
42  *
43  * we suppose s(k+1) - s(k) == s(k) - s(k-1)
44  *
45  * Given the x, x', x'' at P(start), The goal is to find x0, x1, ... x(k-1)
46  * which makes the line P(start), P0, P(1) ... P(k-1) "smooth".
47  */
48 
50  public:
51  PiecewiseJerkProblem(const size_t num_of_knots, const double delta_s,
52  const std::array<double, 3>& x_init);
53 
54  virtual ~PiecewiseJerkProblem() = default;
55 
56  void set_x_bounds(std::vector<std::pair<double, double>> x_bounds);
57 
58  void set_x_bounds(const double x_lower_bound, const double x_upper_bound);
59 
60  void set_dx_bounds(std::vector<std::pair<double, double>> dx_bounds);
61 
62  void set_dx_bounds(const double dx_lower_bound, const double dx_upper_bound);
63 
64  void set_ddx_bounds(std::vector<std::pair<double, double>> ddx_bounds);
65 
66  void set_ddx_bounds(const double ddx_lower_bound,
67  const double ddx_upper_bound);
68 
69  void set_dddx_bound(const double dddx_bound) {
70  set_dddx_bound(-dddx_bound, dddx_bound);
71  }
72 
73  void set_dddx_bound(const double dddx_lower_bound,
74  const double dddx_upper_bound) {
75  dddx_bound_.first = dddx_lower_bound;
76  dddx_bound_.second = dddx_upper_bound;
77  }
78 
79  void set_weight_x(const double weight_x) { weight_x_ = weight_x; }
80 
81  void set_weight_dx(const double weight_dx) { weight_dx_ = weight_dx; }
82 
83  void set_weight_ddx(const double weight_ddx) { weight_ddx_ = weight_ddx; }
84 
85  void set_weight_dddx(const double weight_dddx) { weight_dddx_ = weight_dddx; }
86 
87  void set_scale_factor(const std::array<double, 3>& scale_factor) {
88  scale_factor_ = scale_factor;
89  }
90 
97  void set_x_ref(const double weight_x_ref, std::vector<double> x_ref);
98 
105  void set_x_ref(std::vector<double> weight_x_ref_vec,
106  std::vector<double> x_ref);
107 
108  void set_end_state_ref(const std::array<double, 3>& weight_end_state,
109  const std::array<double, 3>& end_state_ref);
110 
111  virtual bool Optimize(const int max_iter = 4000);
112 
113  const std::vector<double>& opt_x() const { return x_; }
114 
115  const std::vector<double>& opt_dx() const { return dx_; }
116 
117  const std::vector<double>& opt_ddx() const { return ddx_; }
118 
119  protected:
120  // naming convention follows osqp solver.
121  virtual void CalculateKernel(std::vector<c_float>* P_data,
122  std::vector<c_int>* P_indices,
123  std::vector<c_int>* P_indptr) = 0;
124 
125  virtual void CalculateOffset(std::vector<c_float>* q) = 0;
126 
127  virtual void CalculateAffineConstraint(std::vector<c_float>* A_data,
128  std::vector<c_int>* A_indices,
129  std::vector<c_int>* A_indptr,
130  std::vector<c_float>* lower_bounds,
131  std::vector<c_float>* upper_bounds);
132 
133  virtual OSQPSettings* SolverDefaultSettings();
134 
135  OSQPData* FormulateProblem();
136 
137  void FreeData(OSQPData* data);
138 
139  template <typename T>
140  T* CopyData(const std::vector<T>& vec) {
141  T* data = new T[vec.size()];
142  memcpy(data, vec.data(), sizeof(T) * vec.size());
143  return data;
144  }
145 
146  protected:
147  size_t num_of_knots_ = 0;
148 
149  // output
150  std::vector<double> x_;
151  std::vector<double> dx_;
152  std::vector<double> ddx_;
153 
154  std::array<double, 3> x_init_;
155  std::array<double, 3> scale_factor_ = {{1.0, 1.0, 1.0}};
156 
157  std::vector<std::pair<double, double>> x_bounds_;
158  std::vector<std::pair<double, double>> dx_bounds_;
159  std::vector<std::pair<double, double>> ddx_bounds_;
160  std::pair<double, double> dddx_bound_;
161 
162  double weight_x_ = 0.0;
163  double weight_dx_ = 0.0;
164  double weight_ddx_ = 0.0;
165  double weight_dddx_ = 0.0;
166 
167  double delta_s_ = 1.0;
168 
169  bool has_x_ref_ = false;
170  double weight_x_ref_ = 0.0;
171  std::vector<double> x_ref_;
172  // un-uniformed weighting
173  std::vector<double> weight_x_ref_vec_;
174 
175  bool has_end_state_ref_ = false;
176  std::array<double, 3> weight_end_state_ = {{0.0, 0.0, 0.0}};
177  std::array<double, 3> end_state_ref_;
178 };
179 
180 } // namespace planning
181 } // namespace apollo
std::array< double, 3 > weight_end_state_
Definition: piecewise_jerk_problem.h:176
std::array< double, 3 > x_init_
Definition: piecewise_jerk_problem.h:154
virtual bool Optimize(const int max_iter=4000)
virtual void CalculateAffineConstraint(std::vector< c_float > *A_data, std::vector< c_int > *A_indices, std::vector< c_int > *A_indptr, std::vector< c_float > *lower_bounds, std::vector< c_float > *upper_bounds)
virtual void CalculateKernel(std::vector< c_float > *P_data, std::vector< c_int > *P_indices, std::vector< c_int > *P_indptr)=0
std::vector< double > x_ref_
Definition: piecewise_jerk_problem.h:171
size_t num_of_knots_
Definition: piecewise_jerk_problem.h:147
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
virtual void CalculateOffset(std::vector< c_float > *q)=0
void set_weight_x(const double weight_x)
Definition: piecewise_jerk_problem.h:79
Planning module main class. It processes GPS and IMU as input, to generate planning info...
const std::vector< double > & opt_x() const
Definition: piecewise_jerk_problem.h:113
std::vector< std::pair< double, double > > x_bounds_
Definition: piecewise_jerk_problem.h:157
void set_dddx_bound(const double dddx_bound)
Definition: piecewise_jerk_problem.h:69
double delta_s_
Definition: piecewise_jerk_problem.h:167
std::vector< std::pair< double, double > > dx_bounds_
Definition: piecewise_jerk_problem.h:158
void set_x_bounds(std::vector< std::pair< double, double >> x_bounds)
double weight_ddx_
Definition: piecewise_jerk_problem.h:164
std::vector< std::pair< double, double > > ddx_bounds_
Definition: piecewise_jerk_problem.h:159
std::vector< double > weight_x_ref_vec_
Definition: piecewise_jerk_problem.h:173
const std::vector< double > & opt_dx() const
Definition: piecewise_jerk_problem.h:115
std::vector< double > ddx_
Definition: piecewise_jerk_problem.h:152
bool has_x_ref_
Definition: piecewise_jerk_problem.h:169
Definition: piecewise_jerk_problem.h:49
std::vector< double > dx_
Definition: piecewise_jerk_problem.h:151
double weight_x_ref_
Definition: piecewise_jerk_problem.h:170
bool has_end_state_ref_
Definition: piecewise_jerk_problem.h:175
double weight_dddx_
Definition: piecewise_jerk_problem.h:165
std::pair< double, double > dddx_bound_
Definition: piecewise_jerk_problem.h:160
void set_weight_dx(const double weight_dx)
Definition: piecewise_jerk_problem.h:81
double weight_dx_
Definition: piecewise_jerk_problem.h:163
void set_dddx_bound(const double dddx_lower_bound, const double dddx_upper_bound)
Definition: piecewise_jerk_problem.h:73
PiecewiseJerkProblem(const size_t num_of_knots, const double delta_s, const std::array< double, 3 > &x_init)
void set_x_ref(const double weight_x_ref, std::vector< double > x_ref)
Set the x ref object and the uniform x_ref weighting.
void set_weight_dddx(const double weight_dddx)
Definition: piecewise_jerk_problem.h:85
T * CopyData(const std::vector< T > &vec)
Definition: piecewise_jerk_problem.h:140
std::array< double, 3 > scale_factor_
Definition: piecewise_jerk_problem.h:155
std::array< double, 3 > end_state_ref_
Definition: piecewise_jerk_problem.h:177
double weight_x_
Definition: piecewise_jerk_problem.h:162
void set_weight_ddx(const double weight_ddx)
Definition: piecewise_jerk_problem.h:83
void set_ddx_bounds(std::vector< std::pair< double, double >> ddx_bounds)
void set_dx_bounds(std::vector< std::pair< double, double >> dx_bounds)
void set_scale_factor(const std::array< double, 3 > &scale_factor)
Definition: piecewise_jerk_problem.h:87
std::vector< double > x_
Definition: piecewise_jerk_problem.h:150
virtual OSQPSettings * SolverDefaultSettings()
void set_end_state_ref(const std::array< double, 3 > &weight_end_state, const std::array< double, 3 > &end_state_ref)
const std::vector< double > & opt_ddx() const
Definition: piecewise_jerk_problem.h:117