btPATHSolver.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///original version written by Erwin Coumans, October 2013
  14. #ifndef BT_PATH_SOLVER_H
  15. #define BT_PATH_SOLVER_H
  16. //#define BT_USE_PATH
  17. #ifdef BT_USE_PATH
  18. extern "C"
  19. {
  20. #include "PATH/SimpleLCP.h"
  21. #include "PATH/License.h"
  22. #include "PATH/Error_Interface.h"
  23. };
  24. void __stdcall MyError(Void *data, Char *msg)
  25. {
  26. printf("Path Error: %s\n", msg);
  27. }
  28. void __stdcall MyWarning(Void *data, Char *msg)
  29. {
  30. printf("Path Warning: %s\n", msg);
  31. }
  32. Error_Interface e;
  33. #include "btMLCPSolverInterface.h"
  34. #include "Dantzig/lcp.h"
  35. class btPathSolver : public btMLCPSolverInterface
  36. {
  37. public:
  38. btPathSolver()
  39. {
  40. License_SetString("2069810742&Courtesy_License&&&USR&2013&14_12_2011&1000&PATH&GEN&31_12_2013&0_0_0&0&0_0");
  41. e.error_data = 0;
  42. e.warning = MyWarning;
  43. e.error = MyError;
  44. Error_SetInterface(&e);
  45. }
  46. virtual bool solveMLCP(const btMatrixXu &A, const btVectorXu &b, btVectorXu &x, const btVectorXu &lo, const btVectorXu &hi, const btAlignedObjectArray<int> &limitDependency, int numIterations, bool useSparsity = true)
  47. {
  48. MCP_Termination status;
  49. int numVariables = b.rows();
  50. if (0 == numVariables)
  51. return true;
  52. /* - variables - the number of variables in the problem
  53. - m_nnz - the number of nonzeros in the M matrix
  54. - m_i - a vector of size m_nnz containing the row indices for M
  55. - m_j - a vector of size m_nnz containing the column indices for M
  56. - m_ij - a vector of size m_nnz containing the data for M
  57. - q - a vector of size variables
  58. - lb - a vector of size variables containing the lower bounds on x
  59. - ub - a vector of size variables containing the upper bounds on x
  60. */
  61. btAlignedObjectArray<double> values;
  62. btAlignedObjectArray<int> rowIndices;
  63. btAlignedObjectArray<int> colIndices;
  64. for (int i = 0; i < A.rows(); i++)
  65. {
  66. for (int j = 0; j < A.cols(); j++)
  67. {
  68. if (A(i, j) != 0.f)
  69. {
  70. //add 1, because Path starts at 1, instead of 0
  71. rowIndices.push_back(i + 1);
  72. colIndices.push_back(j + 1);
  73. values.push_back(A(i, j));
  74. }
  75. }
  76. }
  77. int numNonZero = rowIndices.size();
  78. btAlignedObjectArray<double> zResult;
  79. zResult.resize(numVariables);
  80. btAlignedObjectArray<double> rhs;
  81. btAlignedObjectArray<double> upperBounds;
  82. btAlignedObjectArray<double> lowerBounds;
  83. for (int i = 0; i < numVariables; i++)
  84. {
  85. upperBounds.push_back(hi[i]);
  86. lowerBounds.push_back(lo[i]);
  87. rhs.push_back(-b[i]);
  88. }
  89. SimpleLCP(numVariables, numNonZero, &rowIndices[0], &colIndices[0], &values[0], &rhs[0], &lowerBounds[0], &upperBounds[0], &status, &zResult[0]);
  90. if (status != MCP_Solved)
  91. {
  92. static const char *gReturnMsgs[] = {
  93. "Invalid return",
  94. "MCP_Solved: The problem was solved",
  95. "MCP_NoProgress: A stationary point was found",
  96. "MCP_MajorIterationLimit: Major iteration limit met",
  97. "MCP_MinorIterationLimit: Cumulative minor iteration limit met",
  98. "MCP_TimeLimit: Ran out of time",
  99. "MCP_UserInterrupt: Control-C, typically",
  100. "MCP_BoundError: Problem has a bound error",
  101. "MCP_DomainError: Could not find starting point",
  102. "MCP_Infeasible: Problem has no solution",
  103. "MCP_Error: An error occurred within the code",
  104. "MCP_LicenseError: License could not be found",
  105. "MCP_OK"};
  106. printf("ERROR: The PATH MCP solver failed: %s\n", gReturnMsgs[(unsigned int)status]); // << std::endl;
  107. printf("using Projected Gauss Seidel fallback\n");
  108. return false;
  109. }
  110. else
  111. {
  112. for (int i = 0; i < numVariables; i++)
  113. {
  114. x[i] = zResult[i];
  115. //check for #NAN
  116. if (x[i] != zResult[i])
  117. return false;
  118. }
  119. return true;
  120. }
  121. }
  122. };
  123. #endif //BT_USE_PATH
  124. #endif //BT_PATH_SOLVER_H