From 138b6db76a985e78d21f66ccd21d55cbb38c72e0 Mon Sep 17 00:00:00 2001 From: Giulio Romualdi Date: Fri, 26 Mar 2021 11:08:07 +0100 Subject: [PATCH] Check if the solver is initialized in some methods of Solver class --- include/OsqpEigen/Solver.tpp | 25 +++++++++++++++++++++++++ src/Solver.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/OsqpEigen/Solver.tpp b/include/OsqpEigen/Solver.tpp index 59fb4d2..48feb70 100644 --- a/include/OsqpEigen/Solver.tpp +++ b/include/OsqpEigen/Solver.tpp @@ -216,6 +216,12 @@ template bool OsqpEigen::Solver::setWarmStart(const Eigen::Matrix &primalVariable, const Eigen::Matrix &dualVariable) { + if(!m_isSolverInitialized){ + debugStream() << "[OsqpEigen::Solver::setWarmStart] The solver is not initialized" + << std::endl; + return false; + } + if(primalVariable.rows() != m_workspace->data->n){ debugStream() << "[OsqpEigen::Solver::setWarmStart] The size of the primal variable vector has to be equal to " << " the number of variables." @@ -240,6 +246,12 @@ bool OsqpEigen::Solver::setWarmStart(const Eigen::Matrix &primalVariabl template bool OsqpEigen::Solver::setPrimalVariable(const Eigen::Matrix &primalVariable) { + if(!m_isSolverInitialized){ + debugStream() << "[OsqpEigen::Solver::setPrimalVariable] The solver is not initialized" + << std::endl; + return false; + } + if(primalVariable.rows() != m_workspace->data->n){ debugStream() << "[OsqpEigen::Solver::setPrimalVariable] The size of the primal variable vector has to be equal to " << " the number of variables." @@ -271,6 +283,12 @@ bool OsqpEigen::Solver::setDualVariable(const Eigen::Matrix &dualVariab template bool OsqpEigen::Solver::getPrimalVariable(Eigen::Matrix &primalVariable) { + if(!m_isSolverInitialized){ + debugStream() << "[OsqpEigen::Solver::getPrimalVariable] The solver is not initialized" + << std::endl; + return false; + } + if(n == Eigen::Dynamic){ primalVariable.resize(m_workspace->data->n, 1); } @@ -291,6 +309,13 @@ bool OsqpEigen::Solver::getPrimalVariable(Eigen::Matrix &primalVariable template bool OsqpEigen::Solver::getDualVariable(Eigen::Matrix &dualVariable) { + if(!m_isSolverInitialized){ + debugStream() << "[OsqpEigen::Solver::getDualVariable] The solver is not initialized" + << std::endl; + return false; + } + + if(m == Eigen::Dynamic){ dualVariable.resize(m_workspace->data->m, 1); } diff --git a/src/Solver.cpp b/src/Solver.cpp index f80a4ae..ad8eeb0 100644 --- a/src/Solver.cpp +++ b/src/Solver.cpp @@ -186,6 +186,12 @@ const std::unique_ptr>& Osqp bool OsqpEigen::Solver::updateGradient(const Eigen::Ref>& gradient) { + if(!m_isSolverInitialized){ + debugStream() << "[OsqpEigen::Solver::updateGradient] The solver is not initialized" + << std::endl; + return false; + } + // check if the dimension of the gradient is correct if(gradient.rows() != m_workspace->data->n){ debugStream() << "[OsqpEigen::Solver::updateGradient] The size of the gradient must be equal to the number of the variables." @@ -204,6 +210,12 @@ bool OsqpEigen::Solver::updateGradient(const Eigen::Ref>& lowerBound) { + if(!m_isSolverInitialized){ + debugStream() << "[OsqpEigen::Solver::updateLowerBound] The solver is not initialized" + << std::endl; + return false; + } + // check if the dimension of the lowerBound vector is correct if(lowerBound.rows() != m_workspace->data->m){ debugStream() << "[OsqpEigen::Solver::updateLowerBound] The size of the lower bound must be equal to the number of the variables." @@ -223,6 +235,12 @@ bool OsqpEigen::Solver::updateLowerBound(const Eigen::Ref>& upperBound) { + if(!m_isSolverInitialized){ + debugStream() << "[OsqpEigen::Solver::updateUpperBound] The solver is not initialized" + << std::endl; + return false; + } + // check if the dimension of the upperBound vector is correct if(upperBound.rows() != m_workspace->data->m){ debugStream() << "[OsqpEigen::Solver::updateUpperBound] The size of the upper bound must be equal to the number of the variables." @@ -242,6 +260,12 @@ bool OsqpEigen::Solver::updateUpperBound(const Eigen::Ref>& lowerBound, const Eigen::Ref>& upperBound) { + if(!m_isSolverInitialized){ + debugStream() << "[OsqpEigen::Solver::updateBounds] The solver is not initialized" + << std::endl; + return false; + } + // check if the dimension of the upperBound vector is correct if(upperBound.rows() != m_workspace->data->m){ debugStream() << "[OsqpEigen::Solver::updateBounds] The size of the upper bound must be equal to the number of the variables."