qp_solver_osqp.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_SOLVER_QP_SOLVER_OSQP_H_
26 #define SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_SOLVER_QP_SOLVER_OSQP_H_
27 
29 
30 #include <memory>
31 
32 #ifdef OSQP
33 #include <osqp.h>
34 #endif
35 
36 namespace corbo {
37 
38 #ifdef OSQP
39 
47 class SolverOsqp : public QpSolverInterface
48 {
49  public:
50  using Ptr = std::shared_ptr<SolverOsqp>;
51  using UPtr = std::unique_ptr<SolverOsqp>;
52 
53  // Default constructor
54  SolverOsqp();
55 
56  // Destructor
57  ~SolverOsqp();
58 
59  // implements interface method
60  QpSolverInterface::Ptr getInstance() const override { return std::make_shared<SolverOsqp>(); }
61 
62  // implements interface method
63  bool initialize() override;
64 
65  // implements interface method
66  bool isSupportingSimpleBounds() override { return false; }
67 
68  // implements interface method
71  bool zero_x_warmstart = false) override;
72 
73  // implements interface method
75  Eigen::Ref<Eigen::VectorXd> ubA, bool new_structure = true, bool zero_x_warmstart = false, bool update_P = true,
76  bool update_q = true, bool update_A = true, bool update_bounds = true) override;
77 
80 
83 
84  void enforceNewStructure(bool new_structure = true) override { _force_new_structure = new_structure; }
85 
88  OSQPSettings* getOsqpSettings() { return _settings.get(); }
90 
91  void clear() override;
92 
93 #ifdef MESSAGE_SUPPORT
94  void toMessage(corbo::messages::SolverOsqp& message) const;
97  void fromMessage(const corbo::messages::SolverOsqp& message, std::stringstream* issues = nullptr);
98 
99  // implements interface method
100  void toMessage(corbo::messages::QpSolver& message) const override { toMessage(*message.mutable_solver_osqp()); }
101  // implements interface method
102  void fromMessage(const corbo::messages::QpSolver& message, std::stringstream* issues = nullptr) override
103  {
104  fromMessage(message.solver_osqp(), issues);
105  }
106 #endif
107 
108  protected:
109  SolverStatus convertOsqpExitFlagToSolverStatus(c_int status) const;
110 
111  private:
112  std::unique_ptr<OSQPSettings> _settings;
113  // std::unique_ptr<OSQPData> _data;
114  OSQPWorkspace* _work = nullptr;
115 
116  Eigen::VectorXd _zero;
117 
118  bool _force_new_structure = false;
119 
120  bool _initialized = false;
121 };
122 
123 FACTORY_REGISTER_QP_SOLVER(SolverOsqp)
124 
125 #else // OSQP
126 
135 {
136  public:
137  using Ptr = std::shared_ptr<SolverOsqp>;
138  using UPtr = std::unique_ptr<SolverOsqp>;
139 
140  // implements interface method
141  QpSolverInterface::Ptr getInstance() const override { return std::make_shared<SolverOsqp>(); }
142 
143  // implements interface method
144  bool isSupportingSimpleBounds() override { return false; }
145 
146  // implements interface method
149  bool zero_x_warmstart = false) override
150  {
151  printWarning();
152  return SolverStatus::Error;
153  }
154 
155  // implements interface method
157  Eigen::Ref<Eigen::VectorXd> ubA, bool new_structure = true, bool zero_x_warmstart = false, bool update_P = true,
158  bool update_q = true, bool update_A = true, bool update_bounds = true) override
159  {
160  printWarning();
161  return SolverStatus::Error;
162  }
163 
166 
169 
170  void enforceNewStructure(bool new_structure = true) override { printWarning(); }
171 
172  void clear() override {}
173 
174 #ifdef MESSAGE_SUPPORT
175  // implements interface method
176  void toMessage(corbo::messages::QpSolver& /*message*/) const override { printWarning(); }
177  // implements interface method
178  void fromMessage(const corbo::messages::QpSolver& /*message*/, std::stringstream* issues) override
179  {
180  if (issues) *issues << "SolverOsqp cannot be selected since it is not installed properly." << std::endl;
181  printWarning();
182  }
183 #endif
184 
185  protected:
186  void printWarning() const { PRINT_WARNING("SolverOsqp cannot be selected since it is not installed properly."); }
187 
188  private:
189  Eigen::VectorXd _dummy;
190 };
191 
192 #endif // OSQP
193 
194 } // namespace corbo
195 
196 #endif // SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_SOLVER_QP_SOLVER_OSQP_H_
SolverStatus solve(SparseMatrix &P, Eigen::Ref< Eigen::VectorXd > q, SparseMatrix &A, Eigen::Ref< Eigen::VectorXd > lbA, Eigen::Ref< Eigen::VectorXd > ubA, Eigen::Ref< Eigen::VectorXd > lb, Eigen::Ref< Eigen::VectorXd > ub, bool new_structure=true, bool zero_x_warmstart=false) override
Solve full QP.
std::shared_ptr< SolverOsqp > Ptr
Interface to the external solver OSQP.
Scalar * x
void clear() override
clear internal caches
Scalar * y
void updatePrimalSolutionWarmStart(const Eigen::Ref< const Eigen::VectorXd > &x) override
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
#define PRINT_WARNING(msg)
Print msg-stream.
Definition: console.h:145
Eigen::SparseMatrix< double, Eigen::ColMajor, long long > SparseMatrix
bool isSupportingSimpleBounds() override
Generic interface for QP solver implementations.
#define FACTORY_REGISTER_QP_SOLVER(type)
Eigen::VectorXd _dummy
MatrixType A(a, *n, *n, *lda)
void printWarning() const
Eigen::Ref< Eigen::VectorXd > getDualSolution() override
std::unique_ptr< SolverOsqp > UPtr
virtual bool initialize()
Initialize the qp solver.
SolverStatus solve(SparseMatrix &P, Eigen::Ref< Eigen::VectorXd > q, SparseMatrix &A, Eigen::Ref< Eigen::VectorXd > lbA, Eigen::Ref< Eigen::VectorXd > ubA, bool new_structure=true, bool zero_x_warmstart=false, bool update_P=true, bool update_q=true, bool update_A=true, bool update_bounds=true) override
Solve QP without simple bounds.
EIGEN_DEVICE_FUNC const Scalar & q
Eigen::Ref< Eigen::VectorXd > getPrimalSolution() override
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
QpSolverInterface::Ptr getInstance() const override
Return a newly created instance of the current solver.
std::shared_ptr< QpSolverInterface > Ptr
void updateDualSolutionWarmStart(const Eigen::Ref< const Eigen::VectorXd > &y) override
void enforceNewStructure(bool new_structure=true) override
std::unique_ptr< QpSolverInterface > UPtr


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:14