btLemkeAlgorithm.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. machEps /= btScalar(2.0);
  30. // If next epsilon yields 1, then break, because current
  31. // epsilon is the machine epsilon.
  32. }
  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. static btScalar epsroot = 0.;
  41. static bool alreadyCalculated = false;
  42. if (!alreadyCalculated) {
  43. epsroot = btSqrt(btMachEps());
  44. alreadyCalculated = true;
  45. }
  46. return epsroot;
  47. }
  48. btVectorXu btLemkeAlgorithm::solve(unsigned int maxloops /* = 0*/)
  49. {
  50. steps = 0;
  51. int dim = m_q.size();
  52. #ifdef BT_DEBUG_OSTREAM
  53. if(DEBUGLEVEL >= 1) {
  54. cout << "Dimension = " << dim << endl;
  55. }
  56. #endif //BT_DEBUG_OSTREAM
  57. btVectorXu solutionVector(2 * dim);
  58. solutionVector.setZero();
  59. //, INIT, 0.);
  60. btMatrixXu ident(dim, dim);
  61. ident.setIdentity();
  62. #ifdef BT_DEBUG_OSTREAM
  63. cout << m_M << std::endl;
  64. #endif
  65. btMatrixXu mNeg = m_M.negative();
  66. btMatrixXu A(dim, 2 * dim + 2);
  67. //
  68. A.setSubMatrix(0, 0, dim - 1, dim - 1,ident);
  69. A.setSubMatrix(0, dim, dim - 1, 2 * dim - 1,mNeg);
  70. A.setSubMatrix(0, 2 * dim, dim - 1, 2 * dim, -1.f);
  71. A.setSubMatrix(0, 2 * dim + 1, dim - 1, 2 * dim + 1,m_q);
  72. #ifdef BT_DEBUG_OSTREAM
  73. cout << A << std::endl;
  74. #endif //BT_DEBUG_OSTREAM
  75. // btVectorXu q_;
  76. // q_ >> A(0, 2 * dim + 1, dim - 1, 2 * dim + 1);
  77. btAlignedObjectArray<int> basis;
  78. //At first, all w-values are in the basis
  79. for (int i = 0; i < dim; i++)
  80. basis.push_back(i);
  81. int pivotRowIndex = -1;
  82. btScalar minValue = 1e30f;
  83. bool greaterZero = true;
  84. for (int i=0;i<dim;i++)
  85. {
  86. btScalar v =A(i,2*dim+1);
  87. if (v<minValue)
  88. {
  89. minValue=v;
  90. pivotRowIndex = i;
  91. }
  92. if (v<0)
  93. greaterZero = false;
  94. }
  95. // int pivotRowIndex = q_.minIndex();//minIndex(q_); // first row is that with lowest q-value
  96. int z0Row = pivotRowIndex; // remember the col of z0 for ending algorithm afterwards
  97. int pivotColIndex = 2 * dim; // first col is that of z0
  98. #ifdef BT_DEBUG_OSTREAM
  99. if (DEBUGLEVEL >= 3)
  100. {
  101. // cout << "A: " << A << endl;
  102. cout << "pivotRowIndex " << pivotRowIndex << endl;
  103. cout << "pivotColIndex " << pivotColIndex << endl;
  104. cout << "Basis: ";
  105. for (int i = 0; i < basis.size(); i++)
  106. cout << basis[i] << " ";
  107. cout << endl;
  108. }
  109. #endif //BT_DEBUG_OSTREAM
  110. if (!greaterZero)
  111. {
  112. if (maxloops == 0) {
  113. maxloops = 100;
  114. // 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...
  115. }
  116. /*start looping*/
  117. for(steps = 0; steps < maxloops; steps++) {
  118. GaussJordanEliminationStep(A, pivotRowIndex, pivotColIndex, basis);
  119. #ifdef BT_DEBUG_OSTREAM
  120. if (DEBUGLEVEL >= 3) {
  121. // cout << "A: " << A << endl;
  122. cout << "pivotRowIndex " << pivotRowIndex << endl;
  123. cout << "pivotColIndex " << pivotColIndex << endl;
  124. cout << "Basis: ";
  125. for (int i = 0; i < basis.size(); i++)
  126. cout << basis[i] << " ";
  127. cout << endl;
  128. }
  129. #endif //BT_DEBUG_OSTREAM
  130. int pivotColIndexOld = pivotColIndex;
  131. /*find new column index */
  132. if (basis[pivotRowIndex] < dim) //if a w-value left the basis get in the correspondent z-value
  133. pivotColIndex = basis[pivotRowIndex] + dim;
  134. else
  135. //else do it the other way round and get in the corresponding w-value
  136. pivotColIndex = basis[pivotRowIndex] - dim;
  137. /*the column becomes part of the basis*/
  138. basis[pivotRowIndex] = pivotColIndexOld;
  139. pivotRowIndex = findLexicographicMinimum(A, pivotColIndex);
  140. if(z0Row == pivotRowIndex) { //if z0 leaves the basis the solution is found --> one last elimination step is necessary
  141. GaussJordanEliminationStep(A, pivotRowIndex, pivotColIndex, basis);
  142. basis[pivotRowIndex] = pivotColIndex; //update basis
  143. break;
  144. }
  145. }
  146. #ifdef BT_DEBUG_OSTREAM
  147. if(DEBUGLEVEL >= 1) {
  148. cout << "Number of loops: " << steps << endl;
  149. cout << "Number of maximal loops: " << maxloops << endl;
  150. }
  151. #endif //BT_DEBUG_OSTREAM
  152. if(!validBasis(basis)) {
  153. info = -1;
  154. #ifdef BT_DEBUG_OSTREAM
  155. if(DEBUGLEVEL >= 1)
  156. cerr << "Lemke-Algorithm ended with Ray-Termination (no valid solution)." << endl;
  157. #endif //BT_DEBUG_OSTREAM
  158. return solutionVector;
  159. }
  160. }
  161. #ifdef BT_DEBUG_OSTREAM
  162. if (DEBUGLEVEL >= 2) {
  163. // cout << "A: " << A << endl;
  164. cout << "pivotRowIndex " << pivotRowIndex << endl;
  165. cout << "pivotColIndex " << pivotColIndex << endl;
  166. }
  167. #endif //BT_DEBUG_OSTREAM
  168. for (int i = 0; i < basis.size(); i++)
  169. {
  170. solutionVector[basis[i]] = A(i,2*dim+1);//q_[i];
  171. }
  172. info = 0;
  173. return solutionVector;
  174. }
  175. int btLemkeAlgorithm::findLexicographicMinimum(const btMatrixXu& A, const int & pivotColIndex) {
  176. int RowIndex = 0;
  177. int dim = A.rows();
  178. btAlignedObjectArray<btVectorXu> Rows;
  179. for (int row = 0; row < dim; row++)
  180. {
  181. btVectorXu vec(dim + 1);
  182. vec.setZero();//, INIT, 0.)
  183. Rows.push_back(vec);
  184. btScalar a = A(row, pivotColIndex);
  185. if (a > 0) {
  186. Rows[row][0] = A(row, 2 * dim + 1) / a;
  187. Rows[row][1] = A(row, 2 * dim) / a;
  188. for (int j = 2; j < dim + 1; j++)
  189. Rows[row][j] = A(row, j - 1) / a;
  190. #ifdef BT_DEBUG_OSTREAM
  191. // if (DEBUGLEVEL) {
  192. // cout << "Rows(" << row << ") = " << Rows[row] << endl;
  193. // }
  194. #endif
  195. }
  196. }
  197. for (int i = 0; i < Rows.size(); i++)
  198. {
  199. if (Rows[i].nrm2() > 0.) {
  200. int j = 0;
  201. for (; j < Rows.size(); j++)
  202. {
  203. if(i != j)
  204. {
  205. if(Rows[j].nrm2() > 0.)
  206. {
  207. btVectorXu test(dim + 1);
  208. for (int ii=0;ii<dim+1;ii++)
  209. {
  210. test[ii] = Rows[j][ii] - Rows[i][ii];
  211. }
  212. //=Rows[j] - Rows[i]
  213. if (! LexicographicPositive(test))
  214. break;
  215. }
  216. }
  217. }
  218. if (j == Rows.size())
  219. {
  220. RowIndex += i;
  221. break;
  222. }
  223. }
  224. }
  225. return RowIndex;
  226. }
  227. bool btLemkeAlgorithm::LexicographicPositive(const btVectorXu & v)
  228. {
  229. int i = 0;
  230. // if (DEBUGLEVEL)
  231. // cout << "v " << v << endl;
  232. while(i < v.size()-1 && fabs(v[i]) < btMachEps())
  233. i++;
  234. if (v[i] > 0)
  235. return true;
  236. return false;
  237. }
  238. void btLemkeAlgorithm::GaussJordanEliminationStep(btMatrixXu& A, int pivotRowIndex, int pivotColumnIndex, const btAlignedObjectArray<int>& basis)
  239. {
  240. btScalar a = -1 / A(pivotRowIndex, pivotColumnIndex);
  241. #ifdef BT_DEBUG_OSTREAM
  242. cout << A << std::endl;
  243. #endif
  244. for (int i = 0; i < A.rows(); i++)
  245. {
  246. if (i != pivotRowIndex)
  247. {
  248. for (int j = 0; j < A.cols(); j++)
  249. {
  250. if (j != pivotColumnIndex)
  251. {
  252. btScalar v = A(i, j);
  253. v += A(pivotRowIndex, j) * A(i, pivotColumnIndex) * a;
  254. A.setElem(i, j, v);
  255. }
  256. }
  257. }
  258. }
  259. #ifdef BT_DEBUG_OSTREAM
  260. cout << A << std::endl;
  261. #endif //BT_DEBUG_OSTREAM
  262. for (int i = 0; i < A.cols(); i++)
  263. {
  264. A.mulElem(pivotRowIndex, i,-a);
  265. }
  266. #ifdef BT_DEBUG_OSTREAM
  267. cout << A << std::endl;
  268. #endif //#ifdef BT_DEBUG_OSTREAM
  269. for (int i = 0; i < A.rows(); i++)
  270. {
  271. if (i != pivotRowIndex)
  272. {
  273. A.setElem(i, pivotColumnIndex,0);
  274. }
  275. }
  276. #ifdef BT_DEBUG_OSTREAM
  277. cout << A << std::endl;
  278. #endif //#ifdef BT_DEBUG_OSTREAM
  279. }
  280. bool btLemkeAlgorithm::greaterZero(const btVectorXu & vector)
  281. {
  282. bool isGreater = true;
  283. for (int i = 0; i < vector.size(); i++) {
  284. if (vector[i] < 0) {
  285. isGreater = false;
  286. break;
  287. }
  288. }
  289. return isGreater;
  290. }
  291. bool btLemkeAlgorithm::validBasis(const btAlignedObjectArray<int>& basis)
  292. {
  293. bool isValid = true;
  294. for (int i = 0; i < basis.size(); i++) {
  295. if (basis[i] >= basis.size() * 2) { //then z0 is in the base
  296. isValid = false;
  297. break;
  298. }
  299. }
  300. return isValid;
  301. }