autogenu-jupyter
An automatic code generator and the continuation/GMRES (C/GMRES) based numerical solvers for nonlinear MPC
Loading...
Searching...
No Matches
multiple_shooting_nlp.hpp
Go to the documentation of this file.
1#ifndef CGMRES__MULTIPLE_SHOOTING_NLP_HPP_
2#define CGMRES__MULTIPLE_SHOOTING_NLP_HPP_
3
4#include <array>
5
6#include "cgmres/types.hpp"
7#include "cgmres/horizon.hpp"
8
11
12namespace cgmres {
13namespace detail {
14
15template <class OCP, int N>
17public:
18 static constexpr int nx = OCP::nx;
19 static constexpr int nu = OCP::nu;
20 static constexpr int nc = OCP::nc;
21 static constexpr int nuc = nu + nc;
22 static constexpr int nub = OCP::nub;
23 static constexpr int dim = nuc * N;
24
26 : ocp_(ocp),
27 horizon_(horizon),
28 dx_(Vector<nx>::Zero()) {
29 static_assert(OCP::nx > 0);
30 static_assert(OCP::nu > 0);
31 static_assert(OCP::nc >= 0);
32 static_assert(OCP::nub >= 0);
33 static_assert(N > 0);
34 }
35
37
39
40 template <typename VectorType>
42 const std::array<Vector<nx>, N+1>& x, const std::array<Vector<nx>, N+1>& lmd,
44 assert(x0.size());
45 const Scalar T = horizon_.T(t);
46 const Scalar dt = T / N;
47 assert(T >= 0);
48 // Compute the erros in the first order necessary conditions (FONC)
49 ocp_.eval_hu(t, x0.derived().data(), solution.template head<nuc>().data(), lmd[1].data(),
50 fonc_hu.template head<nuc>().data());
51 for (size_t i=1; i<N; ++i) {
52 ocp_.eval_hu(t+i*dt, x[i].data(), solution.template segment<nuc>(nuc*i).data(),
53 lmd[i+1].data(), fonc_hu.template segment<nuc>(nuc*i).data());
54 }
55 }
56
57 template <typename VectorType>
59 const std::array<Vector<nx>, N+1>& x,
60 std::array<Vector<nx>, N+1>& fonc_f) {
61 const Scalar T = horizon_.T(t);
62 const Scalar dt = T / N;
63 assert(T >= 0);
64 // Compute optimality error for state.
65 ocp_.eval_f(t, x0.derived().data(), solution.template head<nuc>().data(), dx_.data());
66 fonc_f[0] = x[1] - x0 - dt * dx_;
67 for (size_t i=1; i<N; ++i) {
68 ocp_.eval_f(t+i*dt, x[i].data(), solution.template segment<nuc>(nuc*i).data(), dx_.data());
69 fonc_f[i] = x[i+1] - x[i] - dt * dx_;
70 }
71 }
72
73 template <typename VectorType>
75 std::array<Vector<nx>, N+1>& x,
76 const std::array<Vector<nx>, N+1>& fonc_f) {
77 const Scalar T = horizon_.T(t);
78 const Scalar dt = T / N;
79 assert(T >= 0);
80 // Compute optimality error for state.
81 ocp_.eval_f(t, x0.derived().data(), solution.template head<nuc>().data(), dx_.data());
82 x[1] = x0 + dt * dx_ + fonc_f[0];
83 for (size_t i=1; i<N; ++i) {
84 ocp_.eval_f(t+i*dt, x[i].data(), solution.template segment<nuc>(nuc*i).data(), dx_.data());
85 x[i+1] = x[i] + dt * dx_ + fonc_f[i];
86 }
87 }
88
89 template <typename VectorType>
91 const std::array<Vector<nx>, N+1>& x, const std::array<Vector<nx>, N+1>& lmd,
92 std::array<Vector<nx>, N+1>& fonc_hx) {
93 const Scalar T = horizon_.T(t);
94 const Scalar dt = T / N;
95 assert(T >= 0);
96 // Compute optimality error for lambda.
97 ocp_.eval_phix(t+T, x[N].data(), dx_.data());
98 fonc_hx[N] = lmd[N] - dx_;
99 for (size_t i=N-1; i>=1; --i) {
100 ocp_.eval_hx(t+i*dt, x[i].data(), solution.template segment<nuc>(nuc*i).data(),
101 lmd[i+1].data(), dx_.data());
102 fonc_hx[i] = lmd[i] - lmd[i+1] - dt * dx_;
103 }
104 }
105
106 template <typename VectorType>
108 const std::array<Vector<nx>, N+1>& x, std::array<Vector<nx>, N+1>& lmd,
109 const std::array<Vector<nx>, N+1>& fonc_hx) {
110 const Scalar T = horizon_.T(t);
111 const Scalar dt = T / N;
112 assert(T >= 0);
113 // Compute optimality error for state.
114 ocp_.eval_phix(t+T, x[N].data(), dx_.data());
115 lmd[N] = dx_ + fonc_hx[N];
116 for (size_t i=N-1; i>=1; --i) {
117 ocp_.eval_hx(t+i*dt, x[i].data(), solution.template segment<nuc>(nuc*i).data(),
118 lmd[i+1].data(), dx_.data());
119 lmd[i] = lmd[i+1] + dt * dx_ + fonc_hx[i];
120 }
121 }
122
124 const std::array<Vector<nub>, N>& dummy,
125 const std::array<Vector<nub>, N>& mu,
126 Vector<dim>& fonc_hu) const {
127 ubounds::eval_fonc_hu<OCP, N>(ocp_, solution, dummy, mu, fonc_hu);
128 }
129
131 const std::array<Vector<nub>, N>& dummy,
132 const std::array<Vector<nub>, N>& mu,
133 std::array<Vector<nub>, N>& fonc_hdummy) const {
134 ubounds::eval_fonc_hdummy<OCP, N>(ocp_, solution, dummy, mu, fonc_hdummy);
135 }
136
138 const std::array<Vector<nub>, N>& dummy,
139 const std::array<Vector<nub>, N>& mu,
140 std::array<Vector<nub>, N>& fonc_hmu) const {
141 ubounds::eval_fonc_hmu<OCP, N>(ocp_, solution, dummy, mu, fonc_hmu);
142 }
143
144 static void multiply_hdummy_inv(const std::array<Vector<nub>, N>& dummy,
145 const std::array<Vector<nub>, N>& mu,
146 const std::array<Vector<nub>, N>& fonc_hdummy,
147 const std::array<Vector<nub>, N>& fonc_hmu,
148 std::array<Vector<nub>, N>& fonc_hdummy_inv) {
149 ubounds::multiply_hdummy_inv<OCP, N>(dummy, mu, fonc_hdummy, fonc_hmu,
151 }
152
153 static void multiply_hmu_inv(const std::array<Vector<nub>, N>& dummy,
154 const std::array<Vector<nub>, N>& mu,
155 const std::array<Vector<nub>, N>& fonc_hdummy,
156 const std::array<Vector<nub>, N>& fonc_hmu,
157 const std::array<Vector<nub>, N>& fonc_hdummy_inv,
158 std::array<Vector<nub>, N>& fonc_hmu_inv) {
159 ubounds::multiply_hmu_inv<OCP, N>(dummy, mu, fonc_hdummy, fonc_hmu,
161 }
162
164 const std::array<Vector<OCP::nub>, N>& dummy,
165 const std::array<Vector<OCP::nub>, N>& mu,
167 std::array<Vector<OCP::nub>, N>& dummy_update) {
168 ubounds::retrieve_dummy_update<OCP, N>(ocp_, solution, dummy, mu, solution_update, dummy_update);
169 }
170
172 const std::array<Vector<OCP::nub>, N>& dummy,
173 const std::array<Vector<OCP::nub>, N>& mu,
175 std::array<Vector<OCP::nub>, N>& mu_update) {
176 ubounds::retrieve_mu_update<OCP, N>(ocp_, solution, dummy, mu, solution_update, mu_update);
177 }
178
179 void clip_dummy(std::array<Vector<OCP::nub>, N>& dummy, const Scalar min) {
180 ubounds::clip_dummy<OCP, N>(dummy, min);
181 }
182
183 void synchronize_ocp() { ocp_.synchronize(); }
184
185 const OCP& ocp() const { return ocp_; }
186
187 const Horizon& horizon() const { return horizon_; }
188
190
191private:
192 OCP ocp_;
193 Horizon horizon_;
194 Vector<nx> dx_;
195};
196
197} // namespace detail
198} // namespace cgmres
199
200#endif // CGMRES__MULTIPLE_SHOOTING_NLP_HPP_
Horizon of MPC.
Definition horizon.hpp:18
Scalar T(const Scalar t) const
Gets the length of the horizon.
Definition horizon.hpp:50
Definition multiple_shooting_nlp.hpp:16
MultipleShootingNLP(const OCP &ocp, const Horizon &horizon)
Definition multiple_shooting_nlp.hpp:25
void eval_fonc_f(const Scalar t, const MatrixBase< VectorType > &x0, const Vector< dim > &solution, const std::array< Vector< nx >, N+1 > &x, std::array< Vector< nx >, N+1 > &fonc_f)
Definition multiple_shooting_nlp.hpp:58
static constexpr int nc
Definition multiple_shooting_nlp.hpp:20
void clip_dummy(std::array< Vector< OCP::nub >, N > &dummy, const Scalar min)
Definition multiple_shooting_nlp.hpp:179
void eval_fonc_hdummy(const Vector< dim > &solution, const std::array< Vector< nub >, N > &dummy, const std::array< Vector< nub >, N > &mu, std::array< Vector< nub >, N > &fonc_hdummy) const
Definition multiple_shooting_nlp.hpp:130
static constexpr int dim
Definition multiple_shooting_nlp.hpp:23
void eval_fonc_hu(const Scalar t, const MatrixBase< VectorType > &x0, const Vector< dim > &solution, const std::array< Vector< nx >, N+1 > &x, const std::array< Vector< nx >, N+1 > &lmd, Vector< dim > &fonc_hu)
Definition multiple_shooting_nlp.hpp:41
static void multiply_hdummy_inv(const std::array< Vector< nub >, N > &dummy, const std::array< Vector< nub >, N > &mu, const std::array< Vector< nub >, N > &fonc_hdummy, const std::array< Vector< nub >, N > &fonc_hmu, std::array< Vector< nub >, N > &fonc_hdummy_inv)
Definition multiple_shooting_nlp.hpp:144
void retrieve_x(const Scalar t, const MatrixBase< VectorType > &x0, const Vector< dim > &solution, std::array< Vector< nx >, N+1 > &x, const std::array< Vector< nx >, N+1 > &fonc_f)
Definition multiple_shooting_nlp.hpp:74
static constexpr int nub
Definition multiple_shooting_nlp.hpp:22
static constexpr int nuc
Definition multiple_shooting_nlp.hpp:21
void eval_fonc_hx(const Scalar t, const MatrixBase< VectorType > &x0, const Vector< dim > &solution, const std::array< Vector< nx >, N+1 > &x, const std::array< Vector< nx >, N+1 > &lmd, std::array< Vector< nx >, N+1 > &fonc_hx)
Definition multiple_shooting_nlp.hpp:90
void eval_fonc_hmu(const Vector< dim > &solution, const std::array< Vector< nub >, N > &dummy, const std::array< Vector< nub >, N > &mu, std::array< Vector< nub >, N > &fonc_hmu) const
Definition multiple_shooting_nlp.hpp:137
static constexpr int nx
Definition multiple_shooting_nlp.hpp:18
static constexpr int nu
Definition multiple_shooting_nlp.hpp:19
void synchronize_ocp()
Definition multiple_shooting_nlp.hpp:183
const OCP & ocp() const
Definition multiple_shooting_nlp.hpp:185
void retrieve_dummy_update(const Vector< OCP::nuc *N > &solution, const std::array< Vector< OCP::nub >, N > &dummy, const std::array< Vector< OCP::nub >, N > &mu, const Vector< OCP::nuc *N > &solution_update, std::array< Vector< OCP::nub >, N > &dummy_update)
Definition multiple_shooting_nlp.hpp:163
void retrieve_lmd(const Scalar t, const MatrixBase< VectorType > &x0, const Vector< dim > &solution, const std::array< Vector< nx >, N+1 > &x, std::array< Vector< nx >, N+1 > &lmd, const std::array< Vector< nx >, N+1 > &fonc_hx)
Definition multiple_shooting_nlp.hpp:107
static void multiply_hmu_inv(const std::array< Vector< nub >, N > &dummy, const std::array< Vector< nub >, N > &mu, const std::array< Vector< nub >, N > &fonc_hdummy, const std::array< Vector< nub >, N > &fonc_hmu, const std::array< Vector< nub >, N > &fonc_hdummy_inv, std::array< Vector< nub >, N > &fonc_hmu_inv)
Definition multiple_shooting_nlp.hpp:153
const Horizon & horizon() const
Definition multiple_shooting_nlp.hpp:187
void retrieve_mu_update(const Vector< OCP::nuc *N > &solution, const std::array< Vector< OCP::nub >, N > &dummy, const std::array< Vector< OCP::nub >, N > &mu, const Vector< OCP::nuc *N > &solution_update, std::array< Vector< OCP::nub >, N > &mu_update)
Definition multiple_shooting_nlp.hpp:171
void eval_fonc_hu(const Vector< dim > &solution, const std::array< Vector< nub >, N > &dummy, const std::array< Vector< nub >, N > &mu, Vector< dim > &fonc_hu) const
Definition multiple_shooting_nlp.hpp:123
Definition continuation_gmres.hpp:11
Eigen::Map< MatrixType > Map
Alias of Eigen::Map.
Definition types.hpp:54
Eigen::Matrix< Scalar, size, 1 > Vector
Alias of Eigen::Vector.
Definition types.hpp:27
double Scalar
Alias of double.
Definition types.hpp:15