mosek_quadprog.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef IGL_MOSEK_QUADPROG_H
  2. #define IGL_MOSEK_QUADPROG_H
  3. #include "../igl_inline.h"
  4. #include <vector>
  5. #include <map>
  6. #include "mosek.h"
  7. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  8. #include <Eigen/Dense>
  9. #include <Eigen/Sparse>
  10. namespace igl
  11. {
  12. struct MosekData
  13. {
  14. // Integer parameters
  15. std::map<MSKiparame,int> intparam;
  16. // Double parameters
  17. std::map<MSKdparame,double> douparam;
  18. // Default values
  19. MosekData();
  20. };
  21. // Solve a convex quadratic optimization problem with linear and constant
  22. // bounds, that is:
  23. //
  24. // Minimize: ½ * xT * Q⁰ * x + cT * x + cf
  25. //
  26. // Subject to: lc ≤ Ax ≤ uc
  27. // lx ≤ x ≤ ux
  28. //
  29. // where we are trying to find the optimal vector of values x.
  30. //
  31. // Note: Q⁰ must be symmetric and the ½ is a convention of MOSEK
  32. //
  33. // Note: Because of how MOSEK accepts different parts of the system, Q should
  34. // be stored in IJV (aka Coordinate) format and should only include entries in
  35. // the lower triangle. A should be stored in Column compressed (aka Harwell
  36. // Boeing) format. As described:
  37. // http://netlib.org/linalg/html_templates/node92.html
  38. // or
  39. // http://en.wikipedia.org/wiki/Sparse_matrix
  40. // #Compressed_sparse_column_.28CSC_or_CCS.29
  41. //
  42. //
  43. // Templates:
  44. // Index type for index variables
  45. // Scalar type for floating point variables (gets cast to double?)
  46. // Input:
  47. // n number of variables, i.e. size of x
  48. // Qi vector of qnnz row indices of non-zeros in LOWER TRIANGLE ONLY of Q⁰
  49. // Qj vector of qnnz column indices of non-zeros in LOWER TRIANGLE ONLY of
  50. // Q⁰
  51. // Qv vector of qnnz values of non-zeros in LOWER TRIANGLE ONLY of Q⁰,
  52. // such that:
  53. // Q⁰(Qi[k],Qj[k]) = Qv[k] for k ∈ [0,Qnnz-1], where Qnnz is the number of
  54. // non-zeros in Q⁰
  55. // c (optional) vector of n values of c, transpose of coefficient row vector
  56. // of linear terms, EMPTY means c == 0
  57. // cf (optional) value of constant term in objective, 0 means cf == 0, so
  58. // optional only in the sense that it is mandatory
  59. // m number of constraints, therefore also number of rows in linear
  60. // constraint coefficient matrix A, and in linear constraint bound vectors
  61. // lc and uc
  62. // Av vector of non-zero values of A, in column compressed order
  63. // Ari vector of row indices corresponding to non-zero values of A,
  64. // Acp vector of indices into Ari and Av of the first entry for each column
  65. // of A, size(Acp) = (# columns of A) + 1 = n + 1
  66. // lc vector of m linear constraint lower bounds
  67. // uc vector of m linear constraint upper bounds
  68. // lx vector of n constant lower bounds
  69. // ux vector of n constant upper bounds
  70. // Output:
  71. // x vector of size n to hold output of optimization
  72. // Return:
  73. // true only if optimization was successful with no errors
  74. //
  75. // Note: All indices are 0-based
  76. //
  77. template <typename Index, typename Scalar>
  78. IGL_INLINE bool mosek_quadprog(
  79. const Index n,
  80. /* mosek won't allow this to be const*/ std::vector<Index> & Qi,
  81. /* mosek won't allow this to be const*/ std::vector<Index> & Qj,
  82. /* mosek won't allow this to be const*/ std::vector<Scalar> & Qv,
  83. const std::vector<Scalar> & c,
  84. const Scalar cf,
  85. const Index m,
  86. /* mosek won't allow this to be const*/ std::vector<Scalar> & Av,
  87. /* mosek won't allow this to be const*/ std::vector<Index> & Ari,
  88. const std::vector<Index> & Acp,
  89. const std::vector<Scalar> & lc,
  90. const std::vector<Scalar> & uc,
  91. const std::vector<Scalar> & lx,
  92. const std::vector<Scalar> & ux,
  93. MosekData & mosek_data,
  94. std::vector<Scalar> & x);
  95. // Wrapper with Eigen elements
  96. //// Templates:
  97. //// Scalar Scalar type for sparse matrix (e.g. double)
  98. //// Derived dervied type from matrix/vector (e.g. VectorXd)
  99. IGL_INLINE bool mosek_quadprog(
  100. const Eigen::SparseMatrix<double> & Q,
  101. const Eigen::VectorXd & c,
  102. const double cf,
  103. const Eigen::SparseMatrix<double> & A,
  104. const Eigen::VectorXd & lc,
  105. const Eigen::VectorXd & uc,
  106. const Eigen::VectorXd & lx,
  107. const Eigen::VectorXd & ux,
  108. MosekData & mosek_data,
  109. Eigen::VectorXd & x);
  110. }
  111. #ifdef IGL_HEADER_ONLY
  112. # include "mosek_quadprog.cpp"
  113. #endif
  114. #endif