1#ifndef CGMRES__ZERO_HORIZON_OCP_SOLVER_HPP_
2#define CGMRES__ZERO_HORIZON_OCP_SOLVER_HPP_
24template <
class OCP,
int kmax>
30 static constexpr int nx = OCP::nx;
35 static constexpr int nu = OCP::nu;
40 static constexpr int nc = OCP::nc;
50 static constexpr int nub = OCP::nub;
92 template <
typename VectorType>
95 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::set_u] u.size() must be " + std::to_string(
nu));
107 template <
typename VectorType>
109 if (
uc.size() !=
nuc) {
110 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::set_uc] uc.size() must be " + std::to_string(
nuc));
121 template <
typename VectorType>
124 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::set_dummy] dummy.size() must be " + std::to_string(
nub));
134 template <
typename VectorType>
136 if (
mu.size() !=
nub) {
137 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::set_mu] mu.size() must be " + std::to_string(
nub));
194 template <
typename VectorType>
196 if (x.size() !=
nx) {
197 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::optError] x.size() must be " + std::to_string(
nx));
209 template <
typename VectorType>
211 if (x.size() !=
nx) {
212 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::update] x.size() must be " + std::to_string(
nx));
215 std::cout <<
"\n======================= solve zero horizon OCP =======================" << std::endl;
223 newton_gmres_,
t, x.derived(), solution_, solution_update_);
224 const auto opt_error = newton_gmres_.optError();
225 solution_.noalias() += solution_update_;
231 <<
" (opt tol: " << settings_.
opterr_tol <<
")" << std::endl;
234 std::cout <<
" number of GMRES iter: " <<
gmres_iter
235 <<
" (kmax: " << kmax <<
")" << std::endl;
241 std::cout <<
"converged!" << std::endl;
258 os <<
"Zero horizon OCP solver: " << std::endl;
259 os <<
" kmax: " << kmax << std::endl;
261 os << settings_ << std::endl;
284 void setInnerSolution() {
286 if constexpr (
nub > 0) {
292 void retrieveSolution() {
293 uopt_ = solution_.template
head<nu>();
A timer for benchmarks.
Definition timer.hpp:50
void tick()
Start timer (tick).
Definition timer.hpp:71
TimingProfile getProfile() const
Get timing result as TimingProfile.
Definition timer.hpp:90
void tock()
Stop the timer (tock).
Definition timer.hpp:78
Zero-horizon OCP solver to initialize the solution of the MPC solvers.
Definition zero_horizon_ocp_solver.hpp:25
static constexpr int nu
Dimension of the control input.
Definition zero_horizon_ocp_solver.hpp:35
Scalar optError(const Scalar t, const MatrixBase< VectorType > &x)
Computes and gets the l2-norm of the current optimality errors.
Definition zero_horizon_ocp_solver.hpp:195
~ZeroHorizonOCPSolver()=default
Default destructor.
void set_dummy(const MatrixBase< VectorType > &dummy)
Sets the dummy input vector with respect to the control input bounds constraint.
Definition zero_horizon_ocp_solver.hpp:122
TimingProfile getProfile() const
Get timing result as TimingProfile.
Definition zero_horizon_ocp_solver.hpp:253
const Vector< nub > & dummyopt() const
Getter of the optimal solution.
Definition zero_horizon_ocp_solver.hpp:168
static constexpr int nuc
Dimension of the concatenation of the control input and equality constraints.
Definition zero_horizon_ocp_solver.hpp:45
const Vector< nuc > & ucopt() const
Getter of the optimal solution.
Definition zero_horizon_ocp_solver.hpp:162
static constexpr int dim
Dimension of the linear problem solved by the GMRES solver.
Definition zero_horizon_ocp_solver.hpp:55
const Vector< nu > & uopt() const
Getter of the optimal solution.
Definition zero_horizon_ocp_solver.hpp:156
friend std::ostream & operator<<(std::ostream &os, const ZeroHorizonOCPSolver &solver)
Definition zero_horizon_ocp_solver.hpp:265
const Vector< nub > & muopt() const
Getter of the optimal solution.
Definition zero_horizon_ocp_solver.hpp:174
void solve(const Scalar t, const MatrixBase< VectorType > &x)
Solves the zero-horizon optimal control problem by Newton-GMRES method.
Definition zero_horizon_ocp_solver.hpp:210
detail::MatrixFreeGMRES< NewtonGMRES_, kmax > MatrixFreeGMRES_
Definition zero_horizon_ocp_solver.hpp:59
static constexpr int nc
Dimension of the equality constraints.
Definition zero_horizon_ocp_solver.hpp:40
Scalar optError() const
Gets the l2-norm of the current optimality errors.
Definition zero_horizon_ocp_solver.hpp:186
static constexpr int nx
Dimension of the state.
Definition zero_horizon_ocp_solver.hpp:30
void set_mu(const MatrixBase< VectorType > &mu)
Sets the Lagrange multiplier with respect to the control input bounds constraint.
Definition zero_horizon_ocp_solver.hpp:135
const Vector< nx > & lmdopt() const
Getter of the optimal solution.
Definition zero_horizon_ocp_solver.hpp:180
void set_u(const VectorType &u)
Sets the control input vector.
Definition zero_horizon_ocp_solver.hpp:93
ZeroHorizonOCPSolver(const OCP &ocp, const SolverSettings &settings)
Constructs the zero-horizon OCP solver.
Definition zero_horizon_ocp_solver.hpp:66
ZeroHorizonOCPSolver()=default
Default constructor.
static constexpr int nub
Dimension of the bound constraints on the control input.
Definition zero_horizon_ocp_solver.hpp:50
void disp(std::ostream &os) const
Definition zero_horizon_ocp_solver.hpp:257
void set_uc(const VectorType &uc)
Sets the control input vector and Lagrange multiplier with respect to the equality constraints.
Definition zero_horizon_ocp_solver.hpp:108
void init_dummy_mu()
Initializes the dummy input vectors and Lagrange multipliers with respect to the control input bounds...
Definition zero_horizon_ocp_solver.hpp:146
detail::NewtonGMRES< ZeroHorizonNLP_ > NewtonGMRES_
Definition zero_horizon_ocp_solver.hpp:58
const NLP & get_nlp() const
Definition newton_gmres.hpp:84
void synchronize_ocp()
Definition newton_gmres.hpp:86
void retrieve_mu(Vector< dim > &solution)
Definition newton_gmres.hpp:75
void retrieve_dummy(Vector< dim > &solution, const Scalar min_dummy)
Definition newton_gmres.hpp:70
Scalar optError() const
Definition newton_gmres.hpp:34
void eval_fonc(const Scalar t, const MatrixBase< VectorType > &x, const Vector< dim > &solution)
Definition newton_gmres.hpp:39
decltype(auto) lmd() const
Definition newton_gmres.hpp:82
Definition zero_horizon_nlp.hpp:15
const OCP & ocp() const
Definition zero_horizon_nlp.hpp:84
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
Settings of solvers.
Definition solver_settings.hpp:14
size_t max_iter
Maximum number of iterations of the ZeroHorizonOCPSolver method. Has nothing to do with SingleShootin...
Definition solver_settings.hpp:20
Scalar min_dummy
The minimum value of the dummy inputs. Mainly used in MultipleShootingCGMRESSolver....
Definition solver_settings.hpp:58
Scalar opterr_tol
Termination criterion of the ZeroHorizonOCPSolver method. Has nothing to do with SingleShootingCGMRES...
Definition solver_settings.hpp:27
size_t verbose_level
Verbose level. 0: no printings. 1-2: print some things. Default is 0.
Definition solver_settings.hpp:63
bool profile_solver
If true, a solver profile is taken.
Definition solver_settings.hpp:68
A profile of the timing benchmark.
Definition timer.hpp:16