qpOASES-3.0beta/interfaces/matlab/qpOASES_matlab_utils.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of qpOASES.
3  *
4  * qpOASES -- An Implementation of the Online Active Set Strategy.
5  * Copyright (C) 2007-2011 by Hans Joachim Ferreau, Andreas Potschka,
6  * Christian Kirches et al. All rights reserved.
7  *
8  * qpOASES is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * qpOASES is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with qpOASES; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 
37 #include "mex.h"
38 #include "matrix.h"
39 #include "string.h"
40 
41 
42 /*
43  * g e t S t a t u s
44  */
46 {
47  /* determine status from returnvalue */
48  switch ( returnvalue )
49  {
50  case SUCCESSFUL_RETURN:
51  return 0.0;
52 
54  return 1.0;
55 
58  return -2.0;
59 
62  return -3.0;
63 
64  default:
65  return -1.0;
66  }
67 }
68 
69 
70 /*
71  * s m a r t D i m e n s i o n C h e c k
72  */
73 returnValue smartDimensionCheck( real_t** input, unsigned int m, unsigned int n, BooleanType emptyAllowed,
74  const mxArray* prhs[], int idx
75  )
76 {
77  /* If index is negative, the input does not exist. */
78  if ( idx < 0 )
79  {
80  *input = 0;
81  return SUCCESSFUL_RETURN;
82  }
83 
84  /* Otherwise the input has been passed by the user. */
85  if ( mxIsEmpty( prhs[ idx ] ) )
86  {
87  /* input is empty */
88  if ( emptyAllowed == BT_TRUE )
89  {
90  *input = 0;
91  return SUCCESSFUL_RETURN;
92  }
93  else
94  {
95  char msg[200];
96  snprintf(msg, 199, "ERROR (qpOASES): Empty argument %d not allowed!", idx+1);
97  mexErrMsgTxt( msg );
98  return RET_INVALID_ARGUMENTS;
99  }
100  }
101  else
102  {
103  /* input is non-empty */
104  if ( ( mxGetM( prhs[ idx ] ) == m ) && ( mxGetN( prhs[ idx ] ) == n ) )
105  {
106  *input = (real_t*) mxGetPr( prhs[ idx ] );
107  return SUCCESSFUL_RETURN;
108  }
109  else
110  {
111  char msg[200];
112  snprintf(msg, 199, "ERROR (qpOASES): Input dimension mismatch for argument %d ([%ld,%ld] ~= [%d,%d]).",
113  idx+1, (long int)mxGetM(prhs[idx]), (long int)mxGetN(prhs[idx]), m, n);
114  mexErrMsgTxt( msg );
115  return RET_INVALID_ARGUMENTS;
116  }
117  }
118 
119  return SUCCESSFUL_RETURN;
120 }
121 
122 
123 /*
124  * c o n v e r t F o r t r a n T o C
125  */
126 returnValue convertFortranToC( const real_t* const A_for, int nV, int nC, real_t* const A )
127 {
128  int i,j;
129 
130  for ( i=0; i<nC; ++i )
131  for ( j=0; j<nV; ++j )
132  A[i*nV + j] = A_for[j*nC + i];
133 
134  return SUCCESSFUL_RETURN;
135 }
136 
137 
138 /*
139  * h a s O p t i o n s V a l u e
140  */
141 BooleanType hasOptionsValue( const mxArray* optionsPtr, const char* const optionString, double** optionValue )
142 {
143  mxArray* optionName = mxGetField( optionsPtr,0,optionString );
144 
145  if( !mxIsEmpty(optionName) )
146  {
147  if ( ( mxGetM( optionName ) != 1 ) || ( mxGetN( optionName ) != 1 ) )
148  mexErrMsgTxt( "ERROR (qpOASES_options): Option has to be a numerical constant." );
149 
150  *optionValue = mxGetPr( optionName );
151  return BT_TRUE;
152  }
153 
154  return BT_FALSE;
155 }
156 
157 
158 /*
159  * s e t u p O p t i o n s
160  */
161 returnValue setupOptions( Options* options, const mxArray* optionsPtr, int& nWSRin )
162 {
163  double* optionValue;
164  int optionValueInt;
165 
166  if ( hasOptionsValue( optionsPtr,"maxIter",&optionValue ) == BT_TRUE )
167  if ( *optionValue > 0.0 )
168  nWSRin = (int)*optionValue;
169 
170  if ( hasOptionsValue( optionsPtr,"printLevel",&optionValue ) == BT_TRUE )
171  {
172  optionValueInt = (int)*optionValue;
173  options->printLevel = (qpOASES::PrintLevel)optionValueInt;
174  }
175 
176 
177  if ( hasOptionsValue( optionsPtr,"enableRamping",&optionValue ) == BT_TRUE )
178  {
179  optionValueInt = (int)*optionValue;
180  options->enableRamping = (qpOASES::BooleanType)optionValueInt;
181  }
182 
183  if ( hasOptionsValue( optionsPtr,"enableFarBounds",&optionValue ) == BT_TRUE )
184  {
185  optionValueInt = (int)*optionValue;
186  options->enableFarBounds = (qpOASES::BooleanType)optionValueInt;
187  }
188 
189  if ( hasOptionsValue( optionsPtr,"enableFlippingBounds",&optionValue ) == BT_TRUE )
190  {
191  optionValueInt = (int)*optionValue;
192  options->enableFlippingBounds = (qpOASES::BooleanType)optionValueInt;
193  }
194 
195  if ( hasOptionsValue( optionsPtr,"enableRegularisation",&optionValue ) == BT_TRUE )
196  {
197  optionValueInt = (int)*optionValue;
198  options->enableRegularisation = (qpOASES::BooleanType)optionValueInt;
199  }
200 
201  if ( hasOptionsValue( optionsPtr,"enableFullLITests",&optionValue ) == BT_TRUE )
202  {
203  optionValueInt = (int)*optionValue;
204  options->enableFullLITests = (qpOASES::BooleanType)optionValueInt;
205  }
206 
207  if ( hasOptionsValue( optionsPtr,"enableNZCTests",&optionValue ) == BT_TRUE )
208  {
209  optionValueInt = (int)*optionValue;
210  options->enableNZCTests = (qpOASES::BooleanType)optionValueInt;
211  }
212 
213  if ( hasOptionsValue( optionsPtr,"enableDriftCorrection",&optionValue ) == BT_TRUE )
214  options->enableDriftCorrection = (int)*optionValue;
215 
216  if ( hasOptionsValue( optionsPtr,"enableCholeskyRefactorisation",&optionValue ) == BT_TRUE )
217  options->enableCholeskyRefactorisation = (int)*optionValue;
218 
219  if ( hasOptionsValue( optionsPtr,"enableEqualities",&optionValue ) == BT_TRUE )
220  {
221  optionValueInt = (int)*optionValue;
222  options->enableEqualities = (qpOASES::BooleanType)optionValueInt;
223  }
224 
225 
226  if ( hasOptionsValue( optionsPtr,"terminationTolerance",&optionValue ) == BT_TRUE )
227  options->terminationTolerance = *optionValue;
228 
229  if ( hasOptionsValue( optionsPtr,"boundTolerance",&optionValue ) == BT_TRUE )
230  options->boundTolerance = *optionValue;
231 
232  if ( hasOptionsValue( optionsPtr,"boundRelaxation",&optionValue ) == BT_TRUE )
233  options->boundRelaxation = *optionValue;
234 
235  if ( hasOptionsValue( optionsPtr,"epsNum",&optionValue ) == BT_TRUE )
236  options->epsNum = *optionValue;
237 
238  if ( hasOptionsValue( optionsPtr,"epsDen",&optionValue ) == BT_TRUE )
239  options->epsDen = *optionValue;
240 
241  if ( hasOptionsValue( optionsPtr,"maxPrimalJump",&optionValue ) == BT_TRUE )
242  options->maxPrimalJump = *optionValue;
243 
244  if ( hasOptionsValue( optionsPtr,"maxDualJump",&optionValue ) == BT_TRUE )
245  options->maxDualJump = *optionValue;
246 
247 
248  if ( hasOptionsValue( optionsPtr,"initialRamping",&optionValue ) == BT_TRUE )
249  options->initialRamping = *optionValue;
250 
251  if ( hasOptionsValue( optionsPtr,"finalRamping",&optionValue ) == BT_TRUE )
252  options->finalRamping = *optionValue;
253 
254  if ( hasOptionsValue( optionsPtr,"initialFarBounds",&optionValue ) == BT_TRUE )
255  options->initialFarBounds = *optionValue;
256 
257  if ( hasOptionsValue( optionsPtr,"growFarBounds",&optionValue ) == BT_TRUE )
258  options->growFarBounds = *optionValue;
259 
260  if ( hasOptionsValue( optionsPtr,"initialStatusBounds",&optionValue ) == BT_TRUE )
261  {
262  optionValueInt = (int)*optionValue;
263  options->initialStatusBounds = (qpOASES::SubjectToStatus)optionValueInt;
264  }
265 
266  if ( hasOptionsValue( optionsPtr,"epsFlipping",&optionValue ) == BT_TRUE )
267  options->epsFlipping = *optionValue;
268 
269  if ( hasOptionsValue( optionsPtr,"numRegularisationSteps",&optionValue ) == BT_TRUE )
270  options->numRegularisationSteps = (int)*optionValue;
271 
272  if ( hasOptionsValue( optionsPtr,"epsRegularisation",&optionValue ) == BT_TRUE )
273  options->epsRegularisation = *optionValue;
274 
275  if ( hasOptionsValue( optionsPtr,"numRefinementSteps",&optionValue ) == BT_TRUE )
276  options->numRefinementSteps = (int)*optionValue;
277 
278  if ( hasOptionsValue( optionsPtr,"epsIterRef",&optionValue ) == BT_TRUE )
279  options->epsIterRef = *optionValue;
280 
281  if ( hasOptionsValue( optionsPtr,"epsLITests",&optionValue ) == BT_TRUE )
282  options->epsLITests = *optionValue;
283 
284  if ( hasOptionsValue( optionsPtr,"epsNZCTests",&optionValue ) == BT_TRUE )
285  options->epsNZCTests = *optionValue;
286 
287  return SUCCESSFUL_RETURN;
288 }
289 
290 
291 
292 /*
293  * a l l o c a t e O u t p u t s
294  */
295 void allocateOutputs( int nlhs, mxArray* plhs[], int nV, int nC = 0, int nP = 1
296  )
297 {
298  /* Create output vectors and assign pointers to them. */
299  plhs[0] = mxCreateDoubleMatrix( nV, nP, mxREAL );
300 
301  if ( nlhs >= 2 )
302  {
303  plhs[1] = mxCreateDoubleMatrix( 1, nP, mxREAL );
304 
305  if ( nlhs >= 3 )
306  {
307  plhs[2] = mxCreateDoubleMatrix( 1, nP, mxREAL );
308 
309  if ( nlhs >= 4 )
310  {
311  plhs[3] = mxCreateDoubleMatrix( 1, nP, mxREAL );
312 
313  if ( nlhs == 5 )
314  {
315  plhs[4] = mxCreateDoubleMatrix( nV+nC, nP, mxREAL );
316  }
317  }
318  }
319  }
320 }
321 
322 
323 /*
324  * o b t a i n O u t p u t s
325  */
326 void obtainOutputs( int k, QProblemB* qp, returnValue returnvalue, int nWSRin,
327  int nlhs, mxArray* plhs[], int nV, int nC = 0
328  )
329 {
330  /* Create output vectors and assign pointers to them. */
331  double* x = mxGetPr( plhs[0] );
332  qp->getPrimalSolution( &(x[k*nV]) );
333 
334  if ( nlhs >= 2 )
335  {
336  double* obj = mxGetPr( plhs[1] );
337  obj[k] = qp->getObjVal( );
338 
339  if ( nlhs >= 3 )
340  {
341  double* status = mxGetPr( plhs[2] );
342  status[k] = getStatus( returnvalue );
343 
344  if ( nlhs >= 4 )
345  {
346  double* nWSRout = mxGetPr( plhs[3] );
347  nWSRout[k] = (real_t) nWSRin;
348 
349  if ( nlhs == 5 )
350  {
351  double* y = mxGetPr( plhs[4] );
352  qp->getDualSolution( &(y[k*(nV+nC)]) );
353  }
354  }
355  }
356  }
357 }
358 
359 
360 /*
361  * end of file
362  */
Implements the online active set strategy for box-constrained QPs.
real_t getStatus(returnValue returnvalue)
returnValue setupOptions(Options *options, const mxArray *optionsPtr, int &nWSRin)
Allows to pass back messages to the calling function.
returnValue smartDimensionCheck(real_t **input, unsigned int m, unsigned int n, BooleanType emptyAllowed, const mxArray *prhs[], int idx)
returnValue convertFortranToC(const real_t *const A_for, int nV, int nC, real_t *const A)
void allocateOutputs(int nlhs, mxArray *plhs[], int nV, int nC=0, int nP=1)
Provides a generic way to set and pass user-specified options.
Definition: options.hpp:65
PrintLevel
void obtainOutputs(int k, QProblemB *qp, returnValue returnvalue, int nWSRin, int nlhs, mxArray *plhs[], int nV, int nC=0)
#define BT_TRUE
Definition: acado_types.hpp:47
BooleanType hasOptionsValue(const mxArray *optionsPtr, const char *const optionString, double **optionValue)
#define BT_FALSE
Definition: acado_types.hpp:49
double real_t
Definition: AD_test.c:10


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:00