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