mosek_linprog.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "mosek_linprog.h"
  9. #include "../mosek/mosek_guarded.h"
  10. #include "../harwell_boeing.h"
  11. #include <limits>
  12. #include <cmath>
  13. #include <vector>
  14. IGL_INLINE bool igl::mosek::mosek_linprog(
  15. const Eigen::VectorXd & c,
  16. const Eigen::SparseMatrix<double> & A,
  17. const Eigen::VectorXd & lc,
  18. const Eigen::VectorXd & uc,
  19. const Eigen::VectorXd & lx,
  20. const Eigen::VectorXd & ux,
  21. Eigen::VectorXd & x)
  22. {
  23. // variables for mosek task, env and result code
  24. MSKenv_t env;
  25. // Create the MOSEK environment
  26. mosek_guarded(MSK_makeenv(&env,NULL));
  27. // initialize mosek environment
  28. mosek_guarded(MSK_initenv(env));
  29. const bool ret = mosek_linprog(c,A,lc,uc,lx,ux,env,x);
  30. MSK_deleteenv(&env);
  31. return ret;
  32. }
  33. IGL_INLINE bool igl::mosek::mosek_linprog(
  34. const Eigen::VectorXd & c,
  35. const Eigen::SparseMatrix<double> & A,
  36. const Eigen::VectorXd & lc,
  37. const Eigen::VectorXd & uc,
  38. const Eigen::VectorXd & lx,
  39. const Eigen::VectorXd & ux,
  40. const MSKenv_t & env,
  41. Eigen::VectorXd & x)
  42. {
  43. // following http://docs.mosek.com/7.1/capi/Linear_optimization.html
  44. using namespace std;
  45. // number of constraints
  46. const int m = A.rows();
  47. // number of variables
  48. const int n = A.cols();
  49. vector<double> vAv;
  50. vector<int> vAri,vAcp;
  51. int nr;
  52. harwell_boeing(A,nr,vAv,vAri,vAcp);
  53. MSKtask_t task;
  54. // Create the optimization task
  55. mosek_guarded(MSK_maketask(env,m,n,&task));
  56. // no threads
  57. mosek_guarded(MSK_putintparam(task,MSK_IPAR_NUM_THREADS,1));
  58. if(m>0)
  59. {
  60. // Append 'm' empty constraints, the constrainst will initially have no
  61. // bounds
  62. mosek_guarded(MSK_appendcons(task,m));
  63. }
  64. mosek_guarded(MSK_appendvars(task,n));
  65. const auto & key = [](const double lxj, const double uxj) ->
  66. MSKboundkeye
  67. {
  68. MSKboundkeye k = MSK_BK_FR;
  69. if(isfinite(lxj) && isfinite(uxj))
  70. {
  71. if(lxj == uxj)
  72. {
  73. k = MSK_BK_RA;
  74. }else{
  75. k = MSK_BK_FX;
  76. }
  77. }else if(isfinite(lxj))
  78. {
  79. k = MSK_BK_LO;
  80. }else if(isfinite(uxj))
  81. {
  82. k = MSK_BK_UP;
  83. }
  84. return k;
  85. };
  86. // loop over variables
  87. for(int j = 0;j<n;j++)
  88. {
  89. if(c.size() > 0)
  90. {
  91. // Set linear term c_j in the objective
  92. mosek_guarded(MSK_putcj(task,j,c(j)));
  93. }
  94. // Set constant bounds on variable j
  95. const double lxj = lx.size()>0?lx[j]:-numeric_limits<double>::infinity();
  96. const double uxj = ux.size()>0?ux[j]: numeric_limits<double>::infinity();
  97. mosek_guarded(MSK_putvarbound(task,j,key(lxj,uxj),lxj,uxj));
  98. if(m>0)
  99. {
  100. // Input column j of A
  101. mosek_guarded(
  102. MSK_putacol(
  103. task,
  104. j,
  105. vAcp[j+1]-vAcp[j],
  106. &vAri[vAcp[j]],
  107. &vAv[vAcp[j]])
  108. );
  109. }
  110. }
  111. // loop over constraints
  112. for(int i = 0;i<m;i++)
  113. {
  114. // Set constraint bounds for row i
  115. const double lci = lc.size()>0?lc[i]:-numeric_limits<double>::infinity();
  116. const double uci = uc.size()>0?uc[i]: numeric_limits<double>::infinity();
  117. mosek_guarded(MSK_putconbound(task,i,key(lci,uci),lci,uci));
  118. }
  119. // Now the optimizer has been prepared
  120. MSKrescodee trmcode;
  121. // run the optimizer
  122. mosek_guarded(MSK_optimizetrm(task,&trmcode));
  123. // Get status
  124. MSKsolstae solsta;
  125. MSK_getsolsta (task,MSK_SOL_ITR,&solsta);
  126. bool success = false;
  127. switch(solsta)
  128. {
  129. case MSK_SOL_STA_OPTIMAL:
  130. case MSK_SOL_STA_NEAR_OPTIMAL:
  131. x.resize(n);
  132. /* Request the basic solution. */
  133. MSK_getxx(task,MSK_SOL_BAS,x.data());
  134. success = true;
  135. break;
  136. case MSK_SOL_STA_DUAL_INFEAS_CER:
  137. case MSK_SOL_STA_PRIM_INFEAS_CER:
  138. case MSK_SOL_STA_NEAR_DUAL_INFEAS_CER:
  139. case MSK_SOL_STA_NEAR_PRIM_INFEAS_CER:
  140. //printf("Primal or dual infeasibility certificate found.\n");
  141. break;
  142. case MSK_SOL_STA_UNKNOWN:
  143. //printf("The status of the solution could not be determined.\n");
  144. break;
  145. default:
  146. //printf("Other solution status.");
  147. break;
  148. }
  149. MSK_deletetask(&task);
  150. return success;
  151. }