btLemkeAlgorithm.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. pivotRowIndex = findLexicographicMinimum(A, pivotColIndex);
  146. if (z0Row == pivotRowIndex)
  147. { //if z0 leaves the basis the solution is found --> one last elimination step is necessary
  148. GaussJordanEliminationStep(A, pivotRowIndex, pivotColIndex, basis);
  149. basis[pivotRowIndex] = pivotColIndex; //update basis
  150. break;
  151. }
  152. }
  153. #ifdef BT_DEBUG_OSTREAM
  154. if (DEBUGLEVEL >= 1)
  155. {
  156. cout << "Number of loops: " << steps << endl;
  157. cout << "Number of maximal loops: " << maxloops << endl;
  158. }
  159. #endif //BT_DEBUG_OSTREAM
  160. if (!validBasis(basis))
  161. {
  162. info = -1;
  163. #ifdef BT_DEBUG_OSTREAM
  164. if (DEBUGLEVEL >= 1)
  165. cerr << "Lemke-Algorithm ended with Ray-Termination (no valid solution)." << endl;
  166. #endif //BT_DEBUG_OSTREAM
  167. return solutionVector;
  168. }
  169. }
  170. #ifdef BT_DEBUG_OSTREAM
  171. if (DEBUGLEVEL >= 2)
  172. {
  173. // cout << "A: " << A << endl;
  174. cout << "pivotRowIndex " << pivotRowIndex << endl;
  175. cout << "pivotColIndex " << pivotColIndex << endl;
  176. }
  177. #endif //BT_DEBUG_OSTREAM
  178. for (int i = 0; i < basis.size(); i++)
  179. {
  180. solutionVector[basis[i]] = A(i, 2 * dim + 1); //q_[i];
  181. }
  182. info = 0;
  183. return solutionVector;
  184. }
  185. int btLemkeAlgorithm::findLexicographicMinimum(const btMatrixXu& A, const int& pivotColIndex)
  186. {
  187. int RowIndex = 0;
  188. int dim = A.rows();
  189. btAlignedObjectArray<btVectorXu> Rows;
  190. for (int row = 0; row < dim; row++)
  191. {
  192. btVectorXu vec(dim + 1);
  193. vec.setZero(); //, INIT, 0.)
  194. Rows.push_back(vec);
  195. btScalar a = A(row, pivotColIndex);
  196. if (a > 0)
  197. {
  198. Rows[row][0] = A(row, 2 * dim + 1) / a;
  199. Rows[row][1] = A(row, 2 * dim) / a;
  200. for (int j = 2; j < dim + 1; j++)
  201. Rows[row][j] = A(row, j - 1) / a;
  202. #ifdef BT_DEBUG_OSTREAM
  203. // if (DEBUGLEVEL) {
  204. // cout << "Rows(" << row << ") = " << Rows[row] << endl;
  205. // }
  206. #endif
  207. }
  208. }
  209. for (int i = 0; i < Rows.size(); i++)
  210. {
  211. if (Rows[i].nrm2() > 0.)
  212. {
  213. int j = 0;
  214. for (; j < Rows.size(); j++)
  215. {
  216. if (i != j)
  217. {
  218. if (Rows[j].nrm2() > 0.)
  219. {
  220. btVectorXu test(dim + 1);
  221. for (int ii = 0; ii < dim + 1; ii++)
  222. {
  223. test[ii] = Rows[j][ii] - Rows[i][ii];
  224. }
  225. //=Rows[j] - Rows[i]
  226. if (!LexicographicPositive(test))
  227. break;
  228. }
  229. }
  230. }
  231. if (j == Rows.size())
  232. {
  233. RowIndex += i;
  234. break;
  235. }
  236. }
  237. }
  238. return RowIndex;
  239. }
  240. bool btLemkeAlgorithm::LexicographicPositive(const btVectorXu& v)
  241. {
  242. int i = 0;
  243. // if (DEBUGLEVEL)
  244. // cout << "v " << v << endl;
  245. while (i < v.size() - 1 && fabs(v[i]) < btMachEps())
  246. i++;
  247. if (v[i] > 0)
  248. return true;
  249. return false;
  250. }
  251. void btLemkeAlgorithm::GaussJordanEliminationStep(btMatrixXu& A, int pivotRowIndex, int pivotColumnIndex, const btAlignedObjectArray<int>& basis)
  252. {
  253. btScalar a = -1 / A(pivotRowIndex, pivotColumnIndex);
  254. #ifdef BT_DEBUG_OSTREAM
  255. cout << A << std::endl;
  256. #endif
  257. for (int i = 0; i < A.rows(); i++)
  258. {
  259. if (i != pivotRowIndex)
  260. {
  261. for (int j = 0; j < A.cols(); j++)
  262. {
  263. if (j != pivotColumnIndex)
  264. {
  265. btScalar v = A(i, j);
  266. v += A(pivotRowIndex, j) * A(i, pivotColumnIndex) * a;
  267. A.setElem(i, j, v);
  268. }
  269. }
  270. }
  271. }
  272. #ifdef BT_DEBUG_OSTREAM
  273. cout << A << std::endl;
  274. #endif //BT_DEBUG_OSTREAM
  275. for (int i = 0; i < A.cols(); i++)
  276. {
  277. A.mulElem(pivotRowIndex, i, -a);
  278. }
  279. #ifdef BT_DEBUG_OSTREAM
  280. cout << A << std::endl;
  281. #endif //#ifdef BT_DEBUG_OSTREAM
  282. for (int i = 0; i < A.rows(); i++)
  283. {
  284. if (i != pivotRowIndex)
  285. {
  286. A.setElem(i, pivotColumnIndex, 0);
  287. }
  288. }
  289. #ifdef BT_DEBUG_OSTREAM
  290. cout << A << std::endl;
  291. #endif //#ifdef BT_DEBUG_OSTREAM
  292. }
  293. bool btLemkeAlgorithm::greaterZero(const btVectorXu& vector)
  294. {
  295. bool isGreater = true;
  296. for (int i = 0; i < vector.size(); i++)
  297. {
  298. if (vector[i] < 0)
  299. {
  300. isGreater = false;
  301. break;
  302. }
  303. }
  304. return isGreater;
  305. }
  306. bool btLemkeAlgorithm::validBasis(const btAlignedObjectArray<int>& basis)
  307. {
  308. bool isValid = true;
  309. for (int i = 0; i < basis.size(); i++)
  310. {
  311. if (basis[i] >= basis.size() * 2)
  312. { //then z0 is in the base
  313. isValid = false;
  314. break;
  315. }
  316. }
  317. return isValid;
  318. }