autogenu-jupyter
An automatic code generator and the continuation/GMRES (C/GMRES) based numerical solvers for nonlinear MPC
Loading...
Searching...
No Matches
zero_horizon_ocp_solver.hpp
Go to the documentation of this file.
1#ifndef CGMRES__ZERO_HORIZON_OCP_SOLVER_HPP_
2#define CGMRES__ZERO_HORIZON_OCP_SOLVER_HPP_
3
4#include <array>
5#include <stdexcept>
6#include <iostream>
7
8#include "cgmres/types.hpp"
10#include "cgmres/timer.hpp"
11
15
16namespace cgmres {
17
24template <class OCP, int kmax>
26public:
30 static constexpr int nx = OCP::nx;
31
35 static constexpr int nu = OCP::nu;
36
40 static constexpr int nc = OCP::nc;
41
45 static constexpr int nuc = nu + nc;
46
50 static constexpr int nub = OCP::nub;
51
55 static constexpr int dim = nuc + 2 * nub;
56
60
67 : newton_gmres_(ZeroHorizonNLP_(ocp), settings.finite_difference_epsilon),
68 gmres_(),
69 settings_(settings),
70 uopt_(Vector<nu>::Zero()),
71 ucopt_(Vector<nuc>::Zero()),
72 dummyopt_(Vector<nub>::Zero()),
73 muopt_(Vector<nub>::Zero()),
74 solution_(Vector<dim>::Zero()),
75 solution_update_(Vector<dim>::Zero()) {
76 }
77
82
87
92 template <typename VectorType>
93 void set_u(const VectorType& u) {
94 if (u.size() != nu) {
95 throw std::invalid_argument("[ZeroHorizonOCPSolver::set_u] u.size() must be " + std::to_string(nu));
96 }
97 uopt_ = u;
98 ucopt_.template head<nu>() = u;
99 setInnerSolution();
100 }
101
107 template <typename VectorType>
108 void set_uc(const VectorType& uc) {
109 if (uc.size() != nuc) {
110 throw std::invalid_argument("[ZeroHorizonOCPSolver::set_uc] uc.size() must be " + std::to_string(nuc));
111 }
112 uopt_ = uc.template head<nu>();
113 ucopt_ = uc;
114 setInnerSolution();
115 }
116
121 template <typename VectorType>
123 if (dummy.size() != nub) {
124 throw std::invalid_argument("[ZeroHorizonOCPSolver::set_dummy] dummy.size() must be " + std::to_string(nub));
125 }
126 dummyopt_ = dummy;
127 setInnerSolution();
128 }
129
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));
138 }
139 muopt_ = mu;
140 setInnerSolution();
141 }
142
147 newton_gmres_.retrieve_dummy(solution_, settings_.min_dummy);
148 newton_gmres_.retrieve_mu(solution_);
149 retrieveSolution();
150 }
151
156 const Vector<nu>& uopt() const { return uopt_; }
157
162 const Vector<nuc>& ucopt() const { return ucopt_; }
163
168 const Vector<nub>& dummyopt() const { return dummyopt_; }
169
174 const Vector<nub>& muopt() const { return muopt_; }
175
180 const Vector<nx>& lmdopt() const { return newton_gmres_.lmd(); }
181
186 Scalar optError() const { return newton_gmres_.optError(); }
187
194 template <typename VectorType>
196 if (x.size() != nx) {
197 throw std::invalid_argument("[ZeroHorizonOCPSolver::optError] x.size() must be " + std::to_string(nx));
198 }
199 newton_gmres_.synchronize_ocp();
200 newton_gmres_.eval_fonc(t, x, solution_);
201 return optError();
202 }
203
209 template <typename VectorType>
210 void solve(const Scalar t, const MatrixBase<VectorType>& x) {
211 if (x.size() != nx) {
212 throw std::invalid_argument("[ZeroHorizonOCPSolver::update] x.size() must be " + std::to_string(nx));
213 }
214 if (settings_.verbose_level >= 1) {
215 std::cout << "\n======================= solve zero horizon OCP =======================" << std::endl;
216 }
217
218 newton_gmres_.synchronize_ocp();
219 for (size_t iter=0; iter<settings_.max_iter; ++iter) {
220 if (settings_.profile_solver) timer_.tick();
221 const auto gmres_iter
223 newton_gmres_, t, x.derived(), solution_, solution_update_);
224 const auto opt_error = newton_gmres_.optError();
225 solution_.noalias() += solution_update_;
226 if (settings_.profile_solver) timer_.tock();
227
228 // verbose
229 if (settings_.verbose_level >= 1) {
230 std::cout << "iter " << iter << ": opt error: " << opt_error
231 << " (opt tol: " << settings_.opterr_tol << ")" << std::endl;
232 }
233 if (settings_.verbose_level >= 2) {
234 std::cout << " number of GMRES iter: " << gmres_iter
235 << " (kmax: " << kmax << ")" << std::endl;
236 }
237
238 // check convergence
239 if (opt_error < settings_.opterr_tol) {
240 if (settings_.verbose_level >= 1) {
241 std::cout << "converged!" << std::endl;
242 }
243 break;
244 }
245 }
246 retrieveSolution();
247 }
248
254 return timer_.getProfile();
255 }
256
257 void disp(std::ostream& os) const {
258 os << "Zero horizon OCP solver: " << std::endl;
259 os << " kmax: " << kmax << std::endl;
260 os << newton_gmres_.get_nlp().ocp() << std::endl;
261 os << settings_ << std::endl;
262 os << timer_.getProfile() << std::flush;
263 }
264
265 friend std::ostream& operator<<(std::ostream& os, const ZeroHorizonOCPSolver& solver) {
266 solver.disp(os);
267 return os;
268 }
269
271
272private:
273 NewtonGMRES_ newton_gmres_;
274 MatrixFreeGMRES_ gmres_;
275 SolverSettings settings_;
276 Timer timer_;
277
278 Vector<nu> uopt_;
279 Vector<nuc> ucopt_;
280 Vector<nub> dummyopt_, muopt_;
281
282 Vector<dim> solution_, solution_update_;
283
284 void setInnerSolution() {
285 solution_.template head<nuc>() = ucopt_;
286 if constexpr (nub > 0) {
287 solution_.template segment<nub>(nuc) = dummyopt_;
288 solution_.template segment<nub>(nuc+nub) = muopt_;
289 }
290 }
291
292 void retrieveSolution() {
293 uopt_ = solution_.template head<nu>();
294 ucopt_ = solution_.template head<nuc>();
295 dummyopt_ = solution_.template segment<nub>(nuc);
296 muopt_ = solution_.template segment<nub>(nuc+nub);
297 }
298
299};
300
301} // namespace cgmres
302
303#endif // CGMRES__ZERO_HORIZON_OCP_SOLVER_HPP_
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