aligator
0.19.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Toggle main menu visibility
Loading...
Searching...
No Matches
cost-direct-sum.hpp
Go to the documentation of this file.
1
3
#pragma once
4
5
#include "
aligator/core/cost-abstract.hpp
"
6
#include "
aligator/modelling/spaces/cartesian-product.hpp
"
7
8
namespace
aligator
{
9
10
template
<
typename
_Scalar>
struct
DirectSumCostTpl
:
CostAbstractTpl
<_Scalar> {
11
using
Scalar
= _Scalar;
12
ALIGATOR_DYNAMIC_TYPEDEFS
(
Scalar
);
13
using
BaseCost
=
CostAbstractTpl<Scalar>
;
14
using
BaseData
=
CostDataAbstractTpl<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
)
22
,
c1_
(c1)
23
,
c2_
(c2) {
24
assert(!c1.valueless_after_move() && !c2.valueless_after_move());
25
}
26
27
xyz::polymorphic<BaseCost>
c1_
,
c2_
;
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
38
private
:
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
46
template
<
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
55
template
<
typename
Scalar>
56
void
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
71
template
<
typename
Scalar>
72
void
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
93
template
<
typename
Scalar>
94
void
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
121
template
<
typename
Scalar>
122
auto
DirectSumCostTpl<Scalar>::createData
() const -> shared_ptr<
BaseData
> {
123
return
std::make_shared<Data>(*
this
);
124
}
125
126
template
<
typename
Scalar>
127
auto
directSum
(xyz::polymorphic<
CostAbstractTpl<Scalar>
>
const
&c1,
128
xyz::polymorphic<
CostAbstractTpl<Scalar>
>
const
&c2) {
129
return
DirectSumCostTpl<Scalar>
(c1, c2);
130
}
131
132
}
// namespace aligator
133
134
#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
135
#include "./cost-direct-sum.txx"
136
#endif
cartesian-product.hpp
cost-abstract.hpp
aligator
Main package namespace.
Definition
action-model-wrap.hpp:14
aligator::directSum
auto directSum(xyz::polymorphic< CostAbstractTpl< Scalar > > const &c1, xyz::polymorphic< CostAbstractTpl< Scalar > > const &c2)
Definition
cost-direct-sum.hpp:127
aligator::CartesianProductTpl
The cartesian product of two or more manifolds.
Definition
cartesian-product.hpp:13
aligator::CostAbstractTpl
Stage costs for control problems.
Definition
cost-abstract.hpp:13
aligator::CostAbstractTpl< Scalar >::space
xyz::polymorphic< Manifold > space
Definition
cost-abstract.hpp:20
aligator::CostAbstractTpl::CostAbstractTpl
CostAbstractTpl(U &&space, const int nu)
Definition
cost-abstract.hpp:28
aligator::CostAbstractTpl< Scalar >::nu
int nu
Definition
cost-abstract.hpp:22
aligator::CostAbstractTpl::ndx
int ndx() const
Definition
cost-abstract.hpp:25
aligator::CostDataAbstractTpl
Data struct for CostAbstractTpl.
Definition
cost-abstract.hpp:73
aligator::CostDataAbstractTpl::Lx_
VectorRef Lx_
Gradient .
Definition
cost-abstract.hpp:82
aligator::CostDataAbstractTpl::Luu_
MatrixRef Luu_
Hessian .
Definition
cost-abstract.hpp:92
aligator::CostDataAbstractTpl::Lxx_
MatrixRef Lxx_
Hessian .
Definition
cost-abstract.hpp:86
aligator::CostDataAbstractTpl::Lxu_
MatrixRef Lxu_
Hessian .
Definition
cost-abstract.hpp:88
aligator::CostDataAbstractTpl::Lu_
VectorRef Lu_
Gradient .
Definition
cost-abstract.hpp:84
aligator::DirectSumCostTpl::Data
Definition
cost-direct-sum.hpp:46
aligator::DirectSumCostTpl
Definition
cost-direct-sum.hpp:10
aligator::DirectSumCostTpl::computeGradients
void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const override
Compute the cost gradients .
Definition
cost-direct-sum.hpp:72
aligator::DirectSumCostTpl::PolyCost
xyz::polymorphic< BaseCost > PolyCost
Definition
cost-direct-sum.hpp:16
aligator::DirectSumCostTpl< Scalar >::c1_
xyz::polymorphic< BaseCost > c1_
Definition
cost-direct-sum.hpp:27
aligator::DirectSumCostTpl::createData
shared_ptr< BaseData > createData() const override
Definition
cost-direct-sum.hpp:122
aligator::DirectSumCostTpl::evaluate
void evaluate(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const override
Evaluate the cost function.
Definition
cost-direct-sum.hpp:56
aligator::DirectSumCostTpl::computeHessians
void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const override
Compute the cost Hessians .
Definition
cost-direct-sum.hpp:94
aligator::DirectSumCostTpl::ALIGATOR_DYNAMIC_TYPEDEFS
ALIGATOR_DYNAMIC_TYPEDEFS(Scalar)
aligator::DirectSumCostTpl::DirectSumCostTpl
DirectSumCostTpl(const PolyCost &c1, const PolyCost &c2)
Definition
cost-direct-sum.hpp:20
aligator::DirectSumCostTpl::BaseCost
CostAbstractTpl< Scalar > BaseCost
Definition
cost-direct-sum.hpp:13
aligator::DirectSumCostTpl::Scalar
_Scalar Scalar
Definition
cost-direct-sum.hpp:11
aligator::DirectSumCostTpl< Scalar >::c2_
xyz::polymorphic< BaseCost > c2_
Definition
cost-direct-sum.hpp:27
aligator::DirectSumCostTpl::Manifold
ManifoldAbstractTpl< Scalar > Manifold
Definition
cost-direct-sum.hpp:15
aligator::DirectSumCostTpl::BaseData
CostDataAbstractTpl< Scalar > BaseData
Definition
cost-direct-sum.hpp:14
aligator::ManifoldAbstractTpl
Base class for manifolds, to use in cost funcs, solvers...
Definition
manifold-base.hpp:11
include
aligator
modelling
costs
cost-direct-sum.hpp
Generated by
1.17.0