-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.hpp
366 lines (297 loc) · 14.4 KB
/
Utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* This file is part of qpOASES.
*
* qpOASES -- An Implementation of the Online Active Set Strategy.
* Copyright (C) 2007-2015 by Hans Joachim Ferreau, Andreas Potschka,
* Christian Kirches et al. All rights reserved.
*
* qpOASES is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* qpOASES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with qpOASES; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/**
* \file include/qpOASES/Utils.hpp
* \author Hans Joachim Ferreau, Andreas Potschka, Christian Kirches
* \version 3.2
* \date 2007-2015
*
* Declaration of some utility functions for working with qpOASES.
*/
#ifndef QPOASES_UTILS_HPP
#define QPOASES_UTILS_HPP
#include "MessageHandling.hpp"
BEGIN_NAMESPACE_QPOASES
/** Prints a (possibly named) vector.
* \return SUCCESSFUL_RETURN */
returnValue print( const real_t* const v, /**< Vector to be printed. */
int_t n, /**< Length of vector. */
const char* name = 0 /**< Name of vector. */
);
/** Prints a (possibly named) permuted vector.
* \return SUCCESSFUL_RETURN */
returnValue print( const real_t* const v, /**< Vector to be printed. */
int_t n, /**< Length of vector. */
const int_t* const V_idx, /**< Pemutation vector. */
const char* name = 0 /**< Name of vector. */
);
/** Prints a (possibly named) matrix.
* \return SUCCESSFUL_RETURN */
returnValue print( const real_t* const M, /**< Matrix to be printed. */
int_t nrow, /**< Row number of matrix. */
int_t ncol, /**< Column number of matrix. */
const char* name = 0 /**< Name of matrix. */
);
/** Prints a (possibly named) permuted matrix.
* \return SUCCESSFUL_RETURN */
returnValue print( const real_t* const M, /**< Matrix to be printed. */
int_t nrow, /**< Row number of matrix. */
int_t ncol , /**< Column number of matrix. */
const int_t* const ROW_idx, /**< Row pemutation vector. */
const int_t* const COL_idx, /**< Column pemutation vector. */
const char* name = 0 /**< Name of matrix. */
);
/** Prints a (possibly named) index array.
* \return SUCCESSFUL_RETURN */
returnValue print( const int_t* const index, /**< Index array to be printed. */
int_t n, /**< Length of index array. */
const char* name = 0 /**< Name of index array. */
);
/** Prints a string to desired output target (useful also for MATLAB output!).
* \return SUCCESSFUL_RETURN */
returnValue myPrintf( const char* s /**< String to be written. */
);
/** Prints qpOASES copyright notice.
* \return SUCCESSFUL_RETURN */
returnValue printCopyrightNotice( );
/** Reads a real_t matrix from file.
* \return SUCCESSFUL_RETURN \n
RET_UNABLE_TO_OPEN_FILE \n
RET_UNABLE_TO_READ_FILE */
returnValue readFromFile( real_t* data, /**< Matrix to be read from file. */
int_t nrow, /**< Row number of matrix. */
int_t ncol, /**< Column number of matrix. */
const char* datafilename /**< Data file name. */
);
/** Reads a real_t vector from file.
* \return SUCCESSFUL_RETURN \n
RET_UNABLE_TO_OPEN_FILE \n
RET_UNABLE_TO_READ_FILE */
returnValue readFromFile( real_t* data, /**< Vector to be read from file. */
int_t n, /**< Length of vector. */
const char* datafilename /**< Data file name. */
);
/** Reads an integer (column) vector from file.
* \return SUCCESSFUL_RETURN \n
RET_UNABLE_TO_OPEN_FILE \n
RET_UNABLE_TO_READ_FILE */
returnValue readFromFile( int_t* data, /**< Vector to be read from file. */
int_t n, /**< Length of vector. */
const char* datafilename /**< Data file name. */
);
/** Writes a real_t matrix into a file.
* \return SUCCESSFUL_RETURN \n
RET_UNABLE_TO_OPEN_FILE */
returnValue writeIntoFile( const real_t* const data, /**< Matrix to be written into file. */
int_t nrow, /**< Row number of matrix. */
int_t ncol, /**< Column number of matrix. */
const char* datafilename, /**< Data file name. */
BooleanType append = BT_FALSE /**< Indicates if data shall be appended if the file already exists (otherwise it is overwritten). */
);
/** Writes a real_t vector into a file.
* \return SUCCESSFUL_RETURN \n
RET_UNABLE_TO_OPEN_FILE */
returnValue writeIntoFile( const real_t* const data, /**< Vector to be written into file. */
int_t n, /**< Length of vector. */
const char* datafilename, /**< Data file name. */
BooleanType append = BT_FALSE /**< Indicates if data shall be appended if the file already exists (otherwise it is overwritten). */
);
/** Writes an integer (column) vector into a file.
* \return SUCCESSFUL_RETURN \n
RET_UNABLE_TO_OPEN_FILE */
returnValue writeIntoFile( const int_t* const integer, /**< Integer vector to be written into file. */
int_t n, /**< Length of vector. */
const char* datafilename, /**< Data file name. */
BooleanType append = BT_FALSE /**< Indicates if integer shall be appended if the file already exists (otherwise it is overwritten). */
);
/** Writes a real_t matrix/vector into a Matlab binary file.
* \return SUCCESSFUL_RETURN \n
RET_INVALID_ARGUMENTS
RET_UNABLE_TO_WRITE_FILE */
returnValue writeIntoMatFile( FILE* const matFile, /**< Pointer to Matlab binary file. */
const real_t* const data, /**< Data to be written into file. */
int_t nRows, /**< Row number of matrix. */
int_t nCols, /**< Column number of matrix. */
const char* name /**< Matlab name of matrix/vector to be stored. */
);
/** Writes in integer matrix/vector into a Matlab binary file.
* \return SUCCESSFUL_RETURN \n
RET_INVALID_ARGUMENTS
RET_UNABLE_TO_WRITE_FILE */
returnValue writeIntoMatFile( FILE* const matFile, /**< Pointer to Matlab binary file. */
const int_t* const data, /**< Data to be written into file. */
int_t nRows, /**< Row number of matrix. */
int_t nCols, /**< Column number of matrix. */
const char* name /**< Matlab name of matrix/vector to be stored. */
);
/** Returns the current system time.
* \return current system time */
real_t getCPUtime( );
/** Returns the N-norm of a vector.
* \return >= 0.0: successful */
real_t getNorm( const real_t* const v, /**< Vector. */
int_t n, /**< Vector's dimension. */
int_t type = 2 /**< Norm type, 1: one-norm, 2: Euclidean norm. */
);
/** Tests whether two real_t-valued arguments are (numerically) equal.
* \return BT_TRUE: arguments differ not more than TOL \n
BT_FALSE: arguments differ more than TOL */
inline BooleanType isEqual( real_t x, /**< First real number. */
real_t y, /**< Second real number. */
real_t TOL = ZERO /**< Tolerance for comparison. */
);
/** Tests whether a real-valued argument is (numerically) zero.
* \return BT_TRUE: argument differs from 0.0 not more than TOL \n
BT_FALSE: argument differs from 0.0 more than TOL */
inline BooleanType isZero( real_t x, /**< Real number. */
real_t TOL = ZERO /**< Tolerance for comparison. */
);
/** Returns sign of a real-valued argument.
* \return 1.0: argument is non-negative \n
-1.0: argument is negative */
inline real_t getSign( real_t arg /**< real-valued argument whose sign is to be determined. */
);
/** Returns maximum of two integers.
* \return Maximum of two integers */
inline int_t getMax( int_t x, /**< First integer. */
int_t y /**< Second integer. */
);
/** Returns minimum of two integers.
* \return Minimum of two integers */
inline int_t getMin( int_t x, /**< First integer. */
int_t y /**< Second integer. */
);
/** Returns maximum of two reals.
* \return Maximum of two reals */
inline real_t getMax( real_t x, /**< First real number. */
real_t y /**< Second real number. */
);
/** Returns minimum of two reals.
* \return Minimum of two reals */
inline real_t getMin( real_t x, /**< First real number. */
real_t y /**< Second real number. */
);
/** Returns the absolute value of a real number.
* \return Absolute value of a real number */
inline real_t getAbs( real_t x /**< Real number. */
);
/** Returns the square-root of a real number.
* \return Square-root of a real number */
inline real_t getSqrt( real_t x /**< Non-negative real number. */
);
/** Computes the maximum violation of the KKT optimality conditions
* of given iterate for given QP data. */
returnValue getKktViolation( int_t nV, /**< Number of variables. */
int_t nC, /**< Number of constraints. */
const real_t* const H, /**< Hessian matrix (may be NULL if Hessian is zero or identity matrix). */
const real_t* const g, /**< Gradient vector. */
const real_t* const A, /**< Constraint matrix. */
const real_t* const lb, /**< Lower bound vector (on variables). */
const real_t* const ub, /**< Upper bound vector (on variables). */
const real_t* const lbA, /**< Lower constraints' bound vector. */
const real_t* const ubA, /**< Upper constraints' bound vector. */
const real_t* const x, /**< Primal trial vector. */
const real_t* const y, /**< Dual trial vector. */
real_t& stat, /**< Output: maximum value of stationarity condition residual. */
real_t& feas, /**< Output: maximum value of primal feasibility violation. */
real_t& cmpl, /**< Output: maximum value of complementarity residual. */
const real_t* const workingSetB = 0, /**< Working set of bounds (used to determine active bounds). */
const real_t* const workingSetC = 0, /**< Working set of constraints (used to determine active constraints). */
BooleanType hasIdentityHessian = BT_FALSE /**< Indicating whether Hessian matrix is identity matrix or not if NULL pointer is passed. */
);
/** Computes the maximum violation of the KKT optimality conditions
* of given iterate for given QP data. */
returnValue getKktViolation( int_t nV, /**< Number of variables. */
const real_t* const H, /**< Hessian matrix (may be NULL if Hessian is zero or identity matrix). */
const real_t* const g, /**< Gradient vector. */
const real_t* const lb, /**< Lower bound vector (on variables). */
const real_t* const ub, /**< Upper bound vector (on variables). */
const real_t* const x, /**< Primal trial vector. */
const real_t* const y, /**< Dual trial vector. */
real_t& stat, /**< Output: maximum value of stationarity condition residual. */
real_t& feas, /**< Output: maximum value of primal feasibility violation. */
real_t& cmpl, /**< Output: maximum value of complementarity residual. */
const real_t* const workingSetB = 0, /**< Working set of bounds (used to determine active bounds). */
BooleanType hasIdentityHessian = BT_FALSE /**< Indicating whether Hessian matrix is identity matrix or not if NULL pointer is passed */
);
/** Writes a value of BooleanType into a string.
* \return SUCCESSFUL_RETURN */
returnValue convertBooleanTypeToString( BooleanType value, /**< Value to be written. */
char* const string /**< Input: String of sufficient size, \n
Output: String containing value. */
);
/** Writes a value of SubjectToStatus into a string.
* \return SUCCESSFUL_RETURN */
returnValue convertSubjectToStatusToString( SubjectToStatus value, /**< Value to be written. */
char* const string /**< Input: String of sufficient size, \n
Output: String containing value. */
);
/** Writes a value of PrintLevel into a string.
* \return SUCCESSFUL_RETURN */
returnValue convertPrintLevelToString( PrintLevel value, /**< Value to be written. */
char* const string /**< Input: String of sufficient size, \n
Output: String containing value. */
);
/** Converts a returnValue from an (S)QProblem(B) object into a more
* simple status flag.
*
* \return 0: QP problem solved
* 1: QP could not be solved within given number of iterations
* -1: QP could not be solved due to an internal error
* -2: QP is infeasible (and thus could not be solved)
* -3: QP is unbounded (and thus could not be solved)
*/
int_t getSimpleStatus( returnValue returnvalue, /**< ReturnValue to be analysed. */
BooleanType doPrintStatus = BT_FALSE /**< Flag indicating whether simple status shall be printed to screen. */
);
/** Normalises QP constraints.
* \return SUCCESSFUL_RETURN \n
* RET_INVALID_ARGUMENTS */
returnValue normaliseConstraints( int_t nV, /**< Number of variables. */
int_t nC, /**< Number of constraints. */
real_t* A, /**< Input: Constraint matrix, \n
Output: Normalised constraint matrix. */
real_t* lbA, /**< Input: Constraints' lower bound vector, \n
Output: Normalised constraints' lower bound vector. */
real_t* ubA, /**< Input: Constraints' upper bound vector, \n
Output: Normalised constraints' upper bound vector. */
int_t type = 1 /**< Norm type, 1: one-norm, 2: Euclidean norm. */
);
#ifdef __DEBUG__
/** Writes matrix with given dimension into specified file. */
extern "C" void gdb_printmat( const char *fname, /**< File name. */
real_t *M, /**< Matrix to be written. */
int_t n, /**< Number of rows. */
int_t m, /**< Number of columns. */
int_t ldim /**< Leading dimension. */
);
#endif /* __DEBUG__ */
#if defined(__DSPACE__) || defined(__XPCTARGET__) || defined(__C_WRAPPER__)
extern "C" void __cxa_pure_virtual( void );
#endif /* __DSPACE__ || __XPCTARGET__*/
END_NAMESPACE_QPOASES
#include "Utils.ipp"
#endif /* QPOASES_UTILS_HPP */
/*
* end of file
*/