osqp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #ifndef OSQP_H
  2. # define OSQP_H
  3. # ifdef __cplusplus
  4. extern "C" {
  5. # endif // ifdef __cplusplus
  6. /* Includes */
  7. # include "types.h"
  8. # include "util.h" // Needed for osqp_set_default_settings functions
  9. // Library to deal with sparse matrices enabled only if embedded not defined
  10. # ifndef EMBEDDED
  11. # include "cs.h"
  12. # endif // ifndef EMBEDDED
  13. /********************
  14. * Main Solver API *
  15. ********************/
  16. /**
  17. * @name Main solver API
  18. * @{
  19. */
  20. /**
  21. * Set default settings from constants.h file
  22. * assumes settings already allocated in memory
  23. * @param settings settings structure
  24. */
  25. void osqp_set_default_settings(OSQPSettings *settings);
  26. # ifndef EMBEDDED
  27. /**
  28. * Initialize OSQP solver allocating memory.
  29. *
  30. * All the inputs must be already allocated in memory before calling.
  31. *
  32. * It performs:
  33. * - data and settings validation
  34. * - problem data scaling
  35. * - automatic parameters tuning (if enabled)
  36. * - setup linear system solver:
  37. * - direct solver: KKT matrix factorization is performed here
  38. * - indirect solver: KKT matrix preconditioning is performed here
  39. *
  40. * NB: This is the only function that allocates dynamic memory and is not used
  41. *during code generation
  42. *
  43. * @param workp Solver workspace pointer
  44. * @param data Problem data
  45. * @param settings Solver settings
  46. * @return Exitflag for errors (0 if no errors)
  47. */
  48. c_int osqp_setup(OSQPWorkspace** workp, const OSQPData* data, const OSQPSettings* settings);
  49. # endif // #ifndef EMBEDDED
  50. /**
  51. * Solve quadratic program
  52. *
  53. * The final solver information is stored in the \a work->info structure
  54. *
  55. * The solution is stored in the \a work->solution structure
  56. *
  57. * If the problem is primal infeasible, the certificate is stored
  58. * in \a work->delta_y
  59. *
  60. * If the problem is dual infeasible, the certificate is stored in \a
  61. * work->delta_x
  62. *
  63. * @param work Workspace allocated
  64. * @return Exitflag for errors
  65. */
  66. c_int osqp_solve(OSQPWorkspace *work);
  67. # ifndef EMBEDDED
  68. /**
  69. * Cleanup workspace by deallocating memory
  70. *
  71. * This function is not used in code generation
  72. * @param work Workspace
  73. * @return Exitflag for errors
  74. */
  75. c_int osqp_cleanup(OSQPWorkspace *work);
  76. # endif // ifndef EMBEDDED
  77. /** @} */
  78. /********************************************
  79. * Sublevel API *
  80. * *
  81. * Edit data without performing setup again *
  82. ********************************************/
  83. /**
  84. * @name Sublevel API
  85. * @{
  86. */
  87. /**
  88. * Update linear cost in the problem
  89. * @param work Workspace
  90. * @param q_new New linear cost
  91. * @return Exitflag for errors and warnings
  92. */
  93. c_int osqp_update_lin_cost(OSQPWorkspace *work,
  94. const c_float *q_new);
  95. /**
  96. * Update lower and upper bounds in the problem constraints
  97. * @param work Workspace
  98. * @param l_new New lower bound
  99. * @param u_new New upper bound
  100. * @return Exitflag: 1 if new lower bound is not <= than new upper bound
  101. */
  102. c_int osqp_update_bounds(OSQPWorkspace *work,
  103. const c_float *l_new,
  104. const c_float *u_new);
  105. /**
  106. * Update lower bound in the problem constraints
  107. * @param work Workspace
  108. * @param l_new New lower bound
  109. * @return Exitflag: 1 if new lower bound is not <= than upper bound
  110. */
  111. c_int osqp_update_lower_bound(OSQPWorkspace *work,
  112. const c_float *l_new);
  113. /**
  114. * Update upper bound in the problem constraints
  115. * @param work Workspace
  116. * @param u_new New upper bound
  117. * @return Exitflag: 1 if new upper bound is not >= than lower bound
  118. */
  119. c_int osqp_update_upper_bound(OSQPWorkspace *work,
  120. const c_float *u_new);
  121. /**
  122. * Warm start primal and dual variables
  123. * @param work Workspace structure
  124. * @param x Primal variable
  125. * @param y Dual variable
  126. * @return Exitflag
  127. */
  128. c_int osqp_warm_start(OSQPWorkspace *work,
  129. const c_float *x,
  130. const c_float *y);
  131. /**
  132. * Warm start primal variable
  133. * @param work Workspace structure
  134. * @param x Primal variable
  135. * @return Exitflag
  136. */
  137. c_int osqp_warm_start_x(OSQPWorkspace *work,
  138. const c_float *x);
  139. /**
  140. * Warm start dual variable
  141. * @param work Workspace structure
  142. * @param y Dual variable
  143. * @return Exitflag
  144. */
  145. c_int osqp_warm_start_y(OSQPWorkspace *work,
  146. const c_float *y);
  147. # if EMBEDDED != 1
  148. /**
  149. * Update elements of matrix P (upper triangular)
  150. * without changing sparsity structure.
  151. *
  152. *
  153. * If Px_new_idx is OSQP_NULL, Px_new is assumed to be as long as P->x
  154. * and the whole P->x is replaced.
  155. *
  156. * @param work Workspace structure
  157. * @param Px_new Vector of new elements in P->x (upper triangular)
  158. * @param Px_new_idx Index mapping new elements to positions in P->x
  159. * @param P_new_n Number of new elements to be changed
  160. * @return output flag: 0: OK
  161. * 1: P_new_n > nnzP
  162. * <0: error in the update
  163. */
  164. c_int osqp_update_P(OSQPWorkspace *work,
  165. const c_float *Px_new,
  166. const c_int *Px_new_idx,
  167. c_int P_new_n);
  168. /**
  169. * Update elements of matrix A without changing sparsity structure.
  170. *
  171. *
  172. * If Ax_new_idx is OSQP_NULL, Ax_new is assumed to be as long as A->x
  173. * and the whole A->x is replaced.
  174. *
  175. * @param work Workspace structure
  176. * @param Ax_new Vector of new elements in A->x
  177. * @param Ax_new_idx Index mapping new elements to positions in A->x
  178. * @param A_new_n Number of new elements to be changed
  179. * @return output flag: 0: OK
  180. * 1: A_new_n > nnzA
  181. * <0: error in the update
  182. */
  183. c_int osqp_update_A(OSQPWorkspace *work,
  184. const c_float *Ax_new,
  185. const c_int *Ax_new_idx,
  186. c_int A_new_n);
  187. /**
  188. * Update elements of matrix P (upper triangular) and elements of matrix A
  189. * without changing sparsity structure.
  190. *
  191. *
  192. * If Px_new_idx is OSQP_NULL, Px_new is assumed to be as long as P->x
  193. * and the whole P->x is replaced.
  194. *
  195. * If Ax_new_idx is OSQP_NULL, Ax_new is assumed to be as long as A->x
  196. * and the whole A->x is replaced.
  197. *
  198. * @param work Workspace structure
  199. * @param Px_new Vector of new elements in P->x (upper triangular)
  200. * @param Px_new_idx Index mapping new elements to positions in P->x
  201. * @param P_new_n Number of new elements to be changed
  202. * @param Ax_new Vector of new elements in A->x
  203. * @param Ax_new_idx Index mapping new elements to positions in A->x
  204. * @param A_new_n Number of new elements to be changed
  205. * @return output flag: 0: OK
  206. * 1: P_new_n > nnzP
  207. * 2: A_new_n > nnzA
  208. * <0: error in the update
  209. */
  210. c_int osqp_update_P_A(OSQPWorkspace *work,
  211. const c_float *Px_new,
  212. const c_int *Px_new_idx,
  213. c_int P_new_n,
  214. const c_float *Ax_new,
  215. const c_int *Ax_new_idx,
  216. c_int A_new_n);
  217. /**
  218. * Update rho. Limit it between RHO_MIN and RHO_MAX.
  219. * @param work Workspace
  220. * @param rho_new New rho setting
  221. * @return Exitflag
  222. */
  223. c_int osqp_update_rho(OSQPWorkspace *work,
  224. c_float rho_new);
  225. # endif // if EMBEDDED != 1
  226. /** @} */
  227. /**
  228. * @name Update settings
  229. * @{
  230. */
  231. /**
  232. * Update max_iter setting
  233. * @param work Workspace
  234. * @param max_iter_new New max_iter setting
  235. * @return Exitflag
  236. */
  237. c_int osqp_update_max_iter(OSQPWorkspace *work,
  238. c_int max_iter_new);
  239. /**
  240. * Update absolute tolernace value
  241. * @param work Workspace
  242. * @param eps_abs_new New absolute tolerance value
  243. * @return Exitflag
  244. */
  245. c_int osqp_update_eps_abs(OSQPWorkspace *work,
  246. c_float eps_abs_new);
  247. /**
  248. * Update relative tolernace value
  249. * @param work Workspace
  250. * @param eps_rel_new New relative tolerance value
  251. * @return Exitflag
  252. */
  253. c_int osqp_update_eps_rel(OSQPWorkspace *work,
  254. c_float eps_rel_new);
  255. /**
  256. * Update primal infeasibility tolerance
  257. * @param work Workspace
  258. * @param eps_prim_inf_new New primal infeasibility tolerance
  259. * @return Exitflag
  260. */
  261. c_int osqp_update_eps_prim_inf(OSQPWorkspace *work,
  262. c_float eps_prim_inf_new);
  263. /**
  264. * Update dual infeasibility tolerance
  265. * @param work Workspace
  266. * @param eps_dual_inf_new New dual infeasibility tolerance
  267. * @return Exitflag
  268. */
  269. c_int osqp_update_eps_dual_inf(OSQPWorkspace *work,
  270. c_float eps_dual_inf_new);
  271. /**
  272. * Update relaxation parameter alpha
  273. * @param work Workspace
  274. * @param alpha_new New relaxation parameter value
  275. * @return Exitflag
  276. */
  277. c_int osqp_update_alpha(OSQPWorkspace *work,
  278. c_float alpha_new);
  279. /**
  280. * Update warm_start setting
  281. * @param work Workspace
  282. * @param warm_start_new New warm_start setting
  283. * @return Exitflag
  284. */
  285. c_int osqp_update_warm_start(OSQPWorkspace *work,
  286. c_int warm_start_new);
  287. /**
  288. * Update scaled_termination setting
  289. * @param work Workspace
  290. * @param scaled_termination_new New scaled_termination setting
  291. * @return Exitflag
  292. */
  293. c_int osqp_update_scaled_termination(OSQPWorkspace *work,
  294. c_int scaled_termination_new);
  295. /**
  296. * Update check_termination setting
  297. * @param work Workspace
  298. * @param check_termination_new New check_termination setting
  299. * @return Exitflag
  300. */
  301. c_int osqp_update_check_termination(OSQPWorkspace *work,
  302. c_int check_termination_new);
  303. # ifndef EMBEDDED
  304. /**
  305. * Update regularization parameter in polish
  306. * @param work Workspace
  307. * @param delta_new New regularization parameter
  308. * @return Exitflag
  309. */
  310. c_int osqp_update_delta(OSQPWorkspace *work,
  311. c_float delta_new);
  312. /**
  313. * Update polish setting
  314. * @param work Workspace
  315. * @param polish_new New polish setting
  316. * @return Exitflag
  317. */
  318. c_int osqp_update_polish(OSQPWorkspace *work,
  319. c_int polish_new);
  320. /**
  321. * Update number of iterative refinement steps in polish
  322. * @param work Workspace
  323. * @param polish_refine_iter_new New iterative reginement steps
  324. * @return Exitflag
  325. */
  326. c_int osqp_update_polish_refine_iter(OSQPWorkspace *work,
  327. c_int polish_refine_iter_new);
  328. /**
  329. * Update verbose setting
  330. * @param work Workspace
  331. * @param verbose_new New verbose setting
  332. * @return Exitflag
  333. */
  334. c_int osqp_update_verbose(OSQPWorkspace *work,
  335. c_int verbose_new);
  336. # endif // #ifndef EMBEDDED
  337. # ifdef PROFILING
  338. /**
  339. * Update time_limit setting
  340. * @param work Workspace
  341. * @param time_limit_new New time_limit setting
  342. * @return Exitflag
  343. */
  344. c_int osqp_update_time_limit(OSQPWorkspace *work,
  345. c_float time_limit_new);
  346. # endif // ifdef PROFILING
  347. /** @} */
  348. # ifdef __cplusplus
  349. }
  350. # endif // ifdef __cplusplus
  351. #endif // ifndef OSQP_H