autogenu-jupyter
An automatic code generator and the continuation/GMRES (C/GMRES) based numerical solvers for nonlinear MPC
Loading...
Searching...
No Matches
zero_horizon_nlp.hpp
Go to the documentation of this file.
1#ifndef CGMRES__ZERO_HORIZON_NLP_HPP_
2#define CGMRES__ZERO_HORIZON_NLP_HPP_
3
4#include <array>
5
6#include "cgmres/types.hpp"
7#include "cgmres/horizon.hpp"
8
10
11namespace cgmres {
12namespace detail {
13
14template <class OCP>
16public:
17 static constexpr int nx = OCP::nx;
18 static constexpr int nu = OCP::nu;
19 static constexpr int nc = OCP::nc;
20 static constexpr int nuc = nu + nc;
21 static constexpr int nub = OCP::nub;
22 static constexpr int dim = nuc + 2 * nub;
23
24 ZeroHorizonNLP(const OCP& ocp)
25 : ocp_(ocp),
26 lmd_(Vector<nx>::Zero()) {
27 static_assert(OCP::nx > 0);
28 static_assert(OCP::nu > 0);
29 static_assert(OCP::nc >= 0);
30 static_assert(OCP::nub >= 0);
31 }
32
33 ZeroHorizonNLP() = default;
34
35 ~ZeroHorizonNLP() = default;
36
37 template <typename VectorType>
38 void eval_fonc_hu(const Scalar t, const MatrixBase<VectorType>& x, const Vector<dim>& solution,
39 Vector<dim>& fonc_hu) {
40 // Compute the Lagrange multiplier over the horizon
41 ocp_.eval_phix(t, x.derived().data(), lmd_.data());
42 // Compute the erros in the first order necessary conditions (FONC)
43 ocp_.eval_hu(t, x.derived().data(), solution.data(), lmd_.data(), fonc_hu.data());
44 if constexpr (nub > 0) {
45 const auto uc = solution.template head<nuc>();
46 const auto dummy = solution.template segment<nub>(nuc);
47 const auto mu = solution.template segment<nub>(nuc+nub);
48 auto fonc_huc = fonc_hu.template head<nuc>();
49 auto fonc_hdummy = fonc_hu.template segment<nub>(nuc);
50 auto fonc_hmu = fonc_hu.template segment<nub>(nuc+nub);
51 ubounds::eval_hu(ocp_, uc, dummy, mu, fonc_huc);
52 ubounds::eval_hdummy(ocp_, uc, dummy, mu, fonc_hdummy);
53 ubounds::eval_hmu(ocp_, uc, dummy, mu, fonc_hmu);
54 }
55 }
56
57 void retrive_dummy(Vector<dim>& solution, Vector<dim>& fonc_hu, const Scalar min_dummy) {
58 if constexpr (nub > 0) {
59 const auto uc = solution.template head<nuc>();
60 auto dummy = solution.template segment<nub>(nuc);
61 const auto mu = solution.template segment<nub>(nuc+nub);
62 auto fonc_hmu = fonc_hu.template segment<nub>(nuc+nub);
63 dummy.setZero();
64 ubounds::eval_hmu(ocp_, uc, dummy, mu, fonc_hmu);
65 ubounds::clip_dummy(dummy, min_dummy);
66 dummy.array() = fonc_hmu.array().abs().sqrt();
67 }
68 }
69
70 void retrive_mu(Vector<dim>& solution, Vector<dim>& fonc_hu) {
71 if constexpr (nub > 0) {
72 const auto uc = solution.template head<nuc>();
73 const auto dummy = solution.template segment<nub>(nuc);
74 auto mu = solution.template segment<nub>(nuc+nub);
75 auto fonc_hdummy = fonc_hu.template segment<nub>(nuc);
76 mu.setZero();
77 ubounds::eval_hmu(ocp_, uc, dummy, mu, fonc_hdummy);
78 mu.array() = fonc_hdummy.array() / (2.0 * dummy.array());
79 }
80 }
81
82 void synchronize_ocp() { ocp_.synchronize(); }
83
84 const OCP& ocp() const { return ocp_; }
85
86 const Vector<nx>& lmd() const { return lmd_; }
87
88 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
89
90private:
91 OCP ocp_;
92 Vector<nx> lmd_;
93};
94
95} // namespace detail
96} // namespace cgmres
97
98#endif // CGMRES__ZERO_HORIZON_NLP_HPP_
Definition: zero_horizon_nlp.hpp:15
void retrive_dummy(Vector< dim > &solution, Vector< dim > &fonc_hu, const Scalar min_dummy)
Definition: zero_horizon_nlp.hpp:57
const Vector< nx > & lmd() const
Definition: zero_horizon_nlp.hpp:86
static constexpr int dim
Definition: zero_horizon_nlp.hpp:22
void retrive_mu(Vector< dim > &solution, Vector< dim > &fonc_hu)
Definition: zero_horizon_nlp.hpp:70
static constexpr int nx
Definition: zero_horizon_nlp.hpp:17
void synchronize_ocp()
Definition: zero_horizon_nlp.hpp:82
void eval_fonc_hu(const Scalar t, const MatrixBase< VectorType > &x, const Vector< dim > &solution, Vector< dim > &fonc_hu)
Definition: zero_horizon_nlp.hpp:38
static constexpr int nuc
Definition: zero_horizon_nlp.hpp:20
static constexpr int nu
Definition: zero_horizon_nlp.hpp:18
static constexpr int nc
Definition: zero_horizon_nlp.hpp:19
const OCP & ocp() const
Definition: zero_horizon_nlp.hpp:84
ZeroHorizonNLP(const OCP &ocp)
Definition: zero_horizon_nlp.hpp:24
static constexpr int nub
Definition: zero_horizon_nlp.hpp:21
void clip_dummy(const MatrixBase< VectorType > &dummy, const Scalar min)
Definition: control_input_bounds.hpp:127
void eval_hmu(const OCP &ocp, const MatrixBase< VectorType1 > &u, const MatrixBase< VectorType2 > &dummy, const MatrixBase< VectorType3 > &mu, const MatrixBase< VectorType4 > &hmu)
Definition: control_input_bounds.hpp:47
void eval_hdummy(const OCP &ocp, const MatrixBase< VectorType1 > &u, const MatrixBase< VectorType2 > &dummy, const MatrixBase< VectorType3 > &mu, const MatrixBase< VectorType4 > &hdummy)
Definition: control_input_bounds.hpp:32
void eval_hu(const OCP &ocp, const MatrixBase< VectorType1 > &u, const MatrixBase< VectorType2 > &dummy, const MatrixBase< VectorType3 > &mu, const MatrixBase< VectorType4 > &hu)
Definition: control_input_bounds.hpp:15
Definition: continuation_gmres.hpp:11
Eigen::Matrix< Scalar, size, 1 > Vector
Alias of Eigen::Vector.
Definition: types.hpp:23
double Scalar
Alias of double.
Definition: types.hpp:11
Eigen::MatrixBase< MatrixType > MatrixBase
Alias of Eigen::MatrixBase.
Definition: types.hpp:29