proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
constraint-set-product.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace proxsuite {
6namespace nlp {
7template <typename Derived>
8auto blockMatrixGetRow(const Eigen::MatrixBase<Derived> &matrix,
9 const std::vector<Eigen::Index> &rowBlockSizes,
10 std::size_t rowIdx) {
11 Eigen::Index startIdx = 0;
12 for (std::size_t kk = 0; kk < rowIdx; kk++) {
13 startIdx += rowBlockSizes[kk];
14 }
15 return matrix.const_cast_derived().middleRows(startIdx,
16 rowBlockSizes[rowIdx]);
17}
18
19template <typename Derived>
20auto blockVectorGetRow(const Eigen::MatrixBase<Derived> &matrix,
21 const std::vector<Eigen::Index> &blockSizes,
22 std::size_t blockIdx) {
23 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
24 Eigen::Index startIdx = 0;
25 for (std::size_t kk = 0; kk < blockIdx; kk++) {
26 startIdx += blockSizes[kk];
27 }
28 return matrix.const_cast_derived().segment(startIdx, blockSizes[blockIdx]);
29}
30
35template <typename Scalar>
39 using ActiveType = typename Base::ActiveType;
40
42 const std::vector<Eigen::Index> &blockSizes)
43 : m_components(components), m_blockSizes(blockSizes) {
44 if (components.size() != blockSizes.size()) {
45 PROXSUITE_NLP_RUNTIME_ERROR("Number of components and corresponding "
46 "block sizes should be the same.");
47 }
48 }
49
54
55 Scalar evaluate(const ConstVectorRef &zproj) const override {
56 Scalar res = 0.;
57 for (std::size_t i = 0; i < m_components.size(); i++) {
58 auto zblock = blockVectorGetRow(zproj, m_blockSizes, i);
59 res += m_components[i]->evaluate(zblock);
60 }
61 return res;
62 }
63
64 void projection(const ConstVectorRef &z, VectorRef zout) const override {
65 for (std::size_t i = 0; i < m_components.size(); i++) {
66 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
67 VectorRef outblock = blockVectorGetRow(zout, m_blockSizes, i);
68 m_components[i]->projection(inblock, outblock);
69 }
70 }
71
72 void normalConeProjection(const ConstVectorRef &z,
73 VectorRef zout) const override {
74 for (std::size_t i = 0; i < m_components.size(); i++) {
75 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
76 VectorRef outblock = blockVectorGetRow(zout, m_blockSizes, i);
77 m_components[i]->normalConeProjection(inblock, outblock);
78 }
79 }
80
81 void applyProjectionJacobian(const ConstVectorRef &z,
82 MatrixRef Jout) const override {
83 for (std::size_t i = 0; i < m_components.size(); i++) {
84 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
85 MatrixRef outblock = blockMatrixGetRow(Jout, m_blockSizes, i);
86 m_components[i]->applyProjectionJacobian(inblock, outblock);
87 }
88 }
89
90 void applyNormalConeProjectionJacobian(const ConstVectorRef &z,
91 MatrixRef Jout) const override {
92 for (std::size_t i = 0; i < m_components.size(); i++) {
93 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
94 MatrixRef outblock = blockMatrixGetRow(Jout, m_blockSizes, i);
95 m_components[i]->applyNormalConeProjectionJacobian(inblock, outblock);
96 }
97 }
98
99 void computeActiveSet(const ConstVectorRef &z,
100 Eigen::Ref<ActiveType> out) const override {
101 for (std::size_t i = 0; i < m_components.size(); i++) {
102 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
103 decltype(out) outblock = blockVectorGetRow(out, m_blockSizes, i);
104 m_components[i]->computeActiveSet(inblock, outblock);
105 }
106 }
107
108 const std::vector<xyz::polymorphic<Base>> &components() const {
109 return m_components;
110 }
111 const std::vector<Eigen::Index> &blockSizes() const { return m_blockSizes; }
112
113private:
114 std::vector<xyz::polymorphic<Base>> m_components;
115 std::vector<Eigen::Index> m_blockSizes;
116};
117
118#ifdef PROXSUITE_NLP_ENABLE_TEMPLATE_INSTANTIATION
119extern template struct PROXSUITE_NLP_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI
120 ConstraintSetProductTpl<context::Scalar>;
121#endif
122
123} // namespace nlp
124} // namespace proxsuite
#define PROXSUITE_NLP_RUNTIME_ERROR(msg)
Definition exceptions.hpp:8
#define PROXSUITE_NLP_DYNAMIC_TYPEDEFS(Scalar)
Definition math.hpp:26
auto blockMatrixGetRow(const Eigen::MatrixBase< Derived > &matrix, const std::vector< Eigen::Index > &rowBlockSizes, std::size_t rowIdx)
auto blockVectorGetRow(const Eigen::MatrixBase< Derived > &matrix, const std::vector< Eigen::Index > &blockSizes, std::size_t blockIdx)
Main package namespace.
Definition bcl-params.hpp:5
Cartesian product of multiple constraint sets. This class makes computing multipliers and Jacobian ma...
void computeActiveSet(const ConstVectorRef &z, Eigen::Ref< ActiveType > out) const override
const std::vector< Eigen::Index > & blockSizes() const
ConstraintSetProductTpl(const ConstraintSetProductTpl &)=default
ConstraintSetProductTpl & operator=(const ConstraintSetProductTpl &)=default
const std::vector< xyz::polymorphic< Base > > & components() const
void applyNormalConeProjectionJacobian(const ConstVectorRef &z, MatrixRef Jout) const override
Apply the jacobian of the projection on the normal cone.
void normalConeProjection(const ConstVectorRef &z, VectorRef zout) const override
Compute projection of z onto the normal cone to the set. The default implementation is just .
void applyProjectionJacobian(const ConstVectorRef &z, MatrixRef Jout) const override
Apply a jacobian of the projection/proximal operator to a matrix.
ConstraintSetProductTpl(const std::vector< xyz::polymorphic< Base > > components, const std::vector< Eigen::Index > &blockSizes)
ConstraintSetProductTpl & operator=(ConstraintSetProductTpl &&)=default
void projection(const ConstVectorRef &z, VectorRef zout) const override
Compute projection of variable z onto the constraint set.
ConstraintSetProductTpl(ConstraintSetProductTpl &&)=default
Scalar evaluate(const ConstVectorRef &zproj) const override
Base constraint set type.
Definition fwd.hpp:104
Eigen::Matrix< bool, Eigen::Dynamic, 1 > ActiveType