Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for enabling/disabling debug error output to cerr #75

Merged
merged 5 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ endif()
option(BUILD_TESTING "Create tests using CMake" OFF)
include(CTest)

option(DEBUG_OUTPUT "Print debug error messages to cerr" ON)
if(DEBUG_OUTPUT)
add_compile_definitions("DEBUG_OUTPUT")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there may be two problems with this definition:

  • It is not prefixed by any OSQP_EIGEN_ or similar prefix, so it could be prone to macro name collision, so probably it make sense to give to it a less generic name.
  • To avoid confusion for which target this compile definition is valid, I would suggest to use target_compile_definitions instead of add_compile_definitions

endif()

# Check OsqpEigen dependencies, find necessary libraries.
include(OsqpEigenDependencies)

Expand All @@ -78,7 +83,8 @@ set(LIBRARY_TARGET_NAME OsqpEigen)
set(${LIBRARY_TARGET_NAME}_SRC
src/Data.cpp
src/Settings.cpp
src/Solver.cpp)
src/Solver.cpp
src/Debug.cpp)
S-Dafarra marked this conversation as resolved.
Show resolved Hide resolved

set(${LIBRARY_TARGET_NAME}_HDR
include/OsqpEigen/OsqpEigen.h
Expand Down
20 changes: 11 additions & 9 deletions include/OsqpEigen/Data.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@

#include <iostream>

#include "Debug.hpp"
S-Dafarra marked this conversation as resolved.
Show resolved Hide resolved

template<typename Derived>
bool OsqpEigen::Data::setHessianMatrix(const Eigen::SparseCompressedBase<Derived> &hessianMatrix)
{
if(m_isHessianMatrixSet){
std::cerr << "[OsqpEigen::Data::setHessianMatrix] The hessian matrix was already set. "
debugStream << "[OsqpEigen::Data::setHessianMatrix] The hessian matrix was already set. "
<< "Please use clearHessianMatrix() method to deallocate memory."
<< std::endl;
return false;
}

if(!m_isNumberOfVariablesSet){
std::cerr << "[OsqpEigen::Data::setHessianMatrix] Please set the number of variables before "
debugStream << "[OsqpEigen::Data::setHessianMatrix] Please set the number of variables before "
<< "add the hessian matrix."
<< std::endl;
return false;
}

// check if the number of row and columns are equal to the number of the optimization variables
if ((hessianMatrix.rows() != m_data->n) || (hessianMatrix.cols()!= m_data->n)){
std::cerr << "[OsqpEigen::Data::setHessianMatrix] The Hessian matrix has to be a n x n size matrix."
debugStream << "[OsqpEigen::Data::setHessianMatrix] The Hessian matrix has to be a n x n size matrix."
<< std::endl;
return false;
}
Expand All @@ -35,7 +37,7 @@ bool OsqpEigen::Data::setHessianMatrix(const Eigen::SparseCompressedBase<Derived
// osqp 0.6.0 required only the upper triangular part of the hessian matrix
Derived hessianMatrixUpperTriangular = hessianMatrix.template triangularView<Eigen::Upper>();
if(!OsqpEigen::SparseMatrixHelper::createOsqpSparseMatrix(hessianMatrixUpperTriangular, m_data->P)){
std::cerr << "[OsqpEigen::Data::setHessianMatrix] Unable to instantiate the osqp sparse matrix."
debugStream << "[OsqpEigen::Data::setHessianMatrix] Unable to instantiate the osqp sparse matrix."
<< std::endl;
return false;
}
Expand All @@ -47,34 +49,34 @@ template<typename Derived>
bool OsqpEigen::Data::setLinearConstraintsMatrix(const Eigen::SparseCompressedBase<Derived> &linearConstraintsMatrix)
{
if(m_isLinearConstraintsMatrixSet){
std::cerr << "[OsqpEigen::Data::setLinearConstraintsMatrix] The linear constraint matrix was already set. "
debugStream << "[OsqpEigen::Data::setLinearConstraintsMatrix] The linear constraint matrix was already set. "
<< "Please use clearLinearConstraintsMatrix() method to deallocate memory."
<< std::endl;
return false;
}

if(!m_isNumberOfConstraintsSet){
std::cerr << "[OsqpEigen::Data::setLinearConstraintsMatrix] Please set the number of constraints before add the constraint matrix."
debugStream << "[OsqpEigen::Data::setLinearConstraintsMatrix] Please set the number of constraints before add the constraint matrix."
<< std::endl;
return false;
}

if(!m_isNumberOfVariablesSet){
std::cerr << "[OsqpEigen::Data::setLinearConstraintsMatrix] Please set the number of variables before add the constraint matrix."
debugStream << "[OsqpEigen::Data::setLinearConstraintsMatrix] Please set the number of variables before add the constraint matrix."
<< std::endl;
return false;
}

if ((linearConstraintsMatrix.rows() != m_data->m) || (linearConstraintsMatrix.cols()!= m_data->n)){
std::cerr << "[OsqpEigen::Data::setLinearConstraintsMatrix] The Linear constraints matrix has to be a m x n size matrix."
debugStream << "[OsqpEigen::Data::setLinearConstraintsMatrix] The Linear constraints matrix has to be a m x n size matrix."
<< std::endl;
return false;
}

// set the hessian matrix
if(!OsqpEigen::SparseMatrixHelper::createOsqpSparseMatrix(linearConstraintsMatrix,
m_data->A)){
std::cerr << "[OsqpEigen::Data::setLinearConstraintsMatrix] osqp sparse matrix not created."
debugStream << "[OsqpEigen::Data::setLinearConstraintsMatrix] osqp sparse matrix not created."
<< std::endl;
return false;
}
Expand Down
10 changes: 10 additions & 0 deletions include/OsqpEigen/Debug.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef OSQPEIGEN_DEBUG_HPP
#define OSQPEIGEN_DEBUG_HPP

#include <iostream>

namespace OsqpEigen {
extern std::ostream &debugStream;
}

#endif /* OSQPEIGEN_DEBUG_HPP */
58 changes: 30 additions & 28 deletions include/OsqpEigen/Solver.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
#include <auxil.h>
#include <scaling.h>

#include "Debug.hpp"

template<typename Derived>
bool OsqpEigen::Solver::updateHessianMatrix(const Eigen::SparseCompressedBase<Derived> &hessianMatrix)
{
if(!m_isSolverInitialized){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] The solver has not been initialized."
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] The solver has not been initialized."
<< std::endl;
return false;
}

if(((c_int)hessianMatrix.rows() != m_workspace->data->n)||
((c_int)hessianMatrix.cols() != m_workspace->data->n)){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] The hessian matrix has to be a nxn matrix"
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] The hessian matrix has to be a nxn matrix"
<< std::endl;
return false;
}
Expand All @@ -29,13 +31,13 @@ bool OsqpEigen::Solver::updateHessianMatrix(const Eigen::SparseCompressedBase<De
// evaluate the triplets from old and new hessian sparse matrices
if(!OsqpEigen::SparseMatrixHelper::osqpSparseMatrixToTriplets(m_workspace->data->P,
m_oldHessianTriplet)){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] Unable to evaluate triplets from the old hessian matrix."
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] Unable to evaluate triplets from the old hessian matrix."
<< std::endl;
return false;
}
if(!OsqpEigen::SparseMatrixHelper::eigenSparseMatrixToTriplets(hessianMatrix,
m_newHessianTriplet)){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] Unable to evaluate triplets from the old hessian matrix."
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] Unable to evaluate triplets from the old hessian matrix."
<< std::endl;
return false;
}
Expand All @@ -50,7 +52,7 @@ bool OsqpEigen::Solver::updateHessianMatrix(const Eigen::SparseCompressedBase<De
m_hessianNewIndices, m_hessianNewValues)){
if (m_hessianNewValues.size() > 0) {
if(osqp_update_P(m_workspace.get(), m_hessianNewValues.data(), m_hessianNewIndices.data(), m_hessianNewIndices.size()) != 0){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] Unable to update hessian matrix."
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] Unable to update hessian matrix."
<< std::endl;
return false;
}
Expand All @@ -63,13 +65,13 @@ bool OsqpEigen::Solver::updateHessianMatrix(const Eigen::SparseCompressedBase<De
// get the primal and the dual variables

if(!getPrimalVariable(m_primalVariables)){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] Unable to get the primal variable."
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] Unable to get the primal variable."
<< std::endl;
return false;
}

if(!getDualVariable(m_dualVariables)){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] Unable to get the dual variable."
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] Unable to get the dual variable."
<< std::endl;
return false;
}
Expand All @@ -79,7 +81,7 @@ bool OsqpEigen::Solver::updateHessianMatrix(const Eigen::SparseCompressedBase<De

// set new hessian matrix
if(!m_data->setHessianMatrix(hessianMatrix)){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] Unable to update the hessian matrix in "
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] Unable to update the hessian matrix in "
<< "OptimizaroData object."
<< std::endl;
return false;
Expand All @@ -90,20 +92,20 @@ bool OsqpEigen::Solver::updateHessianMatrix(const Eigen::SparseCompressedBase<De

// initialize a new solver
if(!initSolver()){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] Unable to Initialize the solver."
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] Unable to Initialize the solver."
<< std::endl;
return false;
}

// set the old primal and dual variables
if(!setPrimalVariable(m_primalVariables)){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] Unable to set the primal variable."
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] Unable to set the primal variable."
<< std::endl;
return false;
}

if(!setDualVariable(m_dualVariables)){
std::cerr << "[OsqpEigen::Solver::updateHessianMatrix] Unable to set the dual variable."
debugStream << "[OsqpEigen::Solver::updateHessianMatrix] Unable to set the dual variable."
<< std::endl;
return false;
}
Expand All @@ -115,14 +117,14 @@ template<typename Derived>
bool OsqpEigen::Solver::updateLinearConstraintsMatrix(const Eigen::SparseCompressedBase<Derived> &linearConstraintsMatrix)
{
if(!m_isSolverInitialized){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] The solver has not been initialized."
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] The solver has not been initialized."
<< std::endl;
return false;
}

if(((c_int)linearConstraintsMatrix.rows() != m_workspace->data->m)||
((c_int)linearConstraintsMatrix.cols() != m_workspace->data->n)){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] The constraints matrix has to be a mxn matrix"
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] The constraints matrix has to be a mxn matrix"
<< std::endl;
return false;
}
Expand All @@ -131,13 +133,13 @@ bool OsqpEigen::Solver::updateLinearConstraintsMatrix(const Eigen::SparseCompres

if(!OsqpEigen::SparseMatrixHelper::osqpSparseMatrixToTriplets(m_workspace->data->A,
m_oldLinearConstraintsTriplet)){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to evaluate triplets from the old hessian matrix."
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to evaluate triplets from the old hessian matrix."
<< std::endl;
return false;
}
if(!OsqpEigen::SparseMatrixHelper::eigenSparseMatrixToTriplets(linearConstraintsMatrix,
m_newLinearConstraintsTriplet)){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to evaluate triplets from the old hessian matrix."
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to evaluate triplets from the old hessian matrix."
<< std::endl;
return false;
}
Expand All @@ -150,7 +152,7 @@ bool OsqpEigen::Solver::updateLinearConstraintsMatrix(const Eigen::SparseCompres
m_constraintsNewIndices, m_constraintsNewValues)){
if (m_constraintsNewValues.size() > 0) {
if(osqp_update_A(m_workspace.get(), m_constraintsNewValues.data(), m_constraintsNewIndices.data(), m_constraintsNewIndices.size()) != 0){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to update linear constraints matrix."
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to update linear constraints matrix."
<< std::endl;
return false;
}
Expand All @@ -163,13 +165,13 @@ bool OsqpEigen::Solver::updateLinearConstraintsMatrix(const Eigen::SparseCompres
// get the primal and the dual variables

if(!getPrimalVariable(m_primalVariables)){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to get the primal variable."
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to get the primal variable."
<< std::endl;
return false;
}

if(!getDualVariable(m_dualVariables)){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to get the dual variable."
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to get the dual variable."
<< std::endl;
return false;
}
Expand All @@ -179,7 +181,7 @@ bool OsqpEigen::Solver::updateLinearConstraintsMatrix(const Eigen::SparseCompres

// set new linear constraints matrix
if(!m_data->setLinearConstraintsMatrix(linearConstraintsMatrix)){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to update the hessian matrix in "
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to update the hessian matrix in "
<< "Data object."
<< std::endl;
return false;
Expand All @@ -189,20 +191,20 @@ bool OsqpEigen::Solver::updateLinearConstraintsMatrix(const Eigen::SparseCompres
clearSolver();

if(!initSolver()){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to Initialize the solver."
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to Initialize the solver."
<< std::endl;
return false;
}

// set the old primal and dual variables
if(!setPrimalVariable(m_primalVariables)){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to set the primal variable."
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to set the primal variable."
<< std::endl;
return false;
}

if(!setDualVariable(m_dualVariables)){
std::cerr << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to set the dual variable."
debugStream << "[OsqpEigen::Solver::updateLinearConstraintsMatrix] Unable to set the dual variable."
<< std::endl;
return false;
}
Expand All @@ -215,14 +217,14 @@ bool OsqpEigen::Solver::setWarmStart(const Eigen::Matrix<T, n, 1> &primalVariabl
const Eigen::Matrix<T, m, 1> &dualVariable)
{
if(primalVariable.rows() != m_workspace->data->n){
std::cerr << "[OsqpEigen::Solver::setWarmStart] The size of the primal variable vector has to be equal to "
debugStream << "[OsqpEigen::Solver::setWarmStart] The size of the primal variable vector has to be equal to "
<< " the number of variables."
<< std::endl;
return false;
}

if(dualVariable.rows() != m_workspace->data->m){
std::cerr << "[OsqpEigen::Solver::setWarmStart] The size of the dual variable vector has to be equal to "
debugStream << "[OsqpEigen::Solver::setWarmStart] The size of the dual variable vector has to be equal to "
<< " the number of constraints."
<< std::endl;
return false;
Expand All @@ -239,7 +241,7 @@ template<typename T, int n>
bool OsqpEigen::Solver::setPrimalVariable(const Eigen::Matrix<T, n, 1> &primalVariable)
{
if(primalVariable.rows() != m_workspace->data->n){
std::cerr << "[OsqpEigen::Solver::setPrimalVariable] The size of the primal variable vector has to be equal to "
debugStream << "[OsqpEigen::Solver::setPrimalVariable] The size of the primal variable vector has to be equal to "
<< " the number of variables."
<< std::endl;
return false;
Expand All @@ -255,7 +257,7 @@ template<typename T, int m>
bool OsqpEigen::Solver::setDualVariable(const Eigen::Matrix<T, m, 1> &dualVariable)
{
if(dualVariable.rows() != m_workspace->data->m){
std::cerr << "[OsqpEigen::Solver::setDualVariable] The size of the dual variable vector has to be equal to "
debugStream << "[OsqpEigen::Solver::setDualVariable] The size of the dual variable vector has to be equal to "
<< " the number of constraints."
<< std::endl;
return false;
Expand All @@ -274,7 +276,7 @@ bool OsqpEigen::Solver::getPrimalVariable(Eigen::Matrix<T, n, 1> &primalVariable
}
else{
if (n != m_workspace->data->n){
std::cerr << "[OsqpEigen::Solver::getPrimalVariable] The size of the vector has to be equal to the number of "
debugStream << "[OsqpEigen::Solver::getPrimalVariable] The size of the vector has to be equal to the number of "
<< "variables. (You can use an eigen dynamic vector)"
<< std::endl;
return false;
Expand All @@ -294,7 +296,7 @@ bool OsqpEigen::Solver::getDualVariable(Eigen::Matrix<T, m, 1> &dualVariable)
}
else{
if (m != m_workspace->data->m){
std::cerr << "[OsqpEigen::Solver::getDualVariable] The size of the vector has to be equal to the number of "
debugStream << "[OsqpEigen::Solver::getDualVariable] The size of the vector has to be equal to the number of "
<< "constraints. (You can use an eigen dynamic vector)"
<< std::endl;
return false;
Expand Down
Loading