Autoware.Auto
osqp_interface.hpp
Go to the documentation of this file.
1 // Copyright 2021 The Autoware Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef OSQP_INTERFACE__OSQP_INTERFACE_HPP_
16 #define OSQP_INTERFACE__OSQP_INTERFACE_HPP_
17 
18 #include <limits>
19 #include <memory>
20 #include <string>
21 #include <tuple>
22 #include <vector>
23 
24 #include "common/types.hpp"
25 #include "eigen3/Eigen/Core"
26 #include "osqp/osqp.h"
29 
30 
31 namespace autoware
32 {
33 namespace common
34 {
35 namespace osqp
36 {
37 constexpr c_float INF = OSQP_INFTY;
40 
45 class OSQP_INTERFACE_PUBLIC OSQPInterface
46 {
47 private:
48  std::unique_ptr<OSQPWorkspace, std::function<void(OSQPWorkspace *)>> m_work;
49  std::unique_ptr<OSQPSettings> m_settings;
50  std::unique_ptr<OSQPData> m_data;
51  // store last work info since work is cleaned up at every execution to prevent memory leak.
52  OSQPInfo m_latest_work_info;
53  // Number of parameters to optimize
54  int64_t m_param_n;
55  // Flag to check if the current work exists
56  bool8_t m_work_initialized = false;
57  // Exitflag
58  int64_t m_exitflag;
59 
60  // Runs the solver on the stored problem.
61  std::tuple<std::vector<float64_t>, std::vector<float64_t>, int64_t, int64_t, int64_t> solve();
62 
63  static void OSQPWorkspaceDeleter(OSQPWorkspace * ptr) noexcept;
64 
65 public:
67  explicit OSQPInterface(
68  const c_float eps_abs = std::numeric_limits<c_float>::epsilon(), const bool8_t polish = true);
77  const Eigen::MatrixXd & P, const Eigen::MatrixXd & A, const std::vector<float64_t> & q,
78  const std::vector<float64_t> & l, const std::vector<float64_t> & u, const c_float eps_abs);
80  const CSC_Matrix & P, const CSC_Matrix & A, const std::vector<float64_t> & q,
81  const std::vector<float64_t> & l, const std::vector<float64_t> & u, const c_float eps_abs);
82 
83  /****************
84  * OPTIMIZATION
85  ****************/
87  //
92 
104  std::tuple<std::vector<float64_t>, std::vector<float64_t>, int64_t, int64_t, int64_t> optimize();
105 
122  std::tuple<std::vector<float64_t>, std::vector<float64_t>, int64_t, int64_t, int64_t> optimize(
123  const Eigen::MatrixXd & P, const Eigen::MatrixXd & A, const std::vector<float64_t> & q,
124  const std::vector<float64_t> & l, const std::vector<float64_t> & u);
125 
132  int64_t initializeProblem(
133  const Eigen::MatrixXd & P, const Eigen::MatrixXd & A, const std::vector<float64_t> & q,
134  const std::vector<float64_t> & l, const std::vector<float64_t> & u);
135  int64_t initializeProblem(
136  CSC_Matrix P, CSC_Matrix A, const std::vector<float64_t> & q,
137  const std::vector<float64_t> & l, const std::vector<float64_t> & u);
138 
139  // Updates problem parameters while keeping solution in memory.
140  //
141  // Args:
142  // P_new: (n,n) matrix defining relations between parameters.
143  // A_new: (m,n) matrix defining parameter constraints relative to the lower and upper bound.
144  // q_new: (n) vector defining the linear cost of the problem.
145  // l_new: (m) vector defining the lower bound problem constraint.
146  // u_new: (m) vector defining the upper bound problem constraint.
147  void updateP(const Eigen::MatrixXd & P_new);
148  void updateCscP(const CSC_Matrix & P_csc);
149  void updateA(const Eigen::MatrixXd & A_new);
150  void updateCscA(const CSC_Matrix & A_csc);
151  void updateQ(const std::vector<double> & q_new);
152  void updateL(const std::vector<double> & l_new);
153  void updateU(const std::vector<double> & u_new);
154  void updateBounds(const std::vector<double> & l_new, const std::vector<double> & u_new);
155  void updateEpsAbs(const double eps_abs);
156  void updateEpsRel(const double eps_rel);
157  void updateMaxIter(const int iter);
158  void updateVerbose(const bool verbose);
159  void updateRhoInterval(const int rho_interval);
160  void updateRho(const double rho);
161  void updateAlpha(const double alpha);
162 
164  inline int64_t getTakenIter() const {return static_cast<int64_t>(m_latest_work_info.iter);}
166  inline std::string getStatusMessage() const
167  {
168  return static_cast<std::string>(m_latest_work_info.status);
169  }
171  inline int64_t getStatus() const {return static_cast<int64_t>(m_latest_work_info.status_val);}
173  inline int64_t getStatusPolish() const
174  {
175  return static_cast<int64_t>(m_latest_work_info.status_polish);
176  }
178  inline float64_t getRunTime() const {return m_latest_work_info.run_time;}
180  inline float64_t getObjVal() const {return m_latest_work_info.obj_val;}
182  inline int64_t getExitFlag() const {return m_exitflag;}
183 };
184 
185 } // namespace osqp
186 } // namespace common
187 } // namespace autoware
188 
189 #endif // OSQP_INTERFACE__OSQP_INTERFACE_HPP_
autoware::common::osqp::OSQPInterface::getRunTime
float64_t getRunTime() const
Get the runtime of the latest problem solved.
Definition: osqp_interface.hpp:178
autoware::common::osqp::OSQPInterface::getTakenIter
int64_t getTakenIter() const
Get the number of iteration taken to solve the problem.
Definition: osqp_interface.hpp:164
types.hpp
This file includes common type definition.
u
DifferentialState u
Definition: kinematic_bicycle.snippet.hpp:29
autoware::common::osqp::INF
constexpr c_float INF
Definition: osqp_interface.hpp:37
visibility_control.hpp
autoware::common::types::bool8_t
bool bool8_t
Definition: types.hpp:39
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
autoware::common::osqp::OSQPInterface
Definition: osqp_interface.hpp:45
autoware::common::osqp::OSQPInterface::getObjVal
float64_t getObjVal() const
Get the objective value the latest problem solved.
Definition: osqp_interface.hpp:180
autoware::common::osqp::OSQPInterface::getStatusPolish
int64_t getStatusPolish() const
Get the status polish for the latest problem solved.
Definition: osqp_interface.hpp:173
autoware::common::osqp::OSQPInterface::getStatusMessage
std::string getStatusMessage() const
Get the status message for the latest problem solved.
Definition: osqp_interface.hpp:166
autoware::common::osqp::CSC_Matrix
Compressed-Column-Sparse Matrix.
Definition: csc_matrix_conv.hpp:31
autoware::common::osqp::OSQPInterface::getStatus
int64_t getStatus() const
Get the status value for the latest problem solved.
Definition: osqp_interface.hpp:171
csc_matrix_conv.hpp
autoware::common::types::float64_t
double float64_t
Definition: types.hpp:47
autoware::common::osqp::OSQPInterface::getExitFlag
int64_t getExitFlag() const
Returns flag asserting interface condition (Healthy condition: 0).
Definition: osqp_interface.hpp:182