Apollo  6.0
Open source self driving car software
fem_pos_deviation_sqp_osqp_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 <utility>
24 #include <vector>
25 
26 #include "osqp/osqp.h"
27 
28 namespace apollo {
29 namespace planning {
30 
32  public:
34 
35  virtual ~FemPosDeviationSqpOsqpInterface() = default;
36 
38  const std::vector<std::pair<double, double>>& ref_points) {
39  ref_points_ = ref_points;
40  }
41 
42  void set_bounds_around_refs(const std::vector<double>& bounds_around_refs) {
43  bounds_around_refs_ = bounds_around_refs;
44  }
45 
46  void set_weight_fem_pos_deviation(const double weight_fem_pos_deviation) {
47  weight_fem_pos_deviation_ = weight_fem_pos_deviation;
48  }
49 
50  void set_weight_path_length(const double weight_path_length) {
51  weight_path_length_ = weight_path_length;
52  }
53 
54  void set_weight_ref_deviation(const double weight_ref_deviation) {
55  weight_ref_deviation_ = weight_ref_deviation;
56  }
57 
59  const double weight_curvature_constraint_slack_var) {
60  weight_curvature_constraint_slack_var_ =
61  weight_curvature_constraint_slack_var;
62  }
63 
64  void set_curvature_constraint(const double curvature_constraint) {
65  curvature_constraint_ = curvature_constraint;
66  }
67 
68  void set_max_iter(const int max_iter) { max_iter_ = max_iter; }
69 
70  void set_time_limit(const double time_limit) { time_limit_ = time_limit; }
71 
72  void set_verbose(const bool verbose) { verbose_ = verbose; }
73 
74  void set_scaled_termination(const bool scaled_termination) {
75  scaled_termination_ = scaled_termination;
76  }
77 
78  void set_warm_start(const bool warm_start) { warm_start_ = warm_start; }
79 
80  void set_sqp_pen_max_iter(const int sqp_pen_max_iter) {
81  sqp_pen_max_iter_ = sqp_pen_max_iter;
82  }
83 
84  void set_sqp_ftol(const double sqp_ftol) { sqp_ftol_ = sqp_ftol; }
85 
86  void set_sqp_sub_max_iter(const int sqp_sub_max_iter) {
87  sqp_sub_max_iter_ = sqp_sub_max_iter;
88  }
89 
90  void set_sqp_ctol(const double sqp_ctol) { sqp_ctol_ = sqp_ctol; }
91 
92  bool Solve();
93 
94  const std::vector<std::pair<double, double>>& opt_xy() const {
95  return opt_xy_;
96  }
97 
98  private:
99  void CalculateKernel(std::vector<c_float>* P_data,
100  std::vector<c_int>* P_indices,
101  std::vector<c_int>* P_indptr);
102 
103  void CalculateOffset(std::vector<c_float>* q);
104 
105  std::vector<double> CalculateLinearizedFemPosParams(
106  const std::vector<std::pair<double, double>>& points, const size_t index);
107 
108  void CalculateAffineConstraint(
109  const std::vector<std::pair<double, double>>& points,
110  std::vector<c_float>* A_data, std::vector<c_int>* A_indices,
111  std::vector<c_int>* A_indptr, std::vector<c_float>* lower_bounds,
112  std::vector<c_float>* upper_bounds);
113 
114  void SetPrimalWarmStart(const std::vector<std::pair<double, double>>& points,
115  std::vector<c_float>* primal_warm_start);
116 
117  bool OptimizeWithOsqp(const std::vector<c_float>& primal_warm_start,
118  OSQPWorkspace** work);
119 
120  double CalculateConstraintViolation(
121  const std::vector<std::pair<double, double>>& points);
122 
123  private:
124  // Init states and constraints
125  std::vector<std::pair<double, double>> ref_points_;
126  std::vector<double> bounds_around_refs_;
127  double curvature_constraint_ = 0.2;
128 
129  // Weights in optimization cost function
130  double weight_fem_pos_deviation_ = 1.0e5;
131  double weight_path_length_ = 1.0;
132  double weight_ref_deviation_ = 1.0;
133  double weight_curvature_constraint_slack_var_ = 1.0e5;
134 
135  // Settings of osqp
136  int max_iter_ = 4000;
137  double time_limit_ = 0.0;
138  bool verbose_ = false;
139  bool scaled_termination_ = true;
140  bool warm_start_ = true;
141 
142  // Settings of sqp
143  int sqp_pen_max_iter_ = 100;
144  double sqp_ftol_ = 1e-2;
145  int sqp_sub_max_iter_ = 100;
146  double sqp_ctol_ = 1e-2;
147 
148  // Optimization problem definitions
149  int num_of_points_ = 0;
150  int num_of_pos_variables_ = 0;
151  int num_of_slack_variables_ = 0;
152  int num_of_variables_ = 0;
153  int num_of_variable_constraints_ = 0;
154  int num_of_curvature_constraints_ = 0;
155  int num_of_constraints_ = 0;
156 
157  // Optimized_result
158  std::vector<std::pair<double, double>> opt_xy_;
159  std::vector<double> slack_;
160  double average_interval_length_ = 0.0;
161 };
162 } // namespace planning
163 } // namespace apollo
void set_sqp_ctol(const double sqp_ctol)
Definition: fem_pos_deviation_sqp_osqp_interface.h:90
void set_curvature_constraint(const double curvature_constraint)
Definition: fem_pos_deviation_sqp_osqp_interface.h:64
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void set_max_iter(const int max_iter)
Definition: fem_pos_deviation_sqp_osqp_interface.h:68
void set_time_limit(const double time_limit)
Definition: fem_pos_deviation_sqp_osqp_interface.h:70
void set_warm_start(const bool warm_start)
Definition: fem_pos_deviation_sqp_osqp_interface.h:78
Planning module main class. It processes GPS and IMU as input, to generate planning info...
void set_weight_path_length(const double weight_path_length)
Definition: fem_pos_deviation_sqp_osqp_interface.h:50
void set_sqp_sub_max_iter(const int sqp_sub_max_iter)
Definition: fem_pos_deviation_sqp_osqp_interface.h:86
void set_ref_points(const std::vector< std::pair< double, double >> &ref_points)
Definition: fem_pos_deviation_sqp_osqp_interface.h:37
void set_bounds_around_refs(const std::vector< double > &bounds_around_refs)
Definition: fem_pos_deviation_sqp_osqp_interface.h:42
const std::vector< std::pair< double, double > > & opt_xy() const
Definition: fem_pos_deviation_sqp_osqp_interface.h:94
void set_verbose(const bool verbose)
Definition: fem_pos_deviation_sqp_osqp_interface.h:72
Definition: fem_pos_deviation_sqp_osqp_interface.h:31
void set_weight_curvature_constraint_slack_var(const double weight_curvature_constraint_slack_var)
Definition: fem_pos_deviation_sqp_osqp_interface.h:58
void set_weight_ref_deviation(const double weight_ref_deviation)
Definition: fem_pos_deviation_sqp_osqp_interface.h:54
void set_weight_fem_pos_deviation(const double weight_fem_pos_deviation)
Definition: fem_pos_deviation_sqp_osqp_interface.h:46
void set_scaled_termination(const bool scaled_termination)
Definition: fem_pos_deviation_sqp_osqp_interface.h:74
void set_sqp_ftol(const double sqp_ftol)
Definition: fem_pos_deviation_sqp_osqp_interface.h:84
void set_sqp_pen_max_iter(const int sqp_pen_max_iter)
Definition: fem_pos_deviation_sqp_osqp_interface.h:80