btLemkeAlgorithm.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Copyright (C) 2004-2013 MBSim Development Team
  2. Code was converted for the Bullet Continuous Collision Detection and Physics Library
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. //The original version is here
  13. //https://code.google.com/p/mbsim-env/source/browse/trunk/kernel/mbsim/numerics/linear_complementarity_problem/lemke_algorithm.cc
  14. //This file is re-distributed under the ZLib license, with permission of the original author
  15. //Math library was replaced from fmatvec to a the file src/LinearMath/btMatrixX.h
  16. //STL/std::vector replaced by btAlignedObjectArray
  17. #include "btLemkeAlgorithm.h"
  18. #undef BT_DEBUG_OSTREAM
  19. #ifdef BT_DEBUG_OSTREAM
  20. using namespace std;
  21. #endif //BT_DEBUG_OSTREAM
  22. btScalar btMachEps()
  23. {
  24. static bool calculated = false;
  25. static btScalar machEps = btScalar(1.);
  26. if (!calculated)
  27. {
  28. do
  29. {
  30. machEps /= btScalar(2.0);
  31. // If next epsilon yields 1, then break, because current
  32. // epsilon is the machine epsilon.
  33. } while ((btScalar)(1.0 + (machEps / btScalar(2.0))) != btScalar(1.0));
  34. // printf( "\nCalculated Machine epsilon: %G\n", machEps );
  35. calculated = true;
  36. }
  37. return machEps;
  38. }
  39. btScalar btEpsRoot()
  40. {
  41. static btScalar epsroot = 0.;
  42. static bool alreadyCalculated = false;
  43. if (!alreadyCalculated)
  44. {
  45. epsroot = btSqrt(btMachEps());
  46. alreadyCalculated = true;
  47. }
  48. return epsroot;
  49. }
  50. btVectorXu btLemkeAlgorithm::solve(unsigned int maxloops /* = 0*/)
  51. {
  52. steps = 0;
  53. int dim = m_q.size();
  54. #ifdef BT_DEBUG_OSTREAM
  55. if (DEBUGLEVEL >= 1)
  56. {
  57. cout << "Dimension = " << dim << endl;
  58. }
  59. #endif //BT_DEBUG_OSTREAM
  60. btVectorXu solutionVector(2 * dim);
  61. solutionVector.setZero();
  62. //, INIT, 0.);
  63. btMatrixXu ident(dim, dim);
  64. ident.setIdentity();
  65. #ifdef BT_DEBUG_OSTREAM
  66. cout << m_M << std::endl;
  67. #endif
  68. btMatrixXu mNeg = m_M.negative();
  69. btMatrixXu A(dim, 2 * dim + 2);
  70. //
  71. A.setSubMatrix(0, 0, dim - 1, dim - 1, ident);
  72. A.setSubMatrix(0, dim, dim - 1, 2 * dim - 1, mNeg);
  73. A.setSubMatrix(0, 2 * dim, dim - 1, 2 * dim, -1.f);
  74. A.setSubMatrix(0, 2 * dim + 1, dim - 1, 2 * dim + 1, m_q);
  75. #ifdef BT_DEBUG_OSTREAM
  76. cout << A << std::endl;
  77. #endif //BT_DEBUG_OSTREAM
  78. // btVectorXu q_;
  79. // q_ >> A(0, 2 * dim + 1, dim - 1, 2 * dim + 1);
  80. btAlignedObjectArray<int> basis;
  81. //At first, all w-values are in the basis
  82. for (int i = 0; i < dim; i++)
  83. basis.push_back(i);
  84. int pivotRowIndex = -1;
  85. btScalar minValue = 1e30f;
  86. bool greaterZero = true;
  87. for (int i = 0; i < dim; i++)
  88. {
  89. btScalar v = A(i, 2 * dim + 1);
  90. if (v < minValue)
  91. {
  92. minValue = v;
  93. pivotRowIndex = i;
  94. }
  95. if (v < 0)
  96. greaterZero = false;
  97. }
  98. // int pivotRowIndex = q_.minIndex();//minIndex(q_); // first row is that with lowest q-value
  99. int z0Row = pivotRowIndex; // remember the col of z0 for ending algorithm afterwards
  100. int pivotColIndex = 2 * dim; // first col is that of z0
  101. #ifdef BT_DEBUG_OSTREAM
  102. if (DEBUGLEVEL >= 3)
  103. {
  104. // cout << "A: " << A << endl;
  105. cout << "pivotRowIndex " << pivotRowIndex << endl;
  106. cout << "pivotColIndex " << pivotColIndex << endl;
  107. cout << "Basis: ";
  108. for (int i = 0; i < basis.size(); i++)
  109. cout << basis[i] << " ";
  110. cout << endl;
  111. }
  112. #endif //BT_DEBUG_OSTREAM
  113. if (!greaterZero)
  114. {
  115. if (maxloops == 0)
  116. {
  117. maxloops = 100;
  118. // maxloops = UINT_MAX; //TODO: not a really nice way, problem is: maxloops should be 2^dim (=1<<dim), but this could exceed UINT_MAX and thus the result would be 0 and therefore the lemke algorithm wouldn't start but probably would find a solution within less then UINT_MAX steps. Therefore this constant is used as a upper border right now...
  119. }
  120. /*start looping*/
  121. for (steps = 0; steps < maxloops; steps++)
  122. {
  123. GaussJordanEliminationStep(A, pivotRowIndex, pivotColIndex, basis);
  124. #ifdef BT_DEBUG_OSTREAM
  125. if (DEBUGLEVEL >= 3)
  126. {
  127. // cout << "A: " << A << endl;
  128. cout << "pivotRowIndex " << pivotRowIndex << endl;
  129. cout << "pivotColIndex " << pivotColIndex << endl;
  130. cout << "Basis: ";
  131. for (int i = 0; i < basis.size(); i++)
  132. cout << basis[i] << " ";
  133. cout << endl;
  134. }
  135. #endif //BT_DEBUG_OSTREAM
  136. int pivotColIndexOld = pivotColIndex;
  137. /*find new column index */
  138. if (basis[pivotRowIndex] < dim) //if a w-value left the basis get in the correspondent z-value
  139. pivotColIndex = basis[pivotRowIndex] + dim;
  140. else
  141. //else do it the other way round and get in the corresponding w-value
  142. pivotColIndex = basis[pivotRowIndex] - dim;
  143. /*the column becomes part of the basis*/
  144. basis[pivotRowIndex] = pivotColIndexOld;
  145. bool isRayTermination = false;
  146. pivotRowIndex = findLexicographicMinimum(A, pivotColIndex, z0Row, isRayTermination);
  147. if (isRayTermination)
  148. {
  149. break; // ray termination
  150. }
  151. if (z0Row == pivotRowIndex)
  152. { //if z0 leaves the basis the solution is found --> one last elimination step is necessary
  153. GaussJordanEliminationStep(A, pivotRowIndex, pivotColIndex, basis);
  154. basis[pivotRowIndex] = pivotColIndex; //update basis
  155. break;
  156. }
  157. }
  158. #ifdef BT_DEBUG_OSTREAM
  159. if (DEBUGLEVEL >= 1)
  160. {
  161. cout << "Number of loops: " << steps << endl;
  162. cout << "Number of maximal loops: " << maxloops << endl;
  163. }
  164. #endif //BT_DEBUG_OSTREAM
  165. if (!validBasis(basis))
  166. {
  167. info = -1;
  168. #ifdef BT_DEBUG_OSTREAM
  169. if (DEBUGLEVEL >= 1)
  170. cerr << "Lemke-Algorithm ended with Ray-Termination (no valid solution)." << endl;
  171. #endif //BT_DEBUG_OSTREAM
  172. return solutionVector;
  173. }
  174. }
  175. #ifdef BT_DEBUG_OSTREAM
  176. if (DEBUGLEVEL >= 2)
  177. {
  178. // cout << "A: " << A << endl;
  179. cout << "pivotRowIndex " << pivotRowIndex << endl;
  180. cout << "pivotColIndex " << pivotColIndex << endl;
  181. }
  182. #endif //BT_DEBUG_OSTREAM
  183. for (int i = 0; i < basis.size(); i++)
  184. {
  185. solutionVector[basis[i]] = A(i, 2 * dim + 1); //q_[i];
  186. }
  187. info = 0;
  188. return solutionVector;
  189. }
  190. int btLemkeAlgorithm::findLexicographicMinimum(const btMatrixXu& A, const int& pivotColIndex, const int& z0Row, bool& isRayTermination)
  191. {
  192. isRayTermination = false;
  193. btAlignedObjectArray<int> activeRows;
  194. bool firstRow = true;
  195. btScalar currentMin = 0.0;
  196. int dim = A.rows();
  197. for (int row = 0; row < dim; row++)
  198. {
  199. const btScalar denom = A(row, pivotColIndex);
  200. if (denom > btMachEps())
  201. {
  202. const btScalar q = A(row, dim + dim + 1) / denom;
  203. if (firstRow)
  204. {
  205. currentMin = q;
  206. activeRows.push_back(row);
  207. firstRow = false;
  208. }
  209. else if (fabs(currentMin - q) < btMachEps())
  210. {
  211. activeRows.push_back(row);
  212. }
  213. else if (currentMin > q)
  214. {
  215. currentMin = q;
  216. activeRows.clear();
  217. activeRows.push_back(row);
  218. }
  219. }
  220. }
  221. if (activeRows.size() == 0)
  222. {
  223. isRayTermination = true;
  224. return 0;
  225. }
  226. else if (activeRows.size() == 1)
  227. {
  228. return activeRows[0];
  229. }
  230. // if there are multiple rows, check if they contain the row for z_0.
  231. for (int i = 0; i < activeRows.size(); i++)
  232. {
  233. if (activeRows[i] == z0Row)
  234. {
  235. return z0Row;
  236. }
  237. }
  238. // look through the columns of the inverse of the basic matrix from left to right until the tie is broken.
  239. for (int col = 0; col < dim ; col++)
  240. {
  241. btAlignedObjectArray<int> activeRowsCopy(activeRows);
  242. activeRows.clear();
  243. firstRow = true;
  244. for (int i = 0; i<activeRowsCopy.size();i++)
  245. {
  246. const int row = activeRowsCopy[i];
  247. // denom is positive here as an invariant.
  248. const btScalar denom = A(row, pivotColIndex);
  249. const btScalar ratio = A(row, col) / denom;
  250. if (firstRow)
  251. {
  252. currentMin = ratio;
  253. activeRows.push_back(row);
  254. firstRow = false;
  255. }
  256. else if (fabs(currentMin - ratio) < btMachEps())
  257. {
  258. activeRows.push_back(row);
  259. }
  260. else if (currentMin > ratio)
  261. {
  262. currentMin = ratio;
  263. activeRows.clear();
  264. activeRows.push_back(row);
  265. }
  266. }
  267. if (activeRows.size() == 1)
  268. {
  269. return activeRows[0];
  270. }
  271. }
  272. // must not reach here.
  273. isRayTermination = true;
  274. return 0;
  275. }
  276. void btLemkeAlgorithm::GaussJordanEliminationStep(btMatrixXu& A, int pivotRowIndex, int pivotColumnIndex, const btAlignedObjectArray<int>& basis)
  277. {
  278. btScalar a = -1 / A(pivotRowIndex, pivotColumnIndex);
  279. #ifdef BT_DEBUG_OSTREAM
  280. cout << A << std::endl;
  281. #endif
  282. for (int i = 0; i < A.rows(); i++)
  283. {
  284. if (i != pivotRowIndex)
  285. {
  286. for (int j = 0; j < A.cols(); j++)
  287. {
  288. if (j != pivotColumnIndex)
  289. {
  290. btScalar v = A(i, j);
  291. v += A(pivotRowIndex, j) * A(i, pivotColumnIndex) * a;
  292. A.setElem(i, j, v);
  293. }
  294. }
  295. }
  296. }
  297. #ifdef BT_DEBUG_OSTREAM
  298. cout << A << std::endl;
  299. #endif //BT_DEBUG_OSTREAM
  300. for (int i = 0; i < A.cols(); i++)
  301. {
  302. A.mulElem(pivotRowIndex, i, -a);
  303. }
  304. #ifdef BT_DEBUG_OSTREAM
  305. cout << A << std::endl;
  306. #endif //#ifdef BT_DEBUG_OSTREAM
  307. for (int i = 0; i < A.rows(); i++)
  308. {
  309. if (i != pivotRowIndex)
  310. {
  311. A.setElem(i, pivotColumnIndex, 0);
  312. }
  313. }
  314. #ifdef BT_DEBUG_OSTREAM
  315. cout << A << std::endl;
  316. #endif //#ifdef BT_DEBUG_OSTREAM
  317. }
  318. bool btLemkeAlgorithm::greaterZero(const btVectorXu& vector)
  319. {
  320. bool isGreater = true;
  321. for (int i = 0; i < vector.size(); i++)
  322. {
  323. if (vector[i] < 0)
  324. {
  325. isGreater = false;
  326. break;
  327. }
  328. }
  329. return isGreater;
  330. }
  331. bool btLemkeAlgorithm::validBasis(const btAlignedObjectArray<int>& basis)
  332. {
  333. bool isValid = true;
  334. for (int i = 0; i < basis.size(); i++)
  335. {
  336. if (basis[i] >= basis.size() * 2)
  337. { //then z0 is in the base
  338. isValid = false;
  339. break;
  340. }
  341. }
  342. return isValid;
  343. }