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