linprog.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <[email protected]>
  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 "linprog.h"
  9. #include "find.h"
  10. #include "colon.h"
  11. #include "placeholders.h"
  12. //#define IGL_LINPROG_VERBOSE
  13. IGL_INLINE bool igl::linprog(
  14. const Eigen::VectorXd & c,
  15. const Eigen::MatrixXd & _A,
  16. const Eigen::VectorXd & b,
  17. const int k,
  18. Eigen::VectorXd & x)
  19. {
  20. // This is a very literal translation of
  21. // http://www.mathworks.com/matlabcentral/fileexchange/2166-introduction-to-linear-algebra/content/strang/linprog.m
  22. using namespace Eigen;
  23. using namespace std;
  24. bool success = true;
  25. // number of constraints
  26. const int m = _A.rows();
  27. // number of original variables
  28. const int n = _A.cols();
  29. // number of iterations
  30. int it = 0;
  31. // maximum number of iterations
  32. //const int MAXIT = 10*m;
  33. const int MAXIT = 100*m;
  34. // residual tolerance
  35. const double tol = 1e-10;
  36. const auto & sign = [](const Eigen::VectorXd & B) -> Eigen::VectorXd
  37. {
  38. Eigen::VectorXd Bsign(B.size());
  39. for(int i = 0;i<B.size();i++)
  40. {
  41. Bsign(i) = B(i)>0?1:(B(i)<0?-1:0);
  42. }
  43. return Bsign;
  44. };
  45. // initial (inverse) basis matrix
  46. VectorXd Dv = sign(sign(b).array()+0.5);
  47. Dv.head(k).setConstant(1.);
  48. MatrixXd D = Dv.asDiagonal();
  49. // Incorporate slack variables
  50. MatrixXd A(_A.rows(),_A.cols()+D.cols());
  51. A<<_A,D;
  52. // Initial basis
  53. VectorXi B = igl::colon<int>(n,n+m-1);
  54. // non-basis, may turn out that vector<> would be better here
  55. VectorXi N = igl::colon<int>(0,n-1);
  56. int j;
  57. double bmin = b.minCoeff(&j);
  58. int phase;
  59. VectorXd xb;
  60. VectorXd s;
  61. VectorXi J;
  62. if(k>0 && bmin<0)
  63. {
  64. phase = 1;
  65. xb = VectorXd::Ones(m);
  66. // super cost
  67. s.resize(n+m+1);
  68. s<<VectorXd::Zero(n+k),VectorXd::Ones(m-k+1);
  69. N.resize(n+1);
  70. N<<igl::colon<int>(0,n-1),B(j);
  71. J.resize(B.size()-1);
  72. // [0 1 2 3 4]
  73. // ^
  74. // [0 1]
  75. // [3 4]
  76. J.head(j) = B.head(j);
  77. J.tail(B.size()-j-1) = B.tail(B.size()-j-1);
  78. B(j) = n+m;
  79. MatrixXd AJ = A(igl::placeholders::all,J);
  80. const VectorXd a = b - AJ.rowwise().sum();
  81. {
  82. MatrixXd old_A = A;
  83. A.resize(A.rows(),A.cols()+a.cols());
  84. A<<old_A,a;
  85. }
  86. D.col(j) = -a/a(j);
  87. D(j,j) = 1./a(j);
  88. }else if(k==m)
  89. {
  90. phase = 2;
  91. xb = b;
  92. s.resize(c.size()+m);
  93. // cost function
  94. s<<c,VectorXd::Zero(m);
  95. }else //k = 0 or bmin >=0
  96. {
  97. phase = 1;
  98. xb = b.array().abs();
  99. s.resize(n+m);
  100. // super cost
  101. s<<VectorXd::Zero(n+k),VectorXd::Ones(m-k);
  102. }
  103. while(phase<3)
  104. {
  105. double df = -1;
  106. int t = std::numeric_limits<int>::max();
  107. // Lagrange mutipliers fro Ax=b
  108. VectorXd yb = D.transpose() * s(B);
  109. while(true)
  110. {
  111. if(MAXIT>0 && it>=MAXIT)
  112. {
  113. #ifdef IGL_LINPROG_VERBOSE
  114. cerr<<"linprog: warning! maximum iterations without convergence."<<endl;
  115. #endif
  116. success = false;
  117. break;
  118. }
  119. // no freedom for minimization
  120. if(N.size() == 0)
  121. {
  122. break;
  123. }
  124. // reduced costs
  125. VectorXd sN = s(N);
  126. MatrixXd AN = A(igl::placeholders::all,N);
  127. VectorXd r = sN - AN.transpose() * yb;
  128. int q;
  129. // determine new basic variable
  130. double rmin = r.minCoeff(&q);
  131. // optimal! infinity norm
  132. if(rmin>=-tol*(sN.array().abs().maxCoeff()+1))
  133. {
  134. break;
  135. }
  136. // increment iteration count
  137. it++;
  138. // apply Bland's rule to avoid cycling
  139. if(df>=0)
  140. {
  141. if(MAXIT == -1)
  142. {
  143. #ifdef IGL_LINPROG_VERBOSE
  144. cerr<<"linprog: warning! degenerate vertex"<<endl;
  145. #endif
  146. success = false;
  147. }
  148. igl::find((r.array()<0).eval(),J);
  149. double Nq = N(J).minCoeff();
  150. // again seems like q is assumed to be a scalar though matlab code
  151. // could produce a vector for multiple matches
  152. (N.array()==Nq).cast<int>().maxCoeff(&q);
  153. }
  154. VectorXd d = D*A.col(N(q));
  155. VectorXi I;
  156. igl::find((d.array()>tol).eval(),I);
  157. if(I.size() == 0)
  158. {
  159. #ifdef IGL_LINPROG_VERBOSE
  160. cerr<<"linprog: warning! solution is unbounded"<<endl;
  161. #endif
  162. // This seems dubious:
  163. it=-it;
  164. success = false;
  165. break;
  166. }
  167. VectorXd xbd = xb(I).array()/d(I).array();
  168. // new use of r
  169. int p;
  170. {
  171. double r;
  172. r = xbd.minCoeff(&p);
  173. p = I(p);
  174. // apply Bland's rule to avoid cycling
  175. if(df>=0)
  176. {
  177. igl::find((xbd.array()==r).eval(),J);
  178. double Bp = B(I(J)).minCoeff();
  179. // idiotic way of finding index in B of Bp
  180. // code down the line seems to assume p is a scalar though the matlab
  181. // code could find a vector of matches)
  182. (B.array()==Bp).cast<int>().maxCoeff(&p);
  183. }
  184. // update x
  185. xb -= r*d;
  186. xb(p) = r;
  187. // change in f
  188. df = r*rmin;
  189. }
  190. // row vector
  191. RowVectorXd v = D.row(p)/d(p);
  192. yb += v.transpose() * (s(N(q)) - d.transpose()*s(B));
  193. d(p)-=1;
  194. // update inverse basis matrix
  195. D = D - d*v;
  196. t = B(p);
  197. B(p) = N(q);
  198. if(t>(n+k-1))
  199. {
  200. // remove qth entry from N
  201. VectorXi old_N = N;
  202. N.resize(N.size()-1);
  203. N.head(q) = old_N.head(q);
  204. N.head(q) = old_N.head(q);
  205. N.tail(old_N.size()-q-1) = old_N.tail(old_N.size()-q-1);
  206. }else
  207. {
  208. N(q) = t;
  209. }
  210. }
  211. // iterative refinement
  212. xb = (xb+D*(b-A(igl::placeholders::all,B)*xb)).eval();
  213. // must be due to rounding
  214. VectorXi I;
  215. igl::find((xb.array()<0).eval(),I);
  216. if(I.size()>0)
  217. {
  218. // so correct
  219. xb(I) = VectorXd::Zero(I.size(),1);
  220. }
  221. // B, xb,n,m,res=A(:,B)*xb-b
  222. if(phase == 2 || it<0)
  223. {
  224. break;
  225. }
  226. if(xb.transpose()*s(B) > tol)
  227. {
  228. it = -it;
  229. #ifdef IGL_LINPROG_VERBOSE
  230. cerr<<"linprog: warning, no feasible solution"<<endl;
  231. #endif
  232. success = false;
  233. break;
  234. }
  235. // re-initialize for Phase 2
  236. phase = phase+1;
  237. s*=1e6*c.array().abs().maxCoeff();
  238. s.head(n) = c;
  239. }
  240. x.setZero(std::max(B.maxCoeff()+1,n));
  241. x(B) = xb;
  242. x = x.head(n).eval();
  243. return success;
  244. }
  245. IGL_INLINE bool igl::linprog(
  246. const Eigen::VectorXd & f,
  247. const Eigen::MatrixXd & A,
  248. const Eigen::VectorXd & b,
  249. const Eigen::MatrixXd & B,
  250. const Eigen::VectorXd & c,
  251. Eigen::VectorXd & x)
  252. {
  253. using namespace Eigen;
  254. using namespace std;
  255. const int m = A.rows();
  256. const int n = A.cols();
  257. const int p = B.rows();
  258. MatrixXd Im = MatrixXd::Identity(m,m);
  259. MatrixXd AS(m,n+m);
  260. AS<<A,Im;
  261. MatrixXd bS = b.array().abs();
  262. for(int i = 0;i<m;i++)
  263. {
  264. const auto & sign = [](double x)->double
  265. {
  266. return (x<0?-1:(x>0?1:0));
  267. };
  268. AS.row(i) *= sign(b(i));
  269. }
  270. MatrixXd In = MatrixXd::Identity(n,n);
  271. MatrixXd P(n+m,2*n+m);
  272. P<< In, -In, MatrixXd::Zero(n,m),
  273. MatrixXd::Zero(m,2*n), Im;
  274. MatrixXd ASP = AS*P;
  275. MatrixXd BSP(0,2*n+m);
  276. if(p>0)
  277. {
  278. // B ∈ ℝ^(p × n)
  279. MatrixXd BS(p,n+m);
  280. BS<<B,MatrixXd::Zero(p,m);
  281. // BS ∈ ℝ^(p × n+m)
  282. BSP = BS*P;
  283. // BSP ∈ ℝ^(p × 2n+m)
  284. }
  285. VectorXd fSP = VectorXd::Ones(2*n+m);
  286. fSP.head(2*n) = P.block(0,0,n,2*n).transpose()*f;
  287. const VectorXd & cc = fSP;
  288. MatrixXd AA(m+p,2*n+m);
  289. AA<<ASP,BSP;
  290. VectorXd bb(m+p);
  291. bb<<bS,c;
  292. VectorXd xxs;
  293. // min ccᵀxxs
  294. // s.t. AA xxs ≤ bb
  295. // xxs ≥ 0
  296. //
  297. // x = x⁺ - x⁻
  298. //
  299. // P
  300. // .--^---.
  301. // [I -I 0 [x⁺ = [x
  302. // 0 0 I] x⁻ s]
  303. // s]
  304. // Pᵀ [xᵀ sᵀ] = xxsᵀ
  305. //
  306. // min [fᵀ -fᵀ 𝟙ᵀ] [x⁺;x⁻;s]
  307. // s.t. AA [x⁺;x⁻;s] ≤ b
  308. // s.t. [x⁺;x⁻;s] ≥ 0
  309. bool ret = linprog(cc,AA,bb,0,xxs);
  310. // x = P(1:n,:) xxs
  311. x = P.block(0,0,n,2*n+m)*xxs;
  312. return ret;
  313. }