alpaqa
1.0.0a13
Nonconvex constrained optimization
Loading...
Searching...
No Matches
src
alpaqa
include
alpaqa
inner
directions
panoc-ocp
lqr.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <
alpaqa/config/config.hpp
>
4
#include <Eigen/Cholesky>
5
#include <Eigen/LU>
6
#include <cassert>
7
8
namespace
alpaqa
{
9
10
template
<Config Conf>
11
struct
Dim
{
12
USING_ALPAQA_CONFIG
(
Conf
);
13
length_t
N
,
nx
,
nu
;
14
struct
Horizon
{
15
length_t
N
;
16
struct
Iter
{
17
index_t
i
;
18
Iter
&
operator++
() {
19
++
i
;
20
return
*
this
;
21
}
22
Iter
operator++
(
int
)
const
{
23
Iter
t = *
this
;
24
++
i
;
25
return
t;
26
}
27
index_t
&
operator*
() {
return
i
; }
28
const
index_t
&
operator*
()
const
{
return
i
; }
29
friend
auto
operator<=>
(
const
Iter
&,
const
Iter
&) =
default
;
30
};
31
static
Iter
begin
() {
return
{0}; }
32
Iter
end
()
const
{
return
{
N
}; }
33
};
34
Horizon
horizon
()
const
{
return
{
N
}; }
35
};
36
37
template
<Config Conf>
38
struct
StatefulLQRFactor
{
39
USING_ALPAQA_CONFIG
(
Conf
);
40
41
using
Dim
=
alpaqa::Dim<config_t>
;
42
43
StatefulLQRFactor
(
Dim
d) :
dim
{d} {}
44
Dim
dim
;
45
mat
P
{
dim
.
nx
,
dim
.
nx
};
46
mat
gain_K
{
dim
.
nu
*
dim
.
nx
,
dim
.
N
};
47
mat
e
{
dim
.
nu
,
dim
.
N
};
48
vec
s
{
dim
.
nx
};
49
vec
c
{
dim
.
nx
};
50
vec
y
{
dim
.
nx
};
51
vec
t
{
dim
.
nu
};
52
vec
R̅_sto
{
dim
.
nu
*
dim
.
nu
};
53
vec
S̅_sto
{
dim
.
nu
*
dim
.
nx
};
54
vec
BiJ_sto
{
dim
.
nx
*
dim
.
nu
};
55
vec
PBiJ_sto
{
dim
.
nx
*
dim
.
nu
};
56
mat
PA
{
dim
.
nx
,
dim
.
nx
};
57
real_t
min_rcond
= 1;
58
59
void
factor_masked
(
auto
&&AB,
///< System matrix A & input matrix B
60
auto
&&Q,
///< State cost matrix Q
61
auto
&&R,
///< Input cost matrix R
62
auto
&&S,
///< Cross cost matrix S
63
auto
&&R_prod,
///< Product with input cost matrix R
64
auto
&&S_prod,
///< Product with cross cost matrix S
65
auto
&&q,
///< Linear state factor q
66
auto
&&r,
///< Linear input factor r
67
auto
&&u,
///< Fixed inputs u
68
auto
&&
J
,
///< Index set of inactive constraints
69
auto
&&
K
,
///< Index set of active constraints
70
bool
use_cholesky
///< Use Cholesky instead of LU solver
71
) {
72
using
mmat
= Eigen::Map<mat>;
73
using
Eigen::indexing::all;
74
auto
[N, nx, nu] =
dim
;
75
76
min_rcond
= 1;
77
P
.setZero();
78
Q(N)(
P
);
79
s
= q(N);
80
for
(
index_t
i = N; i-- > 0;) {
81
auto
&&
ABi
= AB(i);
82
auto
&&
Ai
=
ABi
.leftCols(nx);
83
auto
&&
Bi
=
ABi
.rightCols(nu);
84
auto
&&
ui
= u(i);
85
auto
&&
Ji
=
J
(i);
86
auto
&&
Ki
=
K
(i);
87
length_t
nJ =
Ji
.size();
// number of inactive constraints
88
mmat
R̅
{
R̅_sto
.data(), nJ, nJ};
89
mmat
S̅
{
S̅_sto
.data(), nJ, nx};
90
mmat
BiJ
{
BiJ_sto
.data(), nx, nJ};
91
mmat
PBiJ
{
PBiJ_sto
.data(), nx, nJ};
92
auto
&&
ti
=
t
.topRows(nJ);
93
mmat
gain_Ki
{
gain_K
.col(i).data(), nJ, nx};
94
auto
&&
ei
=
e
.col(i).topRows(nJ);
95
// R̅ ← R + Bᵀ P B
96
BiJ
.noalias() =
Bi
(
all
,
Ji
);
97
PBiJ
.noalias() =
P
*
BiJ
;
98
R̅
.noalias() =
BiJ
.transpose() *
PBiJ
;
99
R(i)(
Ji
,
R̅
);
100
// S̅ ← S + Bᵀ P A
101
PA
.noalias() =
P
*
Ai
;
102
S̅
.noalias() =
BiJ
.transpose() *
PA
;
103
S(i)(
Ji
,
S̅
);
104
// c = B(·,K) u(K), y ← P c + s
105
c
.noalias() =
Bi
(
all
,
Ki
) *
ui
(
Ki
);
106
y
.noalias() =
P
*
c
;
107
y
+=
s
;
108
// t ← Bᵀy + r + R(J,K) u(K)
109
ti
.noalias() =
BiJ
.transpose() *
y
;
110
ti
+= r(i)(
Ji
);
111
R_prod(i)(
Ji
,
Ki
,
ui
,
ti
);
112
// Factor R̅
113
if
(
use_cholesky
) {
114
#ifdef EIGEN_RUNTIME_NO_MALLOC
115
bool
prev
= Eigen::internal::is_malloc_allowed();
116
Eigen::internal::set_is_malloc_allowed(
true
);
// TODO
117
#endif
118
Eigen::LDLT<rmat>
R̅LU
{
R̅
};
119
min_rcond
= std::min(
R̅LU
.rcond(),
min_rcond
);
120
#ifdef EIGEN_RUNTIME_NO_MALLOC
121
Eigen::internal::set_is_malloc_allowed(
prev
);
122
#endif
123
// K ← -R̅⁻¹S̅
124
gain_Ki
.noalias() =
R̅LU
.solve(
S̅
);
125
// e ← -R̅⁻¹(Bᵀy + r)
126
ei
.noalias() =
R̅LU
.solve(
ti
);
127
}
else
{
128
#ifdef EIGEN_RUNTIME_NO_MALLOC
129
bool
prev
= Eigen::internal::is_malloc_allowed();
130
Eigen::internal::set_is_malloc_allowed(
true
);
// TODO
131
#endif
132
Eigen::PartialPivLU<rmat>
R̅LU
{
R̅
};
133
min_rcond
= std::min(
R̅LU
.rcond(),
min_rcond
);
134
#ifdef EIGEN_RUNTIME_NO_MALLOC
135
Eigen::internal::set_is_malloc_allowed(
prev
);
136
#endif
137
// K ← -R̅⁻¹S̅
138
gain_Ki
.noalias() =
R̅LU
.solve(
S̅
);
139
// e ← -R̅⁻¹(Bᵀy + r)
140
ei
.noalias() =
R̅LU
.solve(
ti
);
141
}
142
gain_Ki
= -
gain_Ki
;
143
ei
= -
ei
;
144
if
(i > 0) {
145
// P ← Q + Aᵀ P A + S̅ᵀ K
146
P
.noalias() =
Ai
.transpose() *
PA
;
147
P
.noalias() +=
S̅
.transpose() *
gain_Ki
;
148
// s ← S̅ᵀ e + Aᵀ y + q + Sᵀ(·,K) u(K)
149
s
.noalias() =
S̅
.transpose() *
ei
;
150
s
.noalias() +=
Ai
.transpose() *
y
;
151
s
+= q(i);
152
S_prod(i)(
Ki
,
ui
,
s
);
153
Q(i)(
P
);
154
}
155
}
156
}
157
158
void
solve_masked
(
auto
&&AB,
auto
&&
J
,
rvec
Δu_eq
,
rvec
Δx
) {
159
auto
[N, nx, nu] =
dim
;
160
assert
(
Δx
.size() == 2 * nx);
161
Δx
.topRows(nx).setZero();
162
for
(
index_t
i = 0; i < N; ++i) {
163
auto
&&
ABi
= AB(i);
164
auto
&&
Ai
=
ABi
.leftCols(nx);
165
auto
&&
Bi
=
ABi
.rightCols(nu);
166
auto
&&
Ji
=
J
(i);
167
auto
&&
Δxi
=
Δx
.segment((i % 2) * nx, nx);
168
auto
&&
Δx_next
=
Δx
.segment(((i + 1) % 2) * nx, nx);
169
length_t
nJ =
Ji
.size();
170
mmat
Ki
{
gain_K
.col(i).data(), nJ, nx};
171
auto
&&
ei
=
e
.col(i).topRows(nJ);
172
auto
&&
Δui
=
Δu_eq
.segment(i * nu, nu);
173
ei
.noalias() +=
Ki
*
Δxi
;
174
Δui
(
Ji
).noalias() =
ei
;
175
Δx_next
.noalias() =
Ai
*
Δxi
;
176
Δx_next
.noalias() +=
Bi
*
Δui
;
177
}
178
}
179
};
180
181
}
// namespace alpaqa
config.hpp
USING_ALPAQA_CONFIG
#define USING_ALPAQA_CONFIG(Conf)
Definition
config.hpp:56
alpaqa
Definition
anderson.hpp:10
alpaqa::mat
typename Conf::mat mat
Definition
config.hpp:71
alpaqa::real_t
typename Conf::real_t real_t
Definition
config.hpp:65
alpaqa::index_t
typename Conf::index_t index_t
Definition
config.hpp:77
alpaqa::mmat
typename Conf::mmat mmat
Definition
config.hpp:72
alpaqa::length_t
typename Conf::length_t length_t
Definition
config.hpp:76
alpaqa::inf
constexpr const auto inf
Definition
config.hpp:85
alpaqa::rvec
typename Conf::rvec rvec
Definition
config.hpp:69
alpaqa::vec
typename Conf::vec vec
Definition
config.hpp:66
alpaqa::Dim::Horizon::Iter
Definition
lqr.hpp:16
alpaqa::Dim::Horizon::Iter::operator*
const index_t & operator*() const
Definition
lqr.hpp:28
alpaqa::Dim::Horizon::Iter::operator++
Iter & operator++()
Definition
lqr.hpp:18
alpaqa::Dim::Horizon::Iter::operator*
index_t & operator*()
Definition
lqr.hpp:27
alpaqa::Dim::Horizon::Iter::operator<=>
friend auto operator<=>(const Iter &, const Iter &)=default
alpaqa::Dim::Horizon::Iter::i
index_t i
Definition
lqr.hpp:17
alpaqa::Dim::Horizon::Iter::operator++
Iter operator++(int) const
Definition
lqr.hpp:22
alpaqa::Dim::Horizon
Definition
lqr.hpp:14
alpaqa::Dim::Horizon::N
length_t N
Definition
lqr.hpp:15
alpaqa::Dim::Horizon::begin
static Iter begin()
Definition
lqr.hpp:31
alpaqa::Dim::Horizon::end
Iter end() const
Definition
lqr.hpp:32
alpaqa::Dim
Definition
lqr.hpp:11
alpaqa::Dim::nx
length_t nx
Definition
lqr.hpp:13
alpaqa::Dim::N
length_t N
Definition
lqr.hpp:13
alpaqa::Dim::nu
length_t nu
Definition
lqr.hpp:13
alpaqa::Dim::horizon
Horizon horizon() const
Definition
lqr.hpp:34
alpaqa::StatefulLQRFactor
Definition
lqr.hpp:38
alpaqa::StatefulLQRFactor::factor_masked
void factor_masked(auto &&AB, auto &&Q, auto &&R, auto &&S, auto &&R_prod, auto &&S_prod, auto &&q, auto &&r, auto &&u, auto &&J, auto &&K, bool use_cholesky)
Definition
lqr.hpp:59
alpaqa::StatefulLQRFactor::BiJ_sto
vec BiJ_sto
Definition
lqr.hpp:54
alpaqa::StatefulLQRFactor::S̅_sto
vec S̅_sto
Definition
lqr.hpp:53
alpaqa::StatefulLQRFactor::y
vec y
Definition
lqr.hpp:50
alpaqa::StatefulLQRFactor::StatefulLQRFactor
StatefulLQRFactor(Dim d)
Definition
lqr.hpp:43
alpaqa::StatefulLQRFactor::s
vec s
Definition
lqr.hpp:48
alpaqa::StatefulLQRFactor::PA
mat PA
Definition
lqr.hpp:56
alpaqa::StatefulLQRFactor::solve_masked
void solve_masked(auto &&AB, auto &&J, rvec Δu_eq, rvec Δx)
Definition
lqr.hpp:158
alpaqa::StatefulLQRFactor::dim
Dim dim
Definition
lqr.hpp:44
alpaqa::StatefulLQRFactor::min_rcond
real_t min_rcond
Definition
lqr.hpp:57
alpaqa::StatefulLQRFactor::P
mat P
Definition
lqr.hpp:45
alpaqa::StatefulLQRFactor::R̅_sto
vec R̅_sto
Definition
lqr.hpp:52
alpaqa::StatefulLQRFactor::c
vec c
Definition
lqr.hpp:49
alpaqa::StatefulLQRFactor::e
mat e
Definition
lqr.hpp:47
alpaqa::StatefulLQRFactor::PBiJ_sto
vec PBiJ_sto
Definition
lqr.hpp:55
alpaqa::StatefulLQRFactor::gain_K
mat gain_K
Definition
lqr.hpp:46
alpaqa::StatefulLQRFactor::t
vec t
Definition
lqr.hpp:51
Generated on Mon Oct 30 2023 for alpaqa by
1.9.8