active_set.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "active_set.h"
  9. #include "min_quad_with_fixed.h"
  10. #include "slice.h"
  11. #include "slice_into.h"
  12. #include "cat.h"
  13. #include "matlab_format.h"
  14. #include <iostream>
  15. #include <limits>
  16. #include <algorithm>
  17. template <
  18. typename AT,
  19. typename DerivedB,
  20. typename Derivedknown,
  21. typename DerivedY,
  22. typename AeqT,
  23. typename DerivedBeq,
  24. typename AieqT,
  25. typename DerivedBieq,
  26. typename Derivedlx,
  27. typename Derivedux,
  28. typename DerivedZ
  29. >
  30. IGL_INLINE igl::SolverStatus igl::active_set(
  31. const Eigen::SparseMatrix<AT>& A,
  32. const Eigen::PlainObjectBase<DerivedB> & B,
  33. const Eigen::PlainObjectBase<Derivedknown> & known,
  34. const Eigen::PlainObjectBase<DerivedY> & Y,
  35. const Eigen::SparseMatrix<AeqT>& Aeq,
  36. const Eigen::PlainObjectBase<DerivedBeq> & Beq,
  37. const Eigen::SparseMatrix<AieqT>& Aieq,
  38. const Eigen::PlainObjectBase<DerivedBieq> & Bieq,
  39. const Eigen::PlainObjectBase<Derivedlx> & p_lx,
  40. const Eigen::PlainObjectBase<Derivedux> & p_ux,
  41. const igl::active_set_params & params,
  42. Eigen::PlainObjectBase<DerivedZ> & Z
  43. )
  44. {
  45. //#define ACTIVE_SET_CPP_DEBUG
  46. #ifdef ACTIVE_SET_CPP_DEBUG
  47. # warning "ACTIVE_SET_CPP_DEBUG"
  48. #endif
  49. using namespace igl;
  50. using namespace Eigen;
  51. using namespace std;
  52. SolverStatus ret = SOLVER_STATUS_ERROR;
  53. const int n = A.rows();
  54. assert(n == A.cols() && "A must be square");
  55. // Discard const qualifiers
  56. //if(B.size() == 0)
  57. //{
  58. // B = Eigen::PlainObjectBase<DerivedB>::Zero(n,1);
  59. //}
  60. assert(n == B.rows() && "B.rows() must match A.rows()");
  61. assert(B.cols() == 1 && "B must be a column vector");
  62. assert(Y.cols() == 1 && "Y must be a column vector");
  63. assert((Aeq.size() == 0 && Beq.size() == 0) || Aeq.cols() == n);
  64. assert((Aeq.size() == 0 && Beq.size() == 0) || Aeq.rows() == Beq.rows());
  65. assert((Aeq.size() == 0 && Beq.size() == 0) || Beq.cols() == 1);
  66. assert((Aieq.size() == 0 && Bieq.size() == 0) || Aieq.cols() == n);
  67. assert((Aieq.size() == 0 && Bieq.size() == 0) || Aieq.rows() == Bieq.rows());
  68. assert((Aieq.size() == 0 && Bieq.size() == 0) || Bieq.cols() == 1);
  69. Eigen::PlainObjectBase<Derivedlx> lx;
  70. Eigen::PlainObjectBase<Derivedux> ux;
  71. if(p_lx.size() == 0)
  72. {
  73. lx = Eigen::PlainObjectBase<Derivedlx>::Constant(
  74. n,1,-numeric_limits<typename Derivedlx::Scalar>::max());
  75. }else
  76. {
  77. lx = p_lx;
  78. }
  79. if(ux.size() == 0)
  80. {
  81. ux = Eigen::PlainObjectBase<Derivedux>::Constant(
  82. n,1,numeric_limits<typename Derivedux::Scalar>::max());
  83. }else
  84. {
  85. ux = p_ux;
  86. }
  87. assert(lx.rows() == n && "lx must have n rows");
  88. assert(ux.rows() == n && "ux must have n rows");
  89. assert(ux.cols() == 1 && "lx must be a column vector");
  90. assert(lx.cols() == 1 && "ux must be a column vector");
  91. assert((ux.array()-lx.array()).minCoeff() > 0 && "ux(i) must be > lx(i)");
  92. if(Z.size() != 0)
  93. {
  94. // Initial guess should have correct size
  95. assert(Z.rows() == n && "Z must have n rows");
  96. assert(Z.cols() == 1 && "Z must be a column vector");
  97. }
  98. assert(known.cols() == 1 && "known must be a column vector");
  99. // Number of knowns
  100. const int nk = known.size();
  101. // Initialize active sets
  102. typedef int BOOL;
  103. #define TRUE 1
  104. #define FALSE 0
  105. Matrix<BOOL,Dynamic,1> as_lx = Matrix<BOOL,Dynamic,1>::Constant(n,1,FALSE);
  106. Matrix<BOOL,Dynamic,1> as_ux = Matrix<BOOL,Dynamic,1>::Constant(n,1,FALSE);
  107. Matrix<BOOL,Dynamic,1> as_ieq = Matrix<BOOL,Dynamic,1>::Constant(Aieq.rows(),1,FALSE);
  108. // Keep track of previous Z for comparison
  109. PlainObjectBase<DerivedZ> old_Z;
  110. old_Z = PlainObjectBase<DerivedZ>::Constant(
  111. n,1,numeric_limits<typename DerivedZ::Scalar>::max());
  112. int iter = 0;
  113. while(true)
  114. {
  115. #ifdef ACTIVE_SET_CPP_DEBUG
  116. cout<<"Iteration: "<<iter<<":"<<endl;
  117. cout<<" pre"<<endl;
  118. #endif
  119. // FIND BREACHES OF CONSTRAINTS
  120. int new_as_lx = 0;
  121. int new_as_ux = 0;
  122. int new_as_ieq = 0;
  123. if(Z.size() > 0)
  124. {
  125. for(int z = 0;z < n;z++)
  126. {
  127. if(Z(z) < lx(z))
  128. {
  129. new_as_lx += (as_lx(z)?0:1);
  130. //new_as_lx++;
  131. as_lx(z) = TRUE;
  132. }
  133. if(Z(z) > ux(z))
  134. {
  135. new_as_ux += (as_ux(z)?0:1);
  136. //new_as_ux++;
  137. as_ux(z) = TRUE;
  138. }
  139. }
  140. if(Aieq.rows() > 0)
  141. {
  142. PlainObjectBase<DerivedZ> AieqZ;
  143. AieqZ = Aieq*Z;
  144. for(int a = 0;a<Aieq.rows();a++)
  145. {
  146. if(AieqZ(a) > Bieq(a))
  147. {
  148. new_as_ieq += (as_ieq(a)?0:1);
  149. as_ieq(a) = TRUE;
  150. }
  151. }
  152. }
  153. #ifdef ACTIVE_SET_CPP_DEBUG
  154. cout<<" new_as_lx: "<<new_as_lx<<endl;
  155. cout<<" new_as_ux: "<<new_as_ux<<endl;
  156. #endif
  157. const double diff = (Z-old_Z).squaredNorm();
  158. #ifdef ACTIVE_SET_CPP_DEBUG
  159. cout<<"diff: "<<diff<<endl;
  160. #endif
  161. if(diff < params.solution_diff_threshold)
  162. {
  163. ret = SOLVER_STATUS_CONVERGED;
  164. break;
  165. }
  166. old_Z = Z;
  167. }
  168. const int as_lx_count = count(as_lx.data(),as_lx.data()+n,TRUE);
  169. const int as_ux_count = count(as_ux.data(),as_ux.data()+n,TRUE);
  170. const int as_ieq_count =
  171. count(as_ieq.data(),as_ieq.data()+as_ieq.size(),TRUE);
  172. #ifndef NDEBUG
  173. {
  174. int count = 0;
  175. for(int a = 0;a<as_ieq.size();a++)
  176. {
  177. if(as_ieq(a))
  178. {
  179. assert(as_ieq(a) == TRUE);
  180. count++;
  181. }
  182. }
  183. assert(as_ieq_count == count);
  184. }
  185. #endif
  186. // PREPARE FIXED VALUES
  187. PlainObjectBase<Derivedknown> known_i;
  188. known_i.resize(nk + as_lx_count + as_ux_count,1);
  189. PlainObjectBase<DerivedY> Y_i;
  190. Y_i.resize(nk + as_lx_count + as_ux_count,1);
  191. {
  192. known_i.block(0,0,known.rows(),known.cols()) = known;
  193. Y_i.block(0,0,Y.rows(),Y.cols()) = Y;
  194. int k = nk;
  195. // Then all lx
  196. for(int z = 0;z < n;z++)
  197. {
  198. if(as_lx(z))
  199. {
  200. known_i(k) = z;
  201. Y_i(k) = lx(z);
  202. k++;
  203. }
  204. }
  205. // Finally all ux
  206. for(int z = 0;z < n;z++)
  207. {
  208. if(as_ux(z))
  209. {
  210. known_i(k) = z;
  211. Y_i(k) = ux(z);
  212. k++;
  213. }
  214. }
  215. assert(k==Y_i.size());
  216. assert(k==known_i.size());
  217. }
  218. //cout<<matlab_format((known_i.array()+1).eval(),"known_i")<<endl;
  219. // PREPARE EQUALITY CONSTRAINTS
  220. VectorXi as_ieq_list(as_ieq_count,1);
  221. // Gather active constraints and resp. rhss
  222. PlainObjectBase<DerivedBeq> Beq_i;
  223. Beq_i.resize(Beq.rows()+as_ieq_count,1);
  224. Beq_i.head(Beq.rows()) = Beq;
  225. {
  226. int k =0;
  227. for(int a=0;a<as_ieq.size();a++)
  228. {
  229. if(as_ieq(a))
  230. {
  231. assert(k<as_ieq_list.size());
  232. as_ieq_list(k)=a;
  233. Beq_i(Beq.rows()+k,0) = Bieq(k,0);
  234. k++;
  235. }
  236. }
  237. assert(k == as_ieq_count);
  238. }
  239. // extract active constraint rows
  240. SparseMatrix<AeqT> Aeq_i,Aieq_i;
  241. slice(Aieq,as_ieq_list,1,Aieq_i);
  242. // Append to equality constraints
  243. cat(1,Aeq,Aieq_i,Aeq_i);
  244. min_quad_with_fixed_data<AT> data;
  245. #ifndef NDEBUG
  246. {
  247. // NO DUPES!
  248. Matrix<BOOL,Dynamic,1> fixed = Matrix<BOOL,Dynamic,1>::Constant(n,1,FALSE);
  249. for(int k = 0;k<known_i.size();k++)
  250. {
  251. assert(!fixed[known_i(k)]);
  252. fixed[known_i(k)] = TRUE;
  253. }
  254. }
  255. #endif
  256. Eigen::PlainObjectBase<DerivedZ> sol;
  257. if(known_i.size() == A.rows())
  258. {
  259. // Everything's fixed?
  260. #ifdef ACTIVE_SET_CPP_DEBUG
  261. cout<<" everything's fixed."<<endl;
  262. #endif
  263. Z.resize(A.rows(),Y_i.cols());
  264. slice_into(Y_i,known_i,1,Z);
  265. sol.resize(0,Y_i.cols());
  266. assert(Aeq_i.rows() == 0 && "All fixed but linearly constrained");
  267. }else
  268. {
  269. #ifdef ACTIVE_SET_CPP_DEBUG
  270. cout<<" min_quad_with_fixed_precompute"<<endl;
  271. #endif
  272. if(!min_quad_with_fixed_precompute(A,known_i,Aeq_i,params.Auu_pd,data))
  273. {
  274. cerr<<"Error: min_quad_with_fixed precomputation failed."<<endl;
  275. if(iter > 0 && Aeq_i.rows() > Aeq.rows())
  276. {
  277. cerr<<" *Are you sure rows of [Aeq;Aieq] are linearly independent?*"<<
  278. endl;
  279. }
  280. ret = SOLVER_STATUS_ERROR;
  281. break;
  282. }
  283. #ifdef ACTIVE_SET_CPP_DEBUG
  284. cout<<" min_quad_with_fixed_solve"<<endl;
  285. #endif
  286. if(!min_quad_with_fixed_solve(data,B,Y_i,Beq_i,Z,sol))
  287. {
  288. cerr<<"Error: min_quad_with_fixed solve failed."<<endl;
  289. ret = SOLVER_STATUS_ERROR;
  290. break;
  291. }
  292. //cout<<matlab_format((Aeq*Z-Beq).eval(),"cr")<<endl;
  293. //cout<<matlab_format(Z,"Z")<<endl;
  294. #ifdef ACTIVE_SET_CPP_DEBUG
  295. cout<<" post"<<endl;
  296. #endif
  297. // Computing Lagrange multipliers needs to be adjusted slightly if A is not symmetric
  298. assert(data.Auu_sym);
  299. }
  300. // Compute Lagrange multiplier values for known_i
  301. SparseMatrix<AT> Ak;
  302. // Slow
  303. slice(A,known_i,1,Ak);
  304. Eigen::PlainObjectBase<DerivedB> Bk;
  305. slice(B,known_i,Bk);
  306. MatrixXd Lambda_known_i = -(0.5*Ak*Z + 0.5*Bk);
  307. // reverse the lambda values for lx
  308. Lambda_known_i.block(nk,0,as_lx_count,1) =
  309. (-1*Lambda_known_i.block(nk,0,as_lx_count,1)).eval();
  310. // Extract Lagrange multipliers for Aieq_i (always at back of sol)
  311. VectorXd Lambda_Aieq_i(Aieq_i.rows(),1);
  312. for(int l = 0;l<Aieq_i.rows();l++)
  313. {
  314. Lambda_Aieq_i(Aieq_i.rows()-1-l) = sol(sol.rows()-1-l);
  315. }
  316. // Remove from active set
  317. for(int l = 0;l<as_lx_count;l++)
  318. {
  319. if(Lambda_known_i(nk + l) < params.inactive_threshold)
  320. {
  321. as_lx(known_i(nk + l)) = FALSE;
  322. }
  323. }
  324. for(int u = 0;u<as_ux_count;u++)
  325. {
  326. if(Lambda_known_i(nk + as_lx_count + u) <
  327. params.inactive_threshold)
  328. {
  329. as_ux(known_i(nk + as_lx_count + u)) = FALSE;
  330. }
  331. }
  332. for(int a = 0;a<as_ieq_count;a++)
  333. {
  334. if(Lambda_Aieq_i(a) < params.inactive_threshold)
  335. {
  336. as_ieq(as_ieq_list(a)) = FALSE;
  337. }
  338. }
  339. iter++;
  340. //cout<<iter<<endl;
  341. if(params.max_iter>0 && iter>=params.max_iter)
  342. {
  343. ret = SOLVER_STATUS_MAX_ITER;
  344. break;
  345. }
  346. }
  347. return ret;
  348. }
  349. #ifdef IGL_STATIC_LIBRARY
  350. // Explicit template specialization
  351. template igl::SolverStatus igl::active_set<double, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, double, Eigen::Matrix<double, -1, 1, 0, -1, 1>, double, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<double, 0, int> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<double, 0, int> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, igl::active_set_params const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  352. #endif