aligator  0.14.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
 
Loading...
Searching...
No Matches
constraint-set-product.hpp
Go to the documentation of this file.
1
3#pragma once
4
7
8namespace aligator {
9template <typename Derived>
10auto blockMatrixGetRow(const Eigen::MatrixBase<Derived> &matrix,
11 const std::vector<Eigen::Index> &rowBlockSizes,
12 std::size_t rowIdx) {
13 Eigen::Index startIdx = 0;
14 for (std::size_t kk = 0; kk < rowIdx; kk++) {
15 startIdx += rowBlockSizes[kk];
16 }
17 return matrix.const_cast_derived().middleRows(startIdx,
18 rowBlockSizes[rowIdx]);
19}
20
21template <typename Derived>
22auto blockVectorGetRow(const Eigen::MatrixBase<Derived> &matrix,
23 const std::vector<Eigen::Index> &blockSizes,
24 std::size_t blockIdx) {
25 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
26 Eigen::Index startIdx = 0;
27 for (std::size_t kk = 0; kk < blockIdx; kk++) {
28 startIdx += blockSizes[kk];
29 }
30 return matrix.const_cast_derived().segment(startIdx, blockSizes[blockIdx]);
31}
32
37template <typename Scalar>
41 using ActiveType = typename Base::ActiveType;
42
44 const std::vector<Eigen::Index> &blockSizes)
45 : m_components(components)
46 , m_blockSizes(blockSizes) {
47 if (components.size() != blockSizes.size()) {
48 ALIGATOR_RUNTIME_ERROR("Number of components and corresponding "
49 "block sizes should be the same.");
50 }
51 }
52
57
58 Scalar evaluate(const ConstVectorRef &zproj) const override {
59 Scalar res = 0.;
60 for (std::size_t i = 0; i < m_components.size(); i++) {
61 auto zblock = blockVectorGetRow(zproj, m_blockSizes, i);
62 res += m_components[i]->evaluate(zblock);
63 }
64 return res;
65 }
66
67 void projection(const ConstVectorRef &z, VectorRef zout) const override {
68 for (std::size_t i = 0; i < m_components.size(); i++) {
69 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
70 VectorRef outblock = blockVectorGetRow(zout, m_blockSizes, i);
71 m_components[i]->projection(inblock, outblock);
72 }
73 }
74
75 void normalConeProjection(const ConstVectorRef &z,
76 VectorRef zout) const override {
77 for (std::size_t i = 0; i < m_components.size(); i++) {
78 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
79 VectorRef outblock = blockVectorGetRow(zout, m_blockSizes, i);
80 m_components[i]->normalConeProjection(inblock, outblock);
81 }
82 }
83
84 void applyProjectionJacobian(const ConstVectorRef &z,
85 MatrixRef Jout) const override {
86 for (std::size_t i = 0; i < m_components.size(); i++) {
87 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
88 MatrixRef outblock = blockMatrixGetRow(Jout, m_blockSizes, i);
89 m_components[i]->applyProjectionJacobian(inblock, outblock);
90 }
91 }
92
93 void applyNormalConeProjectionJacobian(const ConstVectorRef &z,
94 MatrixRef Jout) const override {
95 for (std::size_t i = 0; i < m_components.size(); i++) {
96 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
97 MatrixRef outblock = blockMatrixGetRow(Jout, m_blockSizes, i);
98 m_components[i]->applyNormalConeProjectionJacobian(inblock, outblock);
99 }
100 }
101
102 void computeActiveSet(const ConstVectorRef &z,
103 Eigen::Ref<ActiveType> out) const override {
104 for (std::size_t i = 0; i < m_components.size(); i++) {
105 ConstVectorRef inblock = blockVectorGetRow(z, m_blockSizes, i);
106 decltype(out) outblock = blockVectorGetRow(out, m_blockSizes, i);
107 m_components[i]->computeActiveSet(inblock, outblock);
108 }
109 }
110
111 const std::vector<xyz::polymorphic<Base>> &components() const {
112 return m_components;
113 }
114 const std::vector<Eigen::Index> &blockSizes() const { return m_blockSizes; }
115
116private:
117 std::vector<xyz::polymorphic<Base>> m_components;
118 std::vector<Eigen::Index> m_blockSizes;
119};
120
121} // namespace aligator
Main package namespace.
auto blockVectorGetRow(const Eigen::MatrixBase< Derived > &matrix, const std::vector< Eigen::Index > &blockSizes, std::size_t blockIdx)
auto blockMatrixGetRow(const Eigen::MatrixBase< Derived > &matrix, const std::vector< Eigen::Index > &rowBlockSizes, std::size_t rowIdx)
ConstraintSetProductTpl(const ConstraintSetProductTpl &)=default
Scalar evaluate(const ConstVectorRef &zproj) const override
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 .
ConstraintSetProductTpl & operator=(const ConstraintSetProductTpl &)=default
ConstraintSetProductTpl & operator=(ConstraintSetProductTpl &&)=default
void projection(const ConstVectorRef &z, VectorRef zout) const override
Compute projection of variable z onto the constraint set.
const std::vector< xyz::polymorphic< Base > > & components() const
ConstraintSetProductTpl(ConstraintSetProductTpl &&)=default
void applyNormalConeProjectionJacobian(const ConstVectorRef &z, MatrixRef Jout) const override
Apply the jacobian of the projection on the normal cone.
const std::vector< Eigen::Index > & blockSizes() const
ConstraintSetProductTpl(const std::vector< xyz::polymorphic< Base > > components, const std::vector< Eigen::Index > &blockSizes)
void computeActiveSet(const ConstVectorRef &z, Eigen::Ref< ActiveType > out) const override
void applyProjectionJacobian(const ConstVectorRef &z, MatrixRef Jout) const override
Apply a jacobian of the projection/proximal operator to a matrix.
Eigen::Matrix< bool, Eigen::Dynamic, 1 > ActiveType