aligator
0.19.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Toggle main menu visibility
Loading...
Searching...
No Matches
constraint-set-product.hpp
Go to the documentation of this file.
1
3
#pragma once
4
5
#include "
aligator/core/constraint-set.hpp
"
6
#include "
aligator/third-party/polymorphic_cxx14.h
"
7
#include "
aligator/utils/span.hpp
"
8
9
namespace
aligator
{
10
template
<
typename
Derived>
11
auto
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
22
template
<
typename
Derived>
23
auto
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
39
template
<
typename
Scalar>
40
struct
ConstraintSetProductTpl
:
ConstraintSetTpl
<Scalar> {
41
ALIGATOR_DYNAMIC_TYPEDEFS
(
Scalar
);
42
using
Base
=
ConstraintSetTpl<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
55
ConstraintSetProductTpl
(
const
ConstraintSetProductTpl
&) =
default
;
56
ConstraintSetProductTpl
&
operator=
(
const
ConstraintSetProductTpl
&) =
default
;
57
ConstraintSetProductTpl
(
ConstraintSetProductTpl
&&) =
default
;
58
ConstraintSetProductTpl
&
operator=
(
ConstraintSetProductTpl
&&) =
default
;
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
118
private
:
119
std::vector<xyz::polymorphic<Base>> m_components;
120
std::vector<Eigen::Index> m_blockSizes;
121
};
122
123
}
// namespace aligator
constraint-set.hpp
aligator
Main package namespace.
Definition
action-model-wrap.hpp:14
aligator::blockMatrixGetRow
auto blockMatrixGetRow(const Eigen::MatrixBase< Derived > &matrix, boost::span< const Eigen::Index > rowBlockSizes, std::size_t rowIdx)
Definition
constraint-set-product.hpp:11
aligator::blockVectorGetRow
auto blockVectorGetRow(const Eigen::MatrixBase< Derived > &matrix, boost::span< const Eigen::Index > blockSizes, std::size_t blockIdx)
Definition
constraint-set-product.hpp:23
polymorphic_cxx14.h
aligator::ConstraintSetProductTpl::ConstraintSetProductTpl
ConstraintSetProductTpl(const ConstraintSetProductTpl &)=default
aligator::ConstraintSetProductTpl::evaluate
Scalar evaluate(const ConstVectorRef &zproj) const override
Definition
constraint-set-product.hpp:60
aligator::ConstraintSetProductTpl::ActiveType
typename Base::ActiveType ActiveType
Definition
constraint-set-product.hpp:43
aligator::ConstraintSetProductTpl::normalConeProjection
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 .
Definition
constraint-set-product.hpp:77
aligator::ConstraintSetProductTpl::operator=
ConstraintSetProductTpl & operator=(const ConstraintSetProductTpl &)=default
aligator::ConstraintSetProductTpl::operator=
ConstraintSetProductTpl & operator=(ConstraintSetProductTpl &&)=default
aligator::ConstraintSetProductTpl::Base
ConstraintSetTpl< Scalar > Base
Definition
constraint-set-product.hpp:42
aligator::ConstraintSetProductTpl::projection
void projection(const ConstVectorRef &z, VectorRef zout) const override
Compute projection of variable z onto the constraint set.
Definition
constraint-set-product.hpp:69
aligator::ConstraintSetProductTpl::components
const std::vector< xyz::polymorphic< Base > > & components() const
Definition
constraint-set-product.hpp:113
aligator::ConstraintSetProductTpl::ConstraintSetProductTpl
ConstraintSetProductTpl(ConstraintSetProductTpl &&)=default
aligator::ConstraintSetProductTpl::ALIGATOR_DYNAMIC_TYPEDEFS
ALIGATOR_DYNAMIC_TYPEDEFS(Scalar)
aligator::ConstraintSetProductTpl::applyNormalConeProjectionJacobian
void applyNormalConeProjectionJacobian(const ConstVectorRef &z, MatrixRef Jout) const override
Apply the jacobian of the projection on the normal cone.
Definition
constraint-set-product.hpp:95
aligator::ConstraintSetProductTpl::blockSizes
const std::vector< Eigen::Index > & blockSizes() const
Definition
constraint-set-product.hpp:116
aligator::ConstraintSetProductTpl::ConstraintSetProductTpl
ConstraintSetProductTpl(const std::vector< xyz::polymorphic< Base > > components, const std::vector< Eigen::Index > &blockSizes)
Definition
constraint-set-product.hpp:45
aligator::ConstraintSetProductTpl::computeActiveSet
void computeActiveSet(const ConstVectorRef &z, Eigen::Ref< ActiveType > out) const override
Definition
constraint-set-product.hpp:104
aligator::ConstraintSetProductTpl::applyProjectionJacobian
void applyProjectionJacobian(const ConstVectorRef &z, MatrixRef Jout) const override
Apply a jacobian of the projection/proximal operator to a matrix.
Definition
constraint-set-product.hpp:86
aligator::ConstraintSetTpl< Scalar >::ConstraintSetTpl
ConstraintSetTpl()=default
aligator::ConstraintSetTpl< Scalar >::ActiveType
Eigen::Matrix< bool, Eigen::Dynamic, 1 > ActiveType
Definition
constraint-set.hpp:20
aligator::ConstraintSetTpl< Scalar >::Scalar
Scalar Scalar
Definition
constraint-set.hpp:18
span.hpp
include
aligator
modelling
constraints
constraint-set-product.hpp
Generated by
1.17.0