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;
67 : newton_gmres_(
ZeroHorizonNLP_(ocp), settings.finite_difference_epsilon),
90 template <
typename VectorType>
91 void set_u(
const VectorType& u) {
93 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::set_u] u.size() must be " + std::to_string(
nu));
96 ucopt_.template head<nu>() = u;
105 template <
typename VectorType>
107 if (uc.size() !=
nuc) {
108 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::set_uc] uc.size() must be " + std::to_string(
nuc));
110 uopt_ = uc.template head<nu>();
119 template <
typename VectorType>
121 if (dummy.size() !=
nub) {
122 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::set_dummy] dummy.size() must be " + std::to_string(
nub));
132 template <
typename VectorType>
134 if (mu.size() !=
nub) {
135 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::set_mu] mu.size() must be " + std::to_string(
nub));
192 template <
typename VectorType>
194 if (x.size() !=
nx) {
195 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::optError] x.size() must be " + std::to_string(
nx));
198 newton_gmres_.
eval_fonc(t, x, solution_);
207 template <
typename VectorType>
209 if (x.size() !=
nx) {
210 throw std::invalid_argument(
"[ZeroHorizonOCPSolver::update] x.size() must be " + std::to_string(
nx));
213 std::cout <<
"\n======================= solve zero horizon OCP =======================" << std::endl;
217 for (
size_t iter=0; iter<settings_.
max_iter; ++iter) {
219 const auto gmres_iter
220 = gmres_.template solve<const Scalar, const VectorType&, const Vector<dim>&>(
221 newton_gmres_, t, x.derived(), solution_, solution_update_);
222 const auto opt_error = newton_gmres_.optError();
223 solution_.noalias() += solution_update_;
228 std::cout <<
"iter " << iter <<
": opt error: " << opt_error
229 <<
" (opt tol: " << settings_.
opterr_tol <<
")" << std::endl;
232 std::cout <<
" number of GMRES iter: " << gmres_iter
233 <<
" (kmax: " << kmax <<
")" << std::endl;
239 std::cout <<
"converged!" << std::endl;
255 void disp(std::ostream& os)
const {
256 os <<
"Zero horizon OCP solver: " << std::endl;
257 os <<
" kmax: " << kmax << std::endl;
258 os << newton_gmres_.
get_nlp().
ocp() << std::endl;
259 os << settings_ << std::endl;
268 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
282 void setInnerSolution() {
283 solution_.template head<nuc>() = ucopt_;
284 if constexpr (
nub > 0) {
285 solution_.template segment<nub>(
nuc) = dummyopt_;
286 solution_.template segment<nub>(
nuc+
nub) = muopt_;
290 void retriveSolution() {
291 uopt_ = solution_.template head<nu>();
292 ucopt_ = solution_.template head<nuc>();
293 dummyopt_ = solution_.template segment<nub>(
nuc);
294 muopt_ = solution_.template segment<nub>(
nuc+
nub);
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:193
~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:120
TimingProfile getProfile() const
Get timing result as TimingProfile.
Definition: zero_horizon_ocp_solver.hpp:251
const Vector< nub > & dummyopt() const
Getter of the optimal solution.
Definition: zero_horizon_ocp_solver.hpp:166
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:160
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:154
friend std::ostream & operator<<(std::ostream &os, const ZeroHorizonOCPSolver &solver)
Definition: zero_horizon_ocp_solver.hpp:263
const Vector< nub > & muopt() const
Getter of the optimal solution.
Definition: zero_horizon_ocp_solver.hpp:172
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:208
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:184
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:133
const Vector< nx > & lmdopt() const
Getter of the optimal solution.
Definition: zero_horizon_ocp_solver.hpp:178
void set_u(const VectorType &u)
Sets the control input vector.
Definition: zero_horizon_ocp_solver.hpp:91
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:255
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:106
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:144
detail::NewtonGMRES< ZeroHorizonNLP_ > NewtonGMRES_
Definition: zero_horizon_ocp_solver.hpp:58
void retrive_dummy(Vector< dim > &solution, const Scalar min_dummy)
Definition: newton_gmres.hpp:70
const NLP & get_nlp() const
Definition: newton_gmres.hpp:84
void synchronize_ocp()
Definition: newton_gmres.hpp:86
Scalar optError() const
Definition: newton_gmres.hpp:34
void retrive_mu(Vector< dim > &solution)
Definition: newton_gmres.hpp:75
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::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
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