ecos.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * ECOS - Embedded Conic Solver.
  3. * Copyright (C) 2012-2015 A. Domahidi [domahidi@embotech.com],
  4. * Automatic Control Lab, ETH Zurich & embotech GmbH, Zurich, Switzerland.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __ECOS_H__
  20. #define __ECOS_H__
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #include "glblopts.h"
  25. #include "spla.h"
  26. #include "cone.h"
  27. #include "kkt.h"
  28. #if PROFILING > 0
  29. #include "timer.h"
  30. #endif
  31. #if CTRLC > 0
  32. #include "ctrlc.h"
  33. #endif
  34. /* ECOS VERSION NUMBER - FORMAT: X.Y.Z --------------------------------- */
  35. #define ECOS_VERSION ("2.0.7")
  36. /* DEFAULT SOLVER PARAMETERS AND SETTINGS STRUCT ----------------------- */
  37. #define MAXIT (100) /* maximum number of iterations */
  38. #define FEASTOL (1E-1) /* primal/dual infeasibility tolerance */
  39. #define ABSTOL (1E-1) /* absolute tolerance on duality gap */
  40. #define RELTOL (1E-1) /* relative tolerance on duality gap */
  41. #define FTOL_INACC (1E-4) /* inaccurate solution feasibility tol. */
  42. #define ATOL_INACC (5E-5) /* inaccurate solution absolute tol. */
  43. #define RTOL_INACC (5E-5) /* inaccurate solution relative tol. */
  44. #define GAMMA (0.99) /* scaling the final step length */
  45. #define STATICREG (1) /* static regularization: 0:off, 1:on */
  46. #define DELTASTAT (7E-9) /* regularization parameter */
  47. #define DELTASTAT1 (0E-9)
  48. #define DELTASTAT2 (7E-9)
  49. #define DELTA (2E-7) /* dyn. regularization parameter */
  50. #define EPS (1E-14) /* dyn. regularization threshold (do not 0!) */
  51. #define VERBOSE (1) /* bool for verbosity; PRINTLEVEL < 3 */
  52. #define NITREF (1) /* number of iterative refinement steps */
  53. #define IRERRFACT (6) /* factor by which IR should reduce err */
  54. #define LINSYSACC (1E-14) /* rel. accuracy of search direction */
  55. #define SIGMAMIN (1E-4) /* always do some centering */
  56. #define SIGMAMAX (1.0) /* never fully center */
  57. #define STEPMIN (1E-6) /* smallest step that we do take */
  58. #define STEPMAX (0.999) /* largest step allowed, also in affine dir. */
  59. #define SAFEGUARD (500) /* Maximum increase in PRES before
  60. ECOS_NUMERICS is thrown. */
  61. /*Ecos exponential cone default settings*/
  62. #ifdef EXPCONE
  63. #define MAX_BK (90) /*Maximum backtracking steps*/
  64. #define BK_SCALE (0.8) /*Backtracking constant*/
  65. #define MIN_DISTANCE (0.1) /* dont let sqrt(r), sqrt(-u) or sqrt(v)
  66. become smaller than
  67. MIN_DISTANCE*mu*/
  68. #define CENTRALITY (1) /*Centrality requirement*/
  69. #endif
  70. /* EQUILIBRATION METHOD ------------------------------------------------ */
  71. #define EQUILIBRATE (1) /* use equlibration of data matrices? >0: yes */
  72. #define EQUIL_ITERS (3) /* number of equilibration iterations */
  73. #define RUIZ_EQUIL /* define algorithm to use - if both are ... */
  74. /*#define ALTERNATING_EQUIL*/ /* ... commented out no equlibration is used */
  75. /* EXITCODES ----------------------------------------------------------- */
  76. #define ECOS_OPTIMAL (0) /* Problem solved to optimality */
  77. #define ECOS_PINF (1) /* Found certificate of primal infeasibility */
  78. #define ECOS_DINF (2) /* Found certificate of dual infeasibility */
  79. #define ECOS_INACC_OFFSET (10) /* Offset exitflag at inaccurate results */
  80. #define ECOS_MAXIT (-1) /* Maximum number of iterations reached */
  81. #define ECOS_NUMERICS (-2) /* Search direction unreliable */
  82. #define ECOS_OUTCONE (-3) /* s or z got outside the cone, numerics? */
  83. #define ECOS_SIGINT (-4) /* solver interrupted by a signal/ctrl-c */
  84. #define ECOS_FATAL (-7) /* Unknown problem in solver */
  85. /* SETTINGS STRUCT ----------------------------------------------------- */
  86. typedef struct settings{
  87. pfloat gamma; /* scaling the final step length */
  88. pfloat delta; /* regularization parameter */
  89. pfloat eps; /* regularization threshold */
  90. pfloat feastol; /* primal/dual infeasibility tolerance */
  91. pfloat abstol; /* absolute tolerance on duality gap */
  92. pfloat reltol; /* relative tolerance on duality gap */
  93. pfloat feastol_inacc; /* primal/dual infeasibility relaxed tolerance */
  94. pfloat abstol_inacc; /* absolute relaxed tolerance on duality gap */
  95. pfloat reltol_inacc; /* relative relaxed tolerance on duality gap */
  96. idxint nitref; /* number of iterative refinement steps */
  97. idxint maxit; /* maximum number of iterations */
  98. idxint verbose; /* verbosity bool for PRINTLEVEL < 3 */
  99. #ifdef EXPCONE /*Exponential cone settings*/
  100. idxint max_bk_iter; /* Maximum backtracking iterations */
  101. pfloat bk_scale; /* Backtracking scaling */
  102. pfloat centrality; /* Centrality bound, ignored when centrality vars = 0*/
  103. #endif
  104. } settings;
  105. /* INFO STRUCT --------------------------------------------------------- */
  106. typedef struct stats{
  107. pfloat pcost;
  108. pfloat dcost;
  109. pfloat pres;
  110. pfloat dres;
  111. pfloat pinf;
  112. pfloat dinf;
  113. pfloat pinfres;
  114. pfloat dinfres;
  115. pfloat gap;
  116. pfloat relgap;
  117. pfloat sigma;
  118. pfloat mu;
  119. pfloat step;
  120. pfloat step_aff;
  121. pfloat kapovert;
  122. idxint iter;
  123. idxint nitref1;
  124. idxint nitref2;
  125. idxint nitref3;
  126. #if PROFILING > 0
  127. pfloat tsetup;
  128. pfloat tsolve;
  129. #endif
  130. #if PROFILING > 1
  131. pfloat tfactor;
  132. pfloat tkktsolve;
  133. pfloat torder;
  134. pfloat tkktcreate;
  135. pfloat ttranspose;
  136. pfloat tperm;
  137. pfloat tfactor_t1;
  138. pfloat tfactor_t2;
  139. #endif
  140. #ifdef EXPCONE
  141. /* Counters for backtracking, each of these counts
  142. * one condition that can fail and cause a backtrack
  143. */
  144. idxint pob; /* Potential decreases */
  145. idxint cb; /* Centrality violations */
  146. idxint cob; /* The s'z of one cone is too small w.r.t. mu */
  147. idxint pb; /* Primal infeasibility */
  148. idxint db; /* Dual infeasibility */
  149. idxint affBack; /* Total affine backtracking steps */
  150. idxint cmbBack; /* Total combined backtracking steps */
  151. pfloat centrality; /*Centrality at the end of the backtracking*/
  152. #endif
  153. } stats;
  154. /* ALL DATA NEEDED BY SOLVER ------------------------------------------- */
  155. typedef struct pwork{
  156. /* dimensions */
  157. idxint n; /* number of primal variables x */
  158. idxint m; /* number of conically constrained variables s */
  159. idxint p; /* number of equality constraints */
  160. idxint D; /* degree of the cone */
  161. /* variables */
  162. pfloat* x; /* primal variables */
  163. pfloat* y; /* multipliers for equality constaints */
  164. pfloat* z; /* multipliers for conic inequalities */
  165. pfloat* s; /* slacks for conic inequalities */
  166. pfloat* lambda; /* scaled variable */
  167. pfloat kap; /* kappa (homogeneous embedding) */
  168. pfloat tau; /* tau (homogeneous embedding) */
  169. /* best iterate seen so far */
  170. /* variables */
  171. pfloat* best_x; /* primal variables */
  172. pfloat* best_y; /* multipliers for equality constaints */
  173. pfloat* best_z; /* multipliers for conic inequalities */
  174. pfloat* best_s; /* slacks for conic inequalities */
  175. pfloat best_kap; /* kappa (homogeneous embedding) */
  176. pfloat best_tau; /* tau (homogeneous embedding) */
  177. pfloat best_cx;
  178. pfloat best_by;
  179. pfloat best_hz;
  180. stats* best_info; /* info of best iterate */
  181. /* temporary stuff holding search direction etc. */
  182. pfloat* dsaff;
  183. pfloat* dzaff;
  184. pfloat* W_times_dzaff;
  185. pfloat* dsaff_by_W;
  186. pfloat* saff;
  187. pfloat* zaff;
  188. /* cone */
  189. cone* C;
  190. /* problem data */
  191. spmat* A; spmat* G; pfloat* c; pfloat* b; pfloat* h;
  192. /* indices that map entries of A and G to the KKT matrix */
  193. idxint *AtoK; idxint *GtoK;
  194. #if defined EQUILIBRATE && EQUILIBRATE > 0
  195. /* equilibration vector */
  196. pfloat *xequil;
  197. pfloat *Aequil;
  198. pfloat *Gequil;
  199. #endif
  200. /* scalings of problem data */
  201. pfloat resx0; pfloat resy0; pfloat resz0;
  202. /* residuals */
  203. pfloat *rx; pfloat *ry; pfloat *rz; pfloat rt;
  204. pfloat hresx; pfloat hresy; pfloat hresz;
  205. /* norm iterates */
  206. pfloat nx,ny,nz,ns;
  207. /* temporary storage */
  208. pfloat cx; pfloat by; pfloat hz; pfloat sz;
  209. /* KKT System */
  210. kkt* KKT;
  211. /* info struct */
  212. stats* info;
  213. /* settings struct */
  214. settings* stgs;
  215. } pwork;
  216. /* SOME USEFUL MACROS -------------------------------------------------- */
  217. #define MAX(X,Y) ((X) < (Y) ? (Y) : (X)) /* maximum of 2 expressions */
  218. /* safe division x/y where y is assumed to be positive! */
  219. #define SAFEDIV_POS(X,Y) ( (Y) < EPS ? ((X)/EPS) : (X)/(Y) )
  220. /* METHODS */
  221. /* set up work space
  222. * could be done by codegen
  223. *
  224. * Parameters:
  225. * idxint n Number of variables
  226. * idxint m Number of inequalities, number of rows of G
  227. * idxint p Number of equality constraints
  228. * idxint l Dimension of positive orthant
  229. * idxint ncones Number of second order cones
  230. * idxint* q Array of length 'ncones', defines the dimension of each cone
  231. * idxint nex Number of exponential cones
  232. * pfloat* Gpr Sparse G matrix data array (column compressed storage)
  233. * idxint* Gjc Sparse G matrix column index array (column compressed storage)
  234. * idxint* Gir Sparse G matrix row index array (column compressed storage)
  235. * pfloat* Apr Sparse A matrix data array (column compressed storage) (can be all NULL if no equalities are present)
  236. * idxint* Ajc Sparse A matrix column index array (column compressed storage) (can be all NULL if no equalities are present)
  237. * idxint* Air Sparse A matrix row index array (column compressed storage) (can be all NULL if no equalities are present)
  238. * pfloat* c Array of size n, cost function weights
  239. * pfloat* h Array of size m, RHS vector of cone constraint
  240. * pfloat* b Array of size p, RHS vector of equalities (can be NULL if no equalities are present)
  241. */
  242. pwork* ECOS_setup(idxint n, idxint m, idxint p, idxint l, idxint ncones, idxint* q, idxint nex,
  243. pfloat* Gpr, idxint* Gjc, idxint* Gir,
  244. pfloat* Apr, idxint* Ajc, idxint* Air,
  245. pfloat* c, pfloat* h, pfloat* b);
  246. #ifdef EXPCONE
  247. pfloat expConeLineSearch(pwork* w, pfloat dtau, pfloat dkappa, idxint affine);
  248. #endif
  249. /* solve */
  250. idxint ECOS_solve(pwork* w);
  251. /**
  252. * Cleanup: free memory (not used for embedded solvers, only standalone)
  253. *
  254. * Use the second argument to give the number of variables to NOT free.
  255. * This is useful if you want to use the result of the optimization without
  256. * copying over the arrays. One use case is the MEX interface, where we
  257. * do not want to free x,y,s,z (depending on the number of LHS).
  258. */
  259. void ECOS_cleanup(pwork* w, idxint keepvars);
  260. /**
  261. * Version: returns the current version number
  262. * Use a character array of length 7 to obtain the version number
  263. * in the format
  264. * x.y.zzz
  265. * where x is the major, y the minor and zzz the build number
  266. */
  267. const char* ECOS_ver(void);
  268. /* ------------------- EXPERT LEVEL INTERFACES ---------------------- */
  269. /*
  270. * Updates one element of the RHS vector h of inequalities
  271. * After the call, w->h[idx] = value (but equilibrated)
  272. */
  273. void ecos_updateDataEntry_h(pwork* w, idxint idx, pfloat value);
  274. /*
  275. * Updates one element of the OBJ vector c of inequalities
  276. * After the call, w->c[idx] = value (but equilibrated)
  277. */
  278. void ecos_updateDataEntry_c(pwork* w, idxint idx, pfloat value);
  279. /*
  280. * Updates numerical data for G, A, c, h, and b,
  281. * and re-equilibrates.
  282. * Then updates the corresponding KKT entries.
  283. */
  284. void ECOS_updateData(pwork *w, pfloat *Gpr, pfloat *Apr,
  285. pfloat* c, pfloat* h, pfloat* b);
  286. #ifdef __cplusplus
  287. }
  288. #endif
  289. #endif