active_set.cpp 11 KB

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