active_set.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "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. #if defined(ACTIVE_SET_CPP_DEBUG) && !defined(_MSC_VER)
  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 = 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 = Derivedlx::Constant(
  73. n,1,-numeric_limits<typename Derivedlx::Scalar>::max());
  74. }else
  75. {
  76. lx = p_lx;
  77. }
  78. if(p_ux.size() == 0)
  79. {
  80. ux = 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. DerivedZ old_Z;
  109. old_Z = 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. #ifdef ACTIVE_SET_CPP_DEBUG
  120. int new_as_lx = 0;
  121. int new_as_ux = 0;
  122. int new_as_ieq = 0;
  123. #endif
  124. if(Z.size() > 0)
  125. {
  126. for(int z = 0;z < n;z++)
  127. {
  128. if(Z(z) < lx(z))
  129. {
  130. #ifdef ACTIVE_SET_CPP_DEBUG
  131. new_as_lx += (as_lx(z)?0:1);
  132. #endif
  133. //new_as_lx++;
  134. as_lx(z) = TRUE;
  135. }
  136. if(Z(z) > ux(z))
  137. {
  138. #ifdef ACTIVE_SET_CPP_DEBUG
  139. new_as_ux += (as_ux(z)?0:1);
  140. #endif
  141. //new_as_ux++;
  142. as_ux(z) = TRUE;
  143. }
  144. }
  145. if(Aieq.rows() > 0)
  146. {
  147. DerivedZ AieqZ;
  148. AieqZ = Aieq*Z;
  149. for(int a = 0;a<Aieq.rows();a++)
  150. {
  151. if(AieqZ(a) > Bieq(a))
  152. {
  153. #ifdef ACTIVE_SET_CPP_DEBUG
  154. new_as_ieq += (as_ieq(a)?0:1);
  155. #endif
  156. as_ieq(a) = TRUE;
  157. }
  158. }
  159. }
  160. #ifdef ACTIVE_SET_CPP_DEBUG
  161. cout<<" new_as_lx: "<<new_as_lx<<endl;
  162. cout<<" new_as_ux: "<<new_as_ux<<endl;
  163. #endif
  164. const double diff = (Z-old_Z).squaredNorm();
  165. #ifdef ACTIVE_SET_CPP_DEBUG
  166. cout<<"diff: "<<diff<<endl;
  167. #endif
  168. if(diff < params.solution_diff_threshold)
  169. {
  170. ret = SOLVER_STATUS_CONVERGED;
  171. break;
  172. }
  173. old_Z = Z;
  174. }
  175. const int as_lx_count = std::count(as_lx.data(),as_lx.data()+n,TRUE);
  176. const int as_ux_count = std::count(as_ux.data(),as_ux.data()+n,TRUE);
  177. const int as_ieq_count =
  178. std::count(as_ieq.data(),as_ieq.data()+as_ieq.size(),TRUE);
  179. #ifndef NDEBUG
  180. {
  181. int count = 0;
  182. for(int a = 0;a<as_ieq.size();a++)
  183. {
  184. if(as_ieq(a))
  185. {
  186. assert(as_ieq(a) == TRUE);
  187. count++;
  188. }
  189. }
  190. assert(as_ieq_count == count);
  191. }
  192. #endif
  193. // PREPARE FIXED VALUES
  194. Derivedknown known_i;
  195. known_i.resize(nk + as_lx_count + as_ux_count,1);
  196. DerivedY Y_i;
  197. Y_i.resize(nk + as_lx_count + as_ux_count,1);
  198. {
  199. known_i.block(0,0,known.rows(),known.cols()) = known;
  200. Y_i.block(0,0,Y.rows(),Y.cols()) = Y;
  201. int k = nk;
  202. // Then all lx
  203. for(int z = 0;z < n;z++)
  204. {
  205. if(as_lx(z))
  206. {
  207. known_i(k) = z;
  208. Y_i(k) = lx(z);
  209. k++;
  210. }
  211. }
  212. // Finally all ux
  213. for(int z = 0;z < n;z++)
  214. {
  215. if(as_ux(z))
  216. {
  217. known_i(k) = z;
  218. Y_i(k) = ux(z);
  219. k++;
  220. }
  221. }
  222. assert(k==Y_i.size());
  223. assert(k==known_i.size());
  224. }
  225. //cout<<matlab_format((known_i.array()+1).eval(),"known_i")<<endl;
  226. // PREPARE EQUALITY CONSTRAINTS
  227. Eigen::Matrix<typename DerivedY::Scalar, Eigen::Dynamic, 1> as_ieq_list(as_ieq_count,1);
  228. // Gather active constraints and resp. rhss
  229. DerivedBeq Beq_i;
  230. Beq_i.resize(Beq.rows()+as_ieq_count,1);
  231. Beq_i.head(Beq.rows()) = Beq;
  232. {
  233. int k =0;
  234. for(int a=0;a<as_ieq.size();a++)
  235. {
  236. if(as_ieq(a))
  237. {
  238. assert(k<as_ieq_list.size());
  239. as_ieq_list(k)=a;
  240. Beq_i(Beq.rows()+k,0) = Bieq(k,0);
  241. k++;
  242. }
  243. }
  244. assert(k == as_ieq_count);
  245. }
  246. // extract active constraint rows
  247. SparseMatrix<AeqT> Aeq_i,Aieq_i;
  248. slice(Aieq,as_ieq_list,1,Aieq_i);
  249. // Append to equality constraints
  250. cat(1,Aeq,Aieq_i,Aeq_i);
  251. min_quad_with_fixed_data<AT> data;
  252. #ifndef NDEBUG
  253. {
  254. // NO DUPES!
  255. Matrix<BOOL,Dynamic,1> fixed = Matrix<BOOL,Dynamic,1>::Constant(n,1,FALSE);
  256. for(int k = 0;k<known_i.size();k++)
  257. {
  258. assert(!fixed[known_i(k)]);
  259. fixed[known_i(k)] = TRUE;
  260. }
  261. }
  262. #endif
  263. DerivedZ sol;
  264. if(known_i.size() == A.rows())
  265. {
  266. // Everything's fixed?
  267. #ifdef ACTIVE_SET_CPP_DEBUG
  268. cout<<" everything's fixed."<<endl;
  269. #endif
  270. Z.resize(A.rows(),Y_i.cols());
  271. slice_into(Y_i,known_i,1,Z);
  272. sol.resize(0,Y_i.cols());
  273. assert(Aeq_i.rows() == 0 && "All fixed but linearly constrained");
  274. }else
  275. {
  276. #ifdef ACTIVE_SET_CPP_DEBUG
  277. cout<<" min_quad_with_fixed_precompute"<<endl;
  278. #endif
  279. if(!min_quad_with_fixed_precompute(A,known_i,Aeq_i,params.Auu_pd,data))
  280. {
  281. cerr<<"Error: min_quad_with_fixed precomputation failed."<<endl;
  282. if(iter > 0 && Aeq_i.rows() > Aeq.rows())
  283. {
  284. cerr<<" *Are you sure rows of [Aeq;Aieq] are linearly independent?*"<<
  285. endl;
  286. }
  287. ret = SOLVER_STATUS_ERROR;
  288. break;
  289. }
  290. #ifdef ACTIVE_SET_CPP_DEBUG
  291. cout<<" min_quad_with_fixed_solve"<<endl;
  292. #endif
  293. if(!min_quad_with_fixed_solve(data,B,Y_i,Beq_i,Z,sol))
  294. {
  295. cerr<<"Error: min_quad_with_fixed solve failed."<<endl;
  296. ret = SOLVER_STATUS_ERROR;
  297. break;
  298. }
  299. //cout<<matlab_format((Aeq*Z-Beq).eval(),"cr")<<endl;
  300. //cout<<matlab_format(Z,"Z")<<endl;
  301. #ifdef ACTIVE_SET_CPP_DEBUG
  302. cout<<" post"<<endl;
  303. #endif
  304. // Computing Lagrange multipliers needs to be adjusted slightly if A is not symmetric
  305. assert(data.Auu_sym);
  306. }
  307. // Compute Lagrange multiplier values for known_i
  308. SparseMatrix<AT> Ak;
  309. // Slow
  310. slice(A,known_i,1,Ak);
  311. DerivedB Bk;
  312. slice(B,known_i,Bk);
  313. MatrixXd Lambda_known_i = -(0.5*Ak*Z + 0.5*Bk);
  314. // reverse the lambda values for lx
  315. Lambda_known_i.block(nk,0,as_lx_count,1) =
  316. (-1*Lambda_known_i.block(nk,0,as_lx_count,1)).eval();
  317. // Extract Lagrange multipliers for Aieq_i (always at back of sol)
  318. VectorXd Lambda_Aieq_i(Aieq_i.rows(),1);
  319. for(int l = 0;l<Aieq_i.rows();l++)
  320. {
  321. Lambda_Aieq_i(Aieq_i.rows()-1-l) = sol(sol.rows()-1-l);
  322. }
  323. // Remove from active set
  324. for(int l = 0;l<as_lx_count;l++)
  325. {
  326. if(Lambda_known_i(nk + l) < params.inactive_threshold)
  327. {
  328. as_lx(known_i(nk + l)) = FALSE;
  329. }
  330. }
  331. for(int u = 0;u<as_ux_count;u++)
  332. {
  333. if(Lambda_known_i(nk + as_lx_count + u) <
  334. params.inactive_threshold)
  335. {
  336. as_ux(known_i(nk + as_lx_count + u)) = FALSE;
  337. }
  338. }
  339. for(int a = 0;a<as_ieq_count;a++)
  340. {
  341. if(Lambda_Aieq_i(a) < params.inactive_threshold)
  342. {
  343. as_ieq(int(as_ieq_list(a))) = FALSE;
  344. }
  345. }
  346. iter++;
  347. //cout<<iter<<endl;
  348. if(params.max_iter>0 && iter>=params.max_iter)
  349. {
  350. ret = SOLVER_STATUS_MAX_ITER;
  351. break;
  352. }
  353. }
  354. return ret;
  355. }
  356. #ifdef IGL_STATIC_LIBRARY
  357. // Explicit template instantiation
  358. 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> >&);
  359. 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> >&);
  360. #endif