aligator  0.14.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
cost-direct-sum.hpp
Go to the documentation of this file.
1
3#pragma once
4
7
8namespace aligator {
9
10template <typename _Scalar> struct DirectSumCostTpl : CostAbstractTpl<_Scalar> {
11 using Scalar = _Scalar;
17
18 struct Data;
19
20 DirectSumCostTpl(const PolyCost &c1, const PolyCost &c2)
21 : BaseCost(c1->space * c2->space, c1->nu + c2->nu)
22 , c1_(c1)
23 , c2_(c2) {
24 assert(!c1.valueless_after_move() && !c2.valueless_after_move());
25 }
26
28
29 shared_ptr<BaseData> createData() const override;
30
31 void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
32 BaseData &data) const override;
33 void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u,
34 BaseData &data) const override;
35 void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u,
36 BaseData &data) const override;
37
38private:
39 using CartesianProduct = aligator::CartesianProductTpl<Scalar>;
40 auto get_product_space() const {
41 return dynamic_cast<CartesianProduct const &>(*this->space);
42 }
43 static Data &data_cast(BaseData &data) { return static_cast<Data &>(data); }
44};
45
46template <typename Scalar> struct DirectSumCostTpl<Scalar>::Data : BaseData {
47
48 shared_ptr<BaseData> data1_, data2_;
49 Data(const DirectSumCostTpl &model)
50 : BaseData(model.ndx(), model.nu)
51 , data1_(model.c1_->createData())
52 , data2_(model.c2_->createData()) {}
53};
54
55template <typename Scalar>
56void DirectSumCostTpl<Scalar>::evaluate(const ConstVectorRef &x,
57 const ConstVectorRef &u,
58 BaseData &data) const {
59 CartesianProduct const space = get_product_space();
60 Data &d = data_cast(data);
61 auto xs = space.split(x);
62 ConstVectorRef u1 = u.head(c1_->nu);
63 ConstVectorRef u2 = u.tail(c2_->nu);
64
65 c1_->evaluate(xs[0], u1, *d.data1_);
66 c2_->evaluate(xs[1], u2, *d.data2_);
67
68 d.value_ = d.data1_->value_ + d.data2_->value_;
69}
70
71template <typename Scalar>
72void DirectSumCostTpl<Scalar>::computeGradients(const ConstVectorRef &x,
73 const ConstVectorRef &u,
74 BaseData &data) const {
75 CartesianProduct const space = get_product_space();
76 Data &d = data_cast(data);
77 auto xs = space.split(x);
78 ConstVectorRef u1 = u.head(c1_->nu);
79 ConstVectorRef u2 = u.tail(c2_->nu);
80
81 BaseData &d1 = *d.data1_;
82 BaseData &d2 = *d.data2_;
83
84 c1_->computeGradients(xs[0], u1, d1);
85 c2_->computeGradients(xs[1], u2, d2);
86
87 d.Lx_.head(c1_->ndx()) = d1.Lx_;
88 d.Lx_.tail(c2_->ndx()) = d2.Lx_;
89 d.Lu_.head(c1_->nu) = d1.Lu_;
90 d.Lu_.tail(c2_->nu) = d2.Lu_;
91}
92
93template <typename Scalar>
94void DirectSumCostTpl<Scalar>::computeHessians(const ConstVectorRef &x,
95 const ConstVectorRef &u,
96 BaseData &data) const {
97 CartesianProduct const space = get_product_space();
98 Data &d = data_cast(data);
99 auto xs = space.split(x);
100 ConstVectorRef u1 = u.head(c1_->nu);
101 ConstVectorRef u2 = u.tail(c2_->nu);
102
103 BaseData &d1 = *d.data1_;
104 BaseData &d2 = *d.data2_;
105
106 c1_->computeHessians(xs[0], u1, d1);
107 c2_->computeHessians(xs[1], u2, d2);
108
109 d.Lxx_.topLeftCorner(c1_->ndx(), c1_->ndx()) = d1.Lxx_;
110 d.Lxx_.bottomRightCorner(c2_->ndx(), c2_->ndx()) = d2.Lxx_;
111
112 d.Luu_.topLeftCorner(c1_->nu, c1_->nu) = d1.Luu_;
113 d.Luu_.bottomRightCorner(c2_->nu, c2_->nu) = d2.Luu_;
114
115 d.Lxu_.topLeftCorner(c1_->ndx(), c1_->nu) = d1.Lxu_;
116 d.Lxu_.bottomRightCorner(c2_->ndx(), c2_->nu) = d2.Lxu_;
117
118 d.Lux_ = d.Lxu_.transpose();
119}
120
121template <typename Scalar>
123 return std::make_shared<Data>(*this);
124}
125
126template <typename Scalar>
131
132} // namespace aligator
133
134#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
135#include "./cost-direct-sum.txx"
136#endif
bool valueless_after_move() const noexcept
Main package namespace.
auto directSum(xyz::polymorphic< CostAbstractTpl< Scalar > > const &c1, xyz::polymorphic< CostAbstractTpl< Scalar > > const &c2)
The cartesian product of two or more manifolds.
Stage costs for control problems.
xyz::polymorphic< Manifold > space
CostAbstractTpl(U &&space, const int nu)
Data struct for CostAbstractTpl.
void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const override
Compute the cost gradients .
xyz::polymorphic< BaseCost > PolyCost
xyz::polymorphic< BaseCost > c1_
shared_ptr< BaseData > createData() const override
void evaluate(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const override
Evaluate the cost function.
void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const override
Compute the cost Hessians .
DirectSumCostTpl(const PolyCost &c1, const PolyCost &c2)
CostAbstractTpl< Scalar > BaseCost
xyz::polymorphic< BaseCost > c2_
ManifoldAbstractTpl< Scalar > Manifold
CostDataAbstractTpl< Scalar > BaseData
Base class for manifolds, to use in cost funcs, solvers...