linprog.cpp 7.6 KB

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