-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
410 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ endif() | |
if(SOLVER_PROXQP) | ||
add_subdirectory(proxqp) | ||
endif() | ||
if(SOLVER_OSQP) | ||
add_subdirectory(osqp) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
SET(TARGET_NAME wbc-solvers-osqp) | ||
|
||
find_package(OsqpEigen REQUIRED) | ||
|
||
set(SOURCES OsqpSolver.cpp) | ||
set(HEADERS OsqpSolver.hpp) | ||
|
||
list(APPEND PKGCONFIG_REQUIRES wbc-core) | ||
string (REPLACE ";" " " PKGCONFIG_REQUIRES "${PKGCONFIG_REQUIRES}") | ||
|
||
add_library(${TARGET_NAME} SHARED ${SOURCES} ${HEADERS}) | ||
target_link_libraries(${TARGET_NAME} PUBLIC | ||
wbc-core) | ||
target_link_libraries(${TARGET_NAME} PUBLIC OsqpEigen::OsqpEigen) | ||
|
||
set_target_properties(${TARGET_NAME} PROPERTIES | ||
VERSION ${PROJECT_VERSION} | ||
SOVERSION ${API_VERSION}) | ||
|
||
install(TARGETS ${TARGET_NAME} | ||
LIBRARY DESTINATION lib) | ||
|
||
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_NAME}.pc.in ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.pc @ONLY) | ||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.pc DESTINATION lib/pkgconfig) | ||
INSTALL(FILES ${HEADERS} DESTINATION include/${PROJECT_NAME}/solvers/osqp) | ||
|
||
add_subdirectory(test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include "OsqpSolver.hpp" | ||
#include "../../core/QuadraticProgram.hpp" | ||
|
||
namespace wbc { | ||
|
||
OsqpSolver::OsqpSolver() : configured(false){ | ||
} | ||
|
||
OsqpSolver::~OsqpSolver(){ | ||
} | ||
|
||
void OsqpSolver::solve(const HierarchicalQP& hierarchical_qp, base::VectorXd& solver_output){ | ||
|
||
if(hierarchical_qp.size() != 1) | ||
throw std::runtime_error("OsqpSolver::solve: Constraints vector size must be 1 for the current implementation"); | ||
|
||
const QuadraticProgram& qp = hierarchical_qp[0]; | ||
uint nc = qp.bounded ? qp.neq + qp.nin + qp.nq : qp.neq + qp.nin; | ||
|
||
if(!configured){ | ||
hessian_sparse.resize(qp.nq,qp.nq); | ||
gradient.resize(qp.nq); | ||
constraint_mat_dense.resize(nc,qp.nq); | ||
constraint_mat_dense.setZero(); | ||
constraint_mat_sparse.resize(nc,qp.nq); | ||
lower_bound.resize(nc); | ||
upper_bound.resize(nc); | ||
lower_bound.setZero(); | ||
upper_bound.setZero(); | ||
hessian_sparse.setZero(); | ||
gradient.setZero(); | ||
constraint_mat_sparse.setZero(); | ||
solver.settings()->setVerbosity(false); | ||
solver.settings()->setWarmStart(true); | ||
solver.data()->setNumberOfVariables(qp.nq); | ||
configured = true; | ||
solver.data()->setNumberOfConstraints(nc); | ||
solver.data()->setHessianMatrix(hessian_sparse); | ||
solver.data()->setGradient(gradient); | ||
solver.data()->setLinearConstraintsMatrix(constraint_mat_sparse); | ||
solver.data()->setLowerBound(lower_bound); | ||
solver.data()->setUpperBound(upper_bound); | ||
solver.initSolver(); | ||
} | ||
|
||
|
||
if(solver.data()->getData()->m != nc){ | ||
solver.data()->setNumberOfConstraints(nc); | ||
solver.data()->clearLinearConstraintsMatrix(); | ||
solver.data()->clearHessianMatrix(); | ||
hessian_sparse.resize(qp.nq,qp.nq); | ||
gradient.resize(qp.nq); | ||
constraint_mat_dense.resize(nc,qp.nq); | ||
constraint_mat_dense.setZero(); | ||
constraint_mat_sparse.resize(nc,qp.nq); | ||
lower_bound.resize(nc); | ||
upper_bound.resize(nc); | ||
lower_bound.setZero(); | ||
upper_bound.setZero(); | ||
hessian_sparse.setZero(); | ||
gradient.setZero(); | ||
constraint_mat_sparse.setZero(); | ||
solver.settings()->setVerbosity(false); | ||
solver.settings()->setWarmStart(true); | ||
solver.data()->setNumberOfVariables(qp.nq); | ||
solver.data()->setNumberOfConstraints(nc); | ||
solver.data()->setHessianMatrix(hessian_sparse); | ||
solver.data()->setGradient(gradient); | ||
solver.data()->setLinearConstraintsMatrix(constraint_mat_sparse); | ||
solver.data()->setLowerBound(lower_bound); | ||
solver.data()->setUpperBound(upper_bound); | ||
} | ||
|
||
// OSQP treats bounds, eq. and ineq. constraints in one constraint matrix / vector, so we have to merge: | ||
if(qp.neq != 0){ // Has equality constraints | ||
constraint_mat_dense.middleRows(0,qp.neq) = qp.A; | ||
lower_bound.segment(0,qp.neq) = qp.b; | ||
upper_bound.segment(0,qp.neq) = qp.b; | ||
} | ||
if(qp.C.rows() != 0){ // Has inequality constraints | ||
constraint_mat_dense.middleRows(qp.neq,qp.nin) = qp.C; | ||
lower_bound.segment(qp.neq,qp.nin) = qp.lower_y; | ||
upper_bound.segment(qp.neq,qp.nin) = qp.upper_y; | ||
} | ||
if(qp.bounded){ // Has bounds | ||
constraint_mat_dense.middleRows(qp.neq+qp.nin,qp.nq).diagonal().setConstant(1.0); | ||
lower_bound.segment(qp.neq+qp.nin,qp.nq) = qp.lower_x; | ||
upper_bound.segment(qp.neq+qp.nin,qp.nq) = qp.upper_x; | ||
} | ||
|
||
constraint_mat_sparse = constraint_mat_dense.sparseView(); | ||
hessian_dense = qp.H; | ||
hessian_sparse = hessian_dense.sparseView(); | ||
gradient = qp.g; | ||
solver.updateHessianMatrix(hessian_sparse); | ||
solver.updateLinearConstraintsMatrix(constraint_mat_sparse); | ||
solver.updateGradient(gradient); | ||
solver.updateBounds(lower_bound,upper_bound); | ||
|
||
OsqpEigen::ErrorExitFlag flag = solver.solveProblem(); | ||
if(flag != OsqpEigen::ErrorExitFlag::NoError){ | ||
qp.print(); | ||
throw std::runtime_error("Error solving QP: " + exitFlagToString(flag)); | ||
} | ||
solver_output = solver.getSolution(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef WBC_OSQP_SOLVER_HPP | ||
#define WBC_OSQP_SOLVER_HPP | ||
|
||
#include <Eigen/Sparse> | ||
#include <OsqpEigen/Solver.hpp> | ||
#include "../../core/QPSolver.hpp" | ||
#include "../../core/QuadraticProgram.hpp" | ||
|
||
namespace wbc { | ||
|
||
class OsqpSolver : public QPSolver{ | ||
public: | ||
OsqpSolver(); | ||
~OsqpSolver(); | ||
|
||
virtual void solve(const HierarchicalQP& hierarchical_qp, base::VectorXd& solver_output); | ||
|
||
protected: | ||
bool configured; | ||
OsqpEigen::Solver solver; | ||
|
||
Eigen::MatrixXd hessian_dense; | ||
Eigen::SparseMatrix<double> hessian_sparse; | ||
Eigen::MatrixXd constraint_mat_dense; | ||
Eigen::SparseMatrix<double> constraint_mat_sparse; | ||
Eigen::VectorXd gradient; | ||
Eigen::VectorXd lower_bound; | ||
Eigen::VectorXd upper_bound; | ||
|
||
void resetData(uint nq, uint nc); | ||
std::string exitFlagToString(OsqpEigen::ErrorExitFlag flag){ | ||
switch(flag){ | ||
case OsqpEigen::ErrorExitFlag::DataValidationError: return "DataValidationError"; | ||
case OsqpEigen::ErrorExitFlag::SettingsValidationError: return "SettingsValidationError"; | ||
case OsqpEigen::ErrorExitFlag::LinsysSolverLoadError: return "LinsysSolverLoadError"; | ||
case OsqpEigen::ErrorExitFlag::LinsysSolverInitError: return "LinsysSolverInitError"; | ||
case OsqpEigen::ErrorExitFlag::NonCvxError: return "NonCvxError"; | ||
case OsqpEigen::ErrorExitFlag::MemAllocError: return "MemAllocError"; | ||
case OsqpEigen::ErrorExitFlag::WorkspaceNotInitError: return "WorkspaceNotInitError"; | ||
default: return "NoError"; | ||
} | ||
} | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
add_executable(test_osqp_solver test_osqp_solver.cpp) | ||
target_link_libraries(test_osqp_solver | ||
wbc-solvers-osqp | ||
Boost::unit_test_framework) |
Oops, something went wrong.