mosek_quadprog.h 4.0 KB

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