linprog.cpp 7.9 KB

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