aligator  0.6.1
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
6#include <proxsuite-nlp/modelling/spaces/cartesian-product.hpp>
7
8namespace aligator {
9
10template <typename _Scalar> struct DirectSumCostTpl : CostAbstractTpl<_Scalar> {
11 using Scalar = _Scalar;
13 using BaseCost = CostAbstractTpl<Scalar>;
14 using BaseData = CostDataAbstractTpl<Scalar>;
15 using Manifold = ManifoldAbstractTpl<Scalar>;
16
17 struct Data;
18
19 DirectSumCostTpl(shared_ptr<BaseCost> c1, shared_ptr<BaseCost> c2)
20 : BaseCost(c1->space * c2->space, c1->nu + c2->nu), c1_(c1), c2_(c2) {
21 assert(c1 != nullptr && c2 != nullptr);
22 }
23
24 shared_ptr<BaseCost> c1_, c2_;
25
26 shared_ptr<BaseData> createData() const override;
27
28 void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
29 BaseData &data) const override;
30 void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u,
31 BaseData &data) const override;
32 void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u,
33 BaseData &data) const override;
34
35private:
36 using CartesianProduct = proxsuite::nlp::CartesianProductTpl<Scalar>;
37 auto get_product_space() const {
38 return static_cast<CartesianProduct const *>(this->space.get());
39 }
40 static Data &data_cast(BaseData &data) { return static_cast<Data &>(data); }
41};
42
43template <typename Scalar> struct DirectSumCostTpl<Scalar>::Data : BaseData {
44
45 shared_ptr<BaseData> data1_, data2_;
46 Data(const DirectSumCostTpl &model)
47 : BaseData(model.ndx(), model.nu), data1_(model.c1_->createData()),
48 data2_(model.c2_->createData()) {}
49};
50
51template <typename Scalar>
52void DirectSumCostTpl<Scalar>::evaluate(const ConstVectorRef &x,
53 const ConstVectorRef &u,
54 BaseData &data) const {
55 CartesianProduct const *space = get_product_space();
56 Data &d = data_cast(data);
57 auto xs = space->split(x);
58 ConstVectorRef u1 = u.head(c1_->nu);
59 ConstVectorRef u2 = u.tail(c2_->nu);
60
61 c1_->evaluate(xs[0], u1, *d.data1_);
62 c2_->evaluate(xs[1], u2, *d.data2_);
63
64 d.value_ = d.data1_->value_ + d.data2_->value_;
65}
66
67template <typename Scalar>
68void DirectSumCostTpl<Scalar>::computeGradients(const ConstVectorRef &x,
69 const ConstVectorRef &u,
70 BaseData &data) const {
71 CartesianProduct const *space = get_product_space();
72 Data &d = data_cast(data);
73 auto xs = space->split(x);
74 ConstVectorRef u1 = u.head(c1_->nu);
75 ConstVectorRef u2 = u.tail(c2_->nu);
76
77 BaseData &d1 = *d.data1_;
78 BaseData &d2 = *d.data2_;
79
80 c1_->computeGradients(xs[0], u1, d1);
81 c2_->computeGradients(xs[1], u2, d2);
82
83 d.Lx_.head(c1_->ndx()) = d1.Lx_;
84 d.Lx_.tail(c2_->ndx()) = d2.Lx_;
85 d.Lu_.head(c1_->nu) = d1.Lu_;
86 d.Lu_.tail(c2_->nu) = d2.Lu_;
87}
88
89template <typename Scalar>
90void DirectSumCostTpl<Scalar>::computeHessians(const ConstVectorRef &x,
91 const ConstVectorRef &u,
92 BaseData &data) const {
93 CartesianProduct const *space = get_product_space();
94 Data &d = data_cast(data);
95 auto xs = space->split(x);
96 ConstVectorRef u1 = u.head(c1_->nu);
97 ConstVectorRef u2 = u.tail(c2_->nu);
98
99 BaseData &d1 = *d.data1_;
100 BaseData &d2 = *d.data2_;
101
102 c1_->computeHessians(xs[0], u1, d1);
103 c2_->computeHessians(xs[1], u2, d2);
104
105 d.Lxx_.topLeftCorner(c1_->ndx(), c1_->ndx()) = d1.Lxx_;
106 d.Lxx_.bottomRightCorner(c2_->ndx(), c2_->ndx()) = d2.Lxx_;
107
108 d.Luu_.topLeftCorner(c1_->nu, c1_->nu) = d1.Luu_;
109 d.Luu_.bottomRightCorner(c2_->nu, c2_->nu) = d2.Luu_;
110
111 d.Lxu_.topLeftCorner(c1_->ndx(), c1_->nu) = d1.Lxu_;
112 d.Lxu_.bottomRightCorner(c2_->ndx(), c2_->nu) = d2.Lxu_;
113
114 d.Lux_ = d.Lxu_.transpose();
115}
116
117template <typename Scalar>
119 return std::make_shared<Data>(*this);
120}
121
122template <typename Scalar>
123auto directSum(shared_ptr<CostAbstractTpl<Scalar>> const &c1,
124 shared_ptr<CostAbstractTpl<Scalar>> const &c2) {
125 return std::make_shared<DirectSumCostTpl<Scalar>>(c1, c2);
126}
127
128} // namespace aligator
129
130#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
131#include "./cost-direct-sum.txx"
132#endif
Main package namespace.
auto directSum(shared_ptr< CostAbstractTpl< Scalar > > const &c1, shared_ptr< CostAbstractTpl< Scalar > > const &c2)
Stage costs for control problems.
int nu
Control dimension.
shared_ptr< Manifold > space
State dimension.
Data(const DirectSumCostTpl &model)
void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const override
Compute the cost gradients .
shared_ptr< 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 .
shared_ptr< BaseCost > c2_
DirectSumCostTpl(shared_ptr< BaseCost > c1, shared_ptr< BaseCost > c2)
ManifoldAbstractTpl< Scalar > Manifold