mosek_quadprog.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "mosek_quadprog.h"
  2. #include "mosek_guarded.h"
  3. #include <cstdio>
  4. #include "../find.h"
  5. #include "../verbose.h"
  6. #include "../speye.h"
  7. #include "../matrix_to_list.h"
  8. #include "../list_to_matrix.h"
  9. #include "../harwell_boeing.h"
  10. #include "../EPS.h"
  11. // Little function mosek needs in order to know how to print to std out
  12. static void MSKAPI printstr(void *handle, char str[])
  13. {
  14. printf("%s",str);
  15. }
  16. igl::MosekData::MosekData()
  17. {
  18. // These are the default settings that worked well for BBW. Your miles may
  19. // very well be kilometers.
  20. // >1e0 NONSOLUTION
  21. // 1e-1 artifacts in deformation
  22. // 1e-3 artifacts in isolines
  23. // 1e-4 seems safe
  24. // 1e-8 MOSEK DEFAULT SOLUTION
  25. douparam[MSK_DPAR_INTPNT_TOL_REL_GAP]=1e-8;
  26. // Force using multiple threads, not sure if MOSEK is properly destorying
  27. //extra threads...
  28. intparam[MSK_IPAR_INTPNT_NUM_THREADS] = 6;
  29. // Turn off presolving
  30. // intparam[MSK_IPAR_PRESOLVE_USE] = MSK_PRESOLVE_MODE_OFF;
  31. // Force particular matrix reordering method
  32. // MSK_ORDER_METHOD_NONE cuts time in half roughly, since half the time is
  33. // usually spent reordering the matrix
  34. // !! WARNING Setting this parameter to anything but MSK_ORDER_METHOD_FREE
  35. // seems to have the effect of setting it to MSK_ORDER_METHOD_NONE
  36. // *Or maybe Mosek is spending a bunch of time analyzing the matrix to
  37. // choose the right ordering method when really any of them are
  38. // instantaneous
  39. intparam[MSK_IPAR_INTPNT_ORDER_METHOD] = MSK_ORDER_METHOD_NONE;
  40. // 1.0 means optimizer is very leniant about declaring model infeasible
  41. douparam[MSK_DPAR_INTPNT_TOL_INFEAS] = 1e-8;
  42. // Hard to say if this is doing anything, probably nothing dramatic
  43. douparam[MSK_DPAR_INTPNT_TOL_PSAFE]= 1e2;
  44. // Turn off convexity check
  45. intparam[MSK_IPAR_CHECK_CONVEXITY] = MSK_CHECK_CONVEXITY_NONE;
  46. // Force turn off data check
  47. intparam[MSK_IPAR_DATA_CHECK]=MSK_OFF;
  48. }
  49. template <typename Index, typename Scalar>
  50. IGL_INLINE bool igl::mosek_quadprog(
  51. const Index n,
  52. std::vector<Index> & Qi,
  53. std::vector<Index> & Qj,
  54. std::vector<Scalar> & Qv,
  55. const std::vector<Scalar> & c,
  56. const Scalar cf,
  57. const Index m,
  58. std::vector<Scalar> & Av,
  59. std::vector<Index> & Ari,
  60. const std::vector<Index> & Acp,
  61. const std::vector<Scalar> & lc,
  62. const std::vector<Scalar> & uc,
  63. const std::vector<Scalar> & lx,
  64. const std::vector<Scalar> & ux,
  65. MosekData & mosek_data,
  66. std::vector<Scalar> & x)
  67. {
  68. // I J V vectors of Q should all be same length
  69. assert(Qv.size() == Qi.size());
  70. assert(Qv.size() == Qj.size());
  71. // number of columns in linear constraint matrix must be ≤ number of
  72. // variables
  73. assert( (int)Acp.size() == (n+1));
  74. // linear bound vectors must be size of number of constraints or empty
  75. assert( ((int)lc.size() == m) || ((int)lc.size() == 0));
  76. assert( ((int)uc.size() == m) || ((int)uc.size() == 0));
  77. // constant bound vectors must be size of number of variables or empty
  78. assert( ((int)lx.size() == n) || ((int)lx.size() == 0));
  79. assert( ((int)ux.size() == n) || ((int)ux.size() == 0));
  80. // allocate space for solution in x
  81. x.resize(n);
  82. // variables for mosek task, env and result code
  83. MSKenv_t env;
  84. MSKtask_t task;
  85. // Create the MOSEK environment
  86. mosek_guarded(MSK_makeenv(&env,NULL,NULL,NULL,NULL));
  87. ///* Directs the log stream to the 'printstr' function. */
  88. //mosek_guarded(MSK_linkfunctoenvstream(env,MSK_STREAM_LOG,NULL,printstr));
  89. // initialize mosek environment
  90. mosek_guarded(MSK_initenv(env));
  91. // Create the optimization task
  92. mosek_guarded(MSK_maketask(env,m,n,&task));
  93. verbose("Creating task with %ld linear constraints and %ld variables...\n",m,n);
  94. //// Tell mosek how to print to std out
  95. //mosek_guarded(MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr));
  96. // Give estimate of number of variables
  97. mosek_guarded(MSK_putmaxnumvar(task,n));
  98. if(m>0)
  99. {
  100. // Give estimate of number of constraints
  101. mosek_guarded(MSK_putmaxnumcon(task,m));
  102. // Give estimate of number of non zeros in A
  103. mosek_guarded(MSK_putmaxnumanz(task,Av.size()));
  104. }
  105. // Give estimate of number of non zeros in Q
  106. mosek_guarded(MSK_putmaxnumqnz(task,Qv.size()));
  107. if(m>0)
  108. {
  109. // Append 'm' empty constraints, the constrainst will initially have no
  110. // bounds
  111. mosek_guarded(MSK_append(task,MSK_ACC_CON,m));
  112. }
  113. // Append 'n' variables
  114. mosek_guarded(MSK_append(task,MSK_ACC_VAR,n));
  115. // add a contant term to the objective
  116. mosek_guarded(MSK_putcfix(task,cf));
  117. // loop over variables
  118. for(int j = 0;j<n;j++)
  119. {
  120. if(c.size() > 0)
  121. {
  122. // Set linear term c_j in the objective
  123. mosek_guarded(MSK_putcj(task,j,c[j]));
  124. }
  125. // Set constant bounds on variable j
  126. if(lx[j] == ux[j])
  127. {
  128. mosek_guarded(MSK_putbound(task,MSK_ACC_VAR,j,MSK_BK_FX,lx[j],ux[j]));
  129. }else
  130. {
  131. mosek_guarded(MSK_putbound(task,MSK_ACC_VAR,j,MSK_BK_RA,lx[j],ux[j]));
  132. }
  133. if(m>0)
  134. {
  135. // Input column j of A
  136. mosek_guarded(
  137. MSK_putavec(
  138. task,
  139. MSK_ACC_VAR,
  140. j,
  141. Acp[j+1]-Acp[j],
  142. &Ari[Acp[j]],
  143. &Av[Acp[j]])
  144. );
  145. }
  146. }
  147. // loop over constraints
  148. for(int i = 0;i<m;i++)
  149. {
  150. // put bounds on constraints
  151. mosek_guarded(MSK_putbound(task,MSK_ACC_CON,i,MSK_BK_RA,lc[i],uc[i]));
  152. }
  153. // Input Q for the objective (REMEMBER Q SHOULD ONLY BE LOWER TRIANGLE)
  154. mosek_guarded(MSK_putqobj(task,Qv.size(),&Qi[0],&Qj[0],&Qv[0]));
  155. // Set up task parameters
  156. for(
  157. std::map<MSKiparame,int>::iterator pit = mosek_data.intparam.begin();
  158. pit != mosek_data.intparam.end();
  159. pit++)
  160. {
  161. mosek_guarded(MSK_putintparam(task,pit->first,pit->second));
  162. }
  163. for(
  164. std::map<MSKdparame,double>::iterator pit = mosek_data.douparam.begin();
  165. pit != mosek_data.douparam.end();
  166. pit++)
  167. {
  168. mosek_guarded(MSK_putdouparam(task,pit->first,pit->second));
  169. }
  170. // Now the optimizer has been prepared
  171. MSKrescodee trmcode;
  172. // run the optimizer
  173. mosek_guarded(MSK_optimizetrm(task,&trmcode));
  174. //// Print a summary containing information about the solution for debugging
  175. //// purposes
  176. //MSK_solutionsummary(task,MSK_STREAM_LOG);
  177. // Get status of solution
  178. MSKsolstae solsta;
  179. MSK_getsolutionstatus(task,MSK_SOL_ITR,NULL,&solsta);
  180. bool success = false;
  181. switch(solsta)
  182. {
  183. case MSK_SOL_STA_OPTIMAL:
  184. case MSK_SOL_STA_NEAR_OPTIMAL:
  185. MSK_getsolutionslice(task,MSK_SOL_ITR,MSK_SOL_ITEM_XX,0,n,&x[0]);
  186. //printf("Optimal primal solution\n");
  187. //for(size_t j=0; j<n; ++j)
  188. //{
  189. // printf("x[%ld]: %g\n",j,x[j]);
  190. //}
  191. success = true;
  192. break;
  193. case MSK_SOL_STA_DUAL_INFEAS_CER:
  194. case MSK_SOL_STA_PRIM_INFEAS_CER:
  195. case MSK_SOL_STA_NEAR_DUAL_INFEAS_CER:
  196. case MSK_SOL_STA_NEAR_PRIM_INFEAS_CER:
  197. //printf("Primal or dual infeasibility certificate found.\n");
  198. break;
  199. case MSK_SOL_STA_UNKNOWN:
  200. //printf("The status of the solution could not be determined.\n");
  201. break;
  202. default:
  203. //printf("Other solution status.");
  204. break;
  205. }
  206. MSK_deletetask(&task);
  207. MSK_deleteenv(&env);
  208. return success;
  209. }
  210. IGL_INLINE bool igl::mosek_quadprog(
  211. const Eigen::SparseMatrix<double> & Q,
  212. const Eigen::VectorXd & c,
  213. const double cf,
  214. const Eigen::SparseMatrix<double> & A,
  215. const Eigen::VectorXd & lc,
  216. const Eigen::VectorXd & uc,
  217. const Eigen::VectorXd & lx,
  218. const Eigen::VectorXd & ux,
  219. MosekData & mosek_data,
  220. Eigen::VectorXd & x)
  221. {
  222. using namespace Eigen;
  223. using namespace std;
  224. using namespace igl;
  225. // Q should be square
  226. assert(Q.rows() == Q.cols());
  227. // Q should be symmetric
  228. #ifdef EIGEN_HAS_A_BUG_AND_FAILS_TO_LET_ME_COMPUTE_Q_MINUS_Q_TRANSPOSE
  229. assert( (Q-Q.transpose()).sum() < FLOAT_EPS);
  230. #endif
  231. // Only keep lower triangular part of Q
  232. SparseMatrix<double> QL;
  233. //QL = Q.template triangularView<Lower>();
  234. QL = Q.triangularView<Lower>();
  235. VectorXi Qi,Qj;
  236. VectorXd Qv;
  237. find(QL,Qi,Qj,Qv);
  238. vector<int> vQi = matrix_to_list(Qi);
  239. vector<int> vQj = matrix_to_list(Qj);
  240. vector<double> vQv = matrix_to_list(Qv);
  241. // Convert linear term
  242. vector<double> vc = matrix_to_list(c);
  243. assert(lc.size() == A.rows());
  244. assert(uc.size() == A.rows());
  245. // Convert A to harwell boeing format
  246. vector<double> vAv;
  247. vector<int> vAr,vAc;
  248. int nr;
  249. harwell_boeing(A,nr,vAv,vAr,vAc);
  250. assert(lx.size() == Q.rows());
  251. assert(ux.size() == Q.rows());
  252. vector<double> vlc = matrix_to_list(lc);
  253. vector<double> vuc = matrix_to_list(uc);
  254. vector<double> vlx = matrix_to_list(lx);
  255. vector<double> vux = matrix_to_list(ux);
  256. vector<double> vx;
  257. bool ret = mosek_quadprog(
  258. Q.rows(),vQi,vQj,vQv,
  259. vc,
  260. cf,
  261. nr,
  262. vAv, vAr, vAc,
  263. vlc,vuc,
  264. vlx,vux,
  265. mosek_data,
  266. vx);
  267. list_to_matrix(vx,x);
  268. return ret;
  269. }
  270. #ifndef IGL_HEADER_ONLY
  271. // Explicit template declarations
  272. #endif