mosek_quadprog.h 4.9 KB

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