Skip to content

Commit

Permalink
fixed issue with NaN test in Matlab/octave interface; suppressed some…
Browse files Browse the repository at this point in the history
… warnings in Matlab tests
  • Loading branch information
jferreau committed Jan 12, 2015
1 parent e48d28d commit e2628db
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 269 deletions.
4 changes: 2 additions & 2 deletions examples/example1a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ int main( )

/* Solve first QP. */
int nWSR = 10;
example.init( 0,g,A,lb,ub,lbA,ubA, nWSR,0 );
example.init( H,g,A,lb,ub,lbA,ubA, nWSR,0 );

/* Solve second QP. */
nWSR = 10;
example.hotstart( 0,g_new,A_new,lb_new,ub_new,lbA_new,ubA_new, nWSR,0 );
example.hotstart( H_new,g_new,A_new,lb_new,ub_new,lbA_new,ubA_new, nWSR,0 );

/* Get and print solution of second QP. */
real_t xOpt[2];
Expand Down
34 changes: 15 additions & 19 deletions interfaces/matlab/qpOASES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,20 +429,17 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )
}

/* check if supplied data contains 'NaN' or 'Inf' */
if (containsNaNorInf(prhs, nV*nV, H_idx, 0) == BT_TRUE) {
if (containsNaNorInf( prhs,H_idx, 0 ) == BT_TRUE)
return;
}

if (containsNaNorInf(prhs, nV, g_idx, 0) == BT_TRUE) {
if (containsNaNorInf( prhs,g_idx, 0 ) == BT_TRUE)
return;
}

if (containsNaNorInf(prhs, nV, lb_idx, 1) == BT_TRUE) {
if (containsNaNorInf( prhs,lb_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nV, ub_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ub_idx, 1 ) == BT_TRUE)
return;
}

/* Check inputs dimensions and assign pointers to inputs. */
if ( ( H_idx >= 0 ) && ( ( mxGetN( prhs[ H_idx ] ) != nV ) || ( mxGetM( prhs[ H_idx ] ) != nV ) ) )
Expand Down Expand Up @@ -473,15 +470,14 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )
return;
}

if (containsNaNorInf(prhs, nV*nC, A_idx, 0) == BT_TRUE) {
if (containsNaNorInf(prhs,A_idx, 0 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nC, lbA_idx, 1) == BT_TRUE) {

if (containsNaNorInf(prhs,lbA_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nC, ubA_idx, 1) == BT_TRUE) {

if (containsNaNorInf(prhs,ubA_idx, 1 ) == BT_TRUE)
return;
}
}

/* check dimensions and copy auxInputs */
Expand Down Expand Up @@ -509,11 +505,11 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )
if ( auxInput_idx >= 0 )
setupAuxiliaryInputs( prhs[auxInput_idx],nV,nC, &hessianType,&x0,&guessedBounds,&guessedConstraints,&R_for );

/* convert Cholesky factor to C storage format */
if ( R_for != 0 )
{
R = new real_t[nV*nV];
convertFortranToC( R_for, nV,nV, R );
/* convert Cholesky factor to C storage format */
if ( R_for != 0 )
{
R = new real_t[nV*nV];
convertFortranToC( R_for, nV,nV, R );
}

/* III) ACTUALLY PERFORM QPOASES FUNCTION CALL: */
Expand Down
23 changes: 13 additions & 10 deletions interfaces/matlab/qpOASES_matlab_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,18 @@ bool mxIsScalar( const mxArray *pm )
* a l l o c a t e Q P r o b l e m I n s t a n c e
*/
int allocateQPInstance( int nV, int nC, HessianType hessianType,
BooleanType isSimplyBounded, const Options *options
BooleanType isSimplyBounded, const Options* options
)
{
QPInstance* inst = new QPInstance( nV,nC,hessianType, isSimplyBounded );

if ( inst->sqp != 0 )
inst->sqp->setOptions ( *options );
if ( ( inst->sqp != 0 ) && ( options != 0 ) )
inst->sqp->setOptions( *options );

if ( inst->qpb != 0 )
inst->qpb->setOptions ( *options );
if ( ( inst->qpb != 0 ) && ( options != 0 ) )
inst->qpb->setOptions( *options );

g_instances.push_back (inst);
g_instances.push_back(inst);
return inst->handle;
}

Expand Down Expand Up @@ -339,18 +339,21 @@ BooleanType containsInf( const real_t* const data, unsigned int dim )
/*
* c o n t a i n s N a N o r I n f
*/
BooleanType containsNaNorInf( const mxArray* prhs[], unsigned int dim, int rhs_index,
bool mayContainInf )
BooleanType containsNaNorInf( const mxArray* prhs[], int rhs_index,
bool mayContainInf
)
{
unsigned int dim;
char msg[MAX_STRING_LENGTH];

if ( rhs_index < 0 )
return BT_FALSE;

/* overwrite dim for sparse matrices */
if (mxIsSparse(prhs[rhs_index]) == 1) {
if (mxIsSparse(prhs[rhs_index]) == 1)
dim = (unsigned int)mxGetNzmax(prhs[rhs_index]);
}
else
dim = mxGetM(prhs[rhs_index]) * mxGetN(prhs[rhs_index]);

if (containsNaN((real_t*) mxGetPr(prhs[rhs_index]), dim) == BT_TRUE) {
snprintf(msg, MAX_STRING_LENGTH,
Expand Down
94 changes: 40 additions & 54 deletions interfaces/matlab/qpOASES_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int QProblemB_init( int handle,
int nWSRin, real_t maxCpuTimeIn,
const double* const x0, Options* options,
int nOutputs, mxArray* plhs[],
const double* const guessedBounds,
const double* const guessedBounds,
const double* const _R
)
{
Expand Down Expand Up @@ -128,7 +128,7 @@ int SQProblem_init( int handle,
int nWSRin, real_t maxCpuTimeIn,
const double* const x0, Options* options,
int nOutputs, mxArray* plhs[],
const double* const guessedBounds, const double* const guessedConstraints,
const double* const guessedBounds, const double* const guessedConstraints,
const double* const _R
)
{
Expand Down Expand Up @@ -434,14 +434,12 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )


/* Check for 'Inf' and 'Nan' in Hessian */
if (containsNaNorInf(prhs, nV*nV, H_idx, 0) == BT_TRUE) {
if (containsNaNorInf( prhs,H_idx, 0 ) == BT_TRUE)
return;
}

/* Check for 'Inf' and 'Nan' in gradient */
if (containsNaNorInf(prhs, nV, g_idx, 0) == BT_TRUE) {
if (containsNaNorInf(prhs,g_idx, 0 ) == BT_TRUE)
return;
}

/* determine whether is it a simply bounded QP */
if ( nrhs <= 7 )
Expand All @@ -454,12 +452,11 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )
lb_idx = 3;
ub_idx = 4;

if (containsNaNorInf(prhs, nV, lb_idx, 1) == BT_TRUE) {
if (containsNaNorInf( prhs,lb_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nV, ub_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ub_idx, 1 ) == BT_TRUE)
return;
}

/* Check inputs dimensions and assign pointers to inputs. */
nC = 0; /* row number of constraint matrix */
Expand Down Expand Up @@ -523,21 +520,20 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )
lbA_idx = 6;
ubA_idx = 7;

if (containsNaNorInf(prhs, nV*nC, A_idx, 0) == BT_TRUE) {
if (containsNaNorInf( prhs,A_idx, 0 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nV, lb_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,lb_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nV, ub_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ub_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nC, lbA_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,lbA_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nC, ubA_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ubA_idx, 1 ) == BT_TRUE)
return;
}

if ( ( mxGetN( prhs[ A_idx ] ) != 0 ) && ( mxGetN( prhs[ A_idx ] ) != nV ) )
{
Expand Down Expand Up @@ -693,16 +689,14 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )
lb_idx = 3;
ub_idx = 4;

if (containsNaNorInf(prhs, nV, g_idx, 0) == BT_TRUE) {
if (containsNaNorInf( prhs,g_idx, 0 ) == BT_TRUE)
return;
}

if (containsNaNorInf(prhs, nV, lb_idx, 1) == BT_TRUE) {
if (containsNaNorInf( prhs,lb_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nV, ub_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ub_idx, 1 ) == BT_TRUE)
return;
}


/* Check inputs dimensions and assign pointers to inputs. */
Expand Down Expand Up @@ -734,12 +728,11 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )
lbA_idx = 5;
ubA_idx = 6;

if (containsNaNorInf(prhs, nC, lbA_idx, 1) == BT_TRUE) {
if (containsNaNorInf( prhs,lbA_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nC, ubA_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ubA_idx, 1 ) == BT_TRUE)
return;
}

if ( smartDimensionCheck( &g,nV,1, BT_FALSE,prhs,g_idx ) != SUCCESSFUL_RETURN )
return;
Expand Down Expand Up @@ -856,31 +849,26 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )
}

/* check if supplied data contains 'NaN' or 'Inf' */
if (containsNaNorInf(prhs, nV*nV, H_idx, 0) == BT_TRUE) {
if (containsNaNorInf(prhs,H_idx, 0) == BT_TRUE)
return;
}

if (containsNaNorInf(prhs, nV, g_idx, 0) == BT_TRUE) {
if (containsNaNorInf( prhs,g_idx, 0 ) == BT_TRUE)
return;
}

if (containsNaNorInf(prhs, nV*nC, A_idx, 0) == BT_TRUE) {
if (containsNaNorInf( prhs,A_idx, 0 ) == BT_TRUE)
return;
}

if (containsNaNorInf(prhs, nV, lb_idx, 1) == BT_TRUE) {
if (containsNaNorInf( prhs,lb_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nV, ub_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ub_idx, 1 ) == BT_TRUE)
return;
}

if (containsNaNorInf(prhs, nC, lbA_idx, 1) == BT_TRUE) {
if (containsNaNorInf( prhs,lbA_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nC, ubA_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ubA_idx, 1 ) == BT_TRUE)
return;
}

/* Check that dimensions are consistent with existing QP instance */
if (nV != (unsigned int) globalQP->getNV () || nC != (unsigned int) globalQP->getNC ())
Expand Down Expand Up @@ -992,22 +980,20 @@ void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] )
ubA_idx = 6;

/* check if supplied data contains 'NaN' or 'Inf' */
if (containsNaNorInf(prhs, nV, g_idx, 0) == BT_TRUE) {
if (containsNaNorInf(prhs,g_idx, 0) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nV, lb_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,lb_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nV, ub_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ub_idx, 1 ) == BT_TRUE)
return;
}

if (containsNaNorInf(prhs, nC, lbA_idx, 1) == BT_TRUE) {
if (containsNaNorInf( prhs,lbA_idx, 1 ) == BT_TRUE)
return;
}
if (containsNaNorInf(prhs, nC, ubA_idx, 1) == BT_TRUE) {

if (containsNaNorInf( prhs,ubA_idx, 1 ) == BT_TRUE)
return;
}

if ( smartDimensionCheck( &g,nV,nRHS, BT_FALSE,prhs,g_idx ) != SUCCESSFUL_RETURN )
return;
Expand Down
Loading

0 comments on commit e2628db

Please sign in to comment.