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
66 ZeroHorizonOCPSolver(const OCP& ocp, const SolverSettings& settings)
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 solution_(Vector<dim>::Zero()),
73 solution_update_(Vector<dim>::Zero()) {
74 }
75
80
85
90 template <typename VectorType>
91 void set_u(const VectorType& u) {
92 if (u.size() != nu) {
93 throw std::invalid_argument("[ZeroHorizonOCPSolver::set_u] u.size() must be " + std::to_string(nu));
94 }
95 uopt_ = u;
96 ucopt_.template head<nu>() = u;
97 setInnerSolution();
98 }
99
105 template <typename VectorType>
106 void set_uc(const VectorType& uc) {
107 if (uc.size() != nuc) {
108 throw std::invalid_argument("[ZeroHorizonOCPSolver::set_uc] uc.size() must be " + std::to_string(nuc));
109 }
110 uopt_ = uc.template head<nu>();
111 ucopt_ = uc;
112 setInnerSolution();
113 }
114
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));
123 }
124 dummyopt_ = dummy;
125 setInnerSolution();
126 }
127
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));
136 }
137 muopt_ = mu;
138 setInnerSolution();
139 }
140
145 newton_gmres_.retrive_dummy(solution_, settings_.min_dummy);
146 newton_gmres_.retrive_mu(solution_);
147 retriveSolution();
148 }
149
154 const Vector<nu>& uopt() const { return uopt_; }
155
160 const Vector<nuc>& ucopt() const { return ucopt_; }
161
166 const Vector<nub>& dummyopt() const { return dummyopt_; }
167
172 const Vector<nub>& muopt() const { return muopt_; }
173
178 const Vector<nx>& lmdopt() const { return newton_gmres_.lmd(); }
179
184 Scalar optError() const { return newton_gmres_.optError(); }
185
192 template <typename VectorType>
194 if (x.size() != nx) {
195 throw std::invalid_argument("[ZeroHorizonOCPSolver::optError] x.size() must be " + std::to_string(nx));
196 }
197 newton_gmres_.synchronize_ocp();
198 newton_gmres_.eval_fonc(t, x, solution_);
199 return optError();
200 }
201
207 template <typename VectorType>
208 void solve(const Scalar t, const MatrixBase<VectorType>& x) {
209 if (x.size() != nx) {
210 throw std::invalid_argument("[ZeroHorizonOCPSolver::update] x.size() must be " + std::to_string(nx));
211 }
212 if (settings_.verbose_level >= 1) {
213 std::cout << "\n======================= solve zero horizon OCP =======================" << std::endl;
214 }
215
216 newton_gmres_.synchronize_ocp();
217 for (size_t iter=0; iter<settings_.max_iter; ++iter) {
218 if (settings_.profile_solver) timer_.tick();
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_;
224 if (settings_.profile_solver) timer_.tock();
225
226 // verbose
227 if (settings_.verbose_level >= 1) {
228 std::cout << "iter " << iter << ": opt error: " << opt_error
229 << " (opt tol: " << settings_.opterr_tol << ")" << std::endl;
230 }
231 if (settings_.verbose_level >= 2) {
232 std::cout << " number of GMRES iter: " << gmres_iter
233 << " (kmax: " << kmax << ")" << std::endl;
234 }
235
236 // check convergence
237 if (opt_error < settings_.opterr_tol) {
238 if (settings_.verbose_level >= 1) {
239 std::cout << "converged!" << std::endl;
240 }
241 break;
242 }
243 }
244 retriveSolution();
245 }
246
252 return timer_.getProfile();
253 }
254
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;
260 os << timer_.getProfile() << std::flush;
261 }
262
263 friend std::ostream& operator<<(std::ostream& os, const ZeroHorizonOCPSolver& solver) {
264 solver.disp(os);
265 return os;
266 }
267
268 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
269
270private:
271 NewtonGMRES_ newton_gmres_;
272 MatrixFreeGMRES_ gmres_;
273 SolverSettings settings_;
274 Timer timer_;
275
276 Vector<nu> uopt_;
277 Vector<nuc> ucopt_;
278 Vector<nub> dummyopt_, muopt_;
279
280 Vector<dim> solution_, solution_update_;
281
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_;
287 }
288 }
289
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);
295 }
296
297};
298
299} // namespace cgmres
300
301#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: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