Crocoddyl
Loading...
Searching...
No Matches
ipopt-iface.hpp
1
2// BSD 3-Clause License
3//
4// Copyright (C) 2022-2025, IRI: CSIC-UPC, Heriot-Watt University
5// Copyright note valid unless otherwise stated in individual files.
6// All rights reserved.
8
9#ifndef __CROCODDYL_CORE_SOLVERS_IPOPT_IPOPT_IFACE_HPP__
10#define __CROCODDYL_CORE_SOLVERS_IPOPT_IPOPT_IFACE_HPP__
11
12#define HAVE_CSTDDEF
13#include <IpTNLP.hpp>
14#undef HAVE_CSTDDEF
15
16#include "crocoddyl/core/optctrl/shooting.hpp"
17
18namespace crocoddyl {
19
59template <typename _Scalar>
60class IpoptInterfaceTpl : public Ipopt::TNLP {
61 public:
62 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
63
64 typedef _Scalar Scalar;
65 typedef ShootingProblemTpl<Scalar> ShootingProblem;
66 typedef typename ShootingProblem::ActionModelAbstract ActionModelAbstract;
67 typedef typename ShootingProblem::ActionDataAbstract ActionDataAbstract;
68 typedef IpoptInterfaceDataTpl<Scalar> IpoptInterfaceData;
69 typedef MathBaseTpl<Scalar> MathBase;
70 typedef typename MathBase::VectorXs VectorXs;
71
77 IpoptInterfaceTpl(const std::shared_ptr<ShootingProblem>& problem);
78
79 virtual ~IpoptInterfaceTpl() = default;
80
96 virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m,
97 Ipopt::Index& nnz_jac_g, Ipopt::Index& nnz_h_lag,
98 IndexStyleEnum& index_style);
99
127 virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l,
128 Ipopt::Number* x_u, Ipopt::Index m,
129 Ipopt::Number* g_l, Ipopt::Number* g_u);
130
163 // [TNLP_get_starting_point]
164 virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x,
165 bool init_z, Ipopt::Number* z_L,
166 Ipopt::Number* z_U, Ipopt::Index m,
167 bool init_lambda, Ipopt::Number* lambda);
168
186 virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
187 Ipopt::Number& obj_value);
188
206 virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
207 Ipopt::Number* grad_f);
208
226 virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
227 Ipopt::Index m, Ipopt::Number* g);
228
272 virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
273 Ipopt::Index m, Ipopt::Index nele_jac,
274 Ipopt::Index* iRow, Ipopt::Index* jCol,
275 Ipopt::Number* values);
276
331 virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
332 Ipopt::Number obj_factor, Ipopt::Index m,
333 const Ipopt::Number* lambda, bool new_lambda,
334 Ipopt::Index nele_hess, Ipopt::Index* iRow,
335 Ipopt::Index* jCol, Ipopt::Number* values);
336
382 virtual void finalize_solution(
383 Ipopt::SolverReturn status, Ipopt::Index n, const Ipopt::Number* x,
384 const Ipopt::Number* z_L, const Ipopt::Number* z_U, Ipopt::Index m,
385 const Ipopt::Number* g, const Ipopt::Number* lambda,
386 Ipopt::Number obj_value, const Ipopt::IpoptData* ip_data,
387 Ipopt::IpoptCalculatedQuantities* ip_cq);
388
412 Ipopt::AlgorithmMode mode, Ipopt::Index iter, Ipopt::Number obj_value,
413 Ipopt::Number inf_pr, Ipopt::Number inf_du, Ipopt::Number mu,
414 Ipopt::Number d_norm, Ipopt::Number regularization_size,
415 Ipopt::Number alpha_du, Ipopt::Number alpha_pr, Ipopt::Index ls_trials,
416 const Ipopt::IpoptData* ip_data, Ipopt::IpoptCalculatedQuantities* ip_cq);
417
423 std::shared_ptr<IpoptInterfaceData> createData(const std::size_t nx,
424 const std::size_t ndx,
425 const std::size_t nu);
426
427 void resizeData();
428
433 std::size_t get_nvar() const;
434
438 std::size_t get_nconst() const;
439
443 const std::vector<VectorXs>& get_xs() const;
444
448 const std::vector<VectorXs>& get_us() const;
449
453 const std::shared_ptr<ShootingProblem>& get_problem() const;
454
455 Scalar get_cost() const;
456
460 void set_xs(const std::vector<VectorXs>& xs);
461
465 void set_us(const std::vector<VectorXs>& us);
466
467 private:
468 std::shared_ptr<ShootingProblem> problem_;
469 std::vector<VectorXs> xs_;
470 std::vector<VectorXs> us_;
471 std::vector<std::size_t> ixu_;
472 std::size_t nvar_;
473 std::size_t nconst_;
474 std::vector<std::shared_ptr<IpoptInterfaceData>> datas_;
475 Scalar cost_;
476
478
479 IpoptInterfaceTpl& operator=(const IpoptInterfaceTpl&);
480};
481
482template <typename _Scalar>
483struct IpoptInterfaceDataTpl {
484 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
485
486 typedef _Scalar Scalar;
487 typedef MathBaseTpl<Scalar> MathBase;
488 typedef typename MathBase::VectorXs VectorXs;
489 typedef typename MathBase::MatrixXs MatrixXs;
490
491 IpoptInterfaceDataTpl(const std::size_t nx, const std::size_t ndx,
492 const std::size_t nu)
493 : x(nx),
494 xnext(nx),
495 dx(ndx),
496 dxnext(ndx),
497 x_diff(ndx),
498 u(nu),
499 Jint_dx(ndx, ndx),
500 Jint_dxnext(ndx, ndx),
501 Jdiff_x(ndx, ndx),
502 Jdiff_xnext(ndx, ndx),
503 Jg_dx(ndx, ndx),
504 Jg_dxnext(ndx, ndx),
505 Jg_u(ndx, ndx),
506 Jg_ic(ndx, ndx),
507 FxJint_dx(ndx, ndx),
508 Ldx(ndx),
509 Ldxdx(ndx, ndx),
510 Ldxu(ndx, nu) {
511 x.setZero();
512 xnext.setZero();
513 dx.setZero();
514 dxnext.setZero();
515 x_diff.setZero();
516 u.setZero();
517 Jint_dx.setZero();
518 Jint_dxnext.setZero();
519 Jdiff_x.setZero();
520 Jdiff_xnext.setZero();
521 Jg_dx.setZero();
522 Jg_dxnext.setZero();
523 Jg_u.setZero();
524 Jg_ic.setZero();
525 FxJint_dx.setZero();
526 Ldx.setZero();
527 Ldxdx.setZero();
528 Ldxu.setZero();
529 }
530
531 void resize(const std::size_t nx, const std::size_t ndx,
532 const std::size_t nu) {
533 x.conservativeResize(nx);
534 xnext.conservativeResize(nx);
535 dx.conservativeResize(ndx);
536 dxnext.conservativeResize(ndx);
537 x_diff.conservativeResize(ndx);
538 u.conservativeResize(nu);
539 Jint_dx.conservativeResize(ndx, ndx);
540 Jint_dxnext.conservativeResize(ndx, ndx);
541 Jdiff_x.conservativeResize(ndx, ndx);
542 Jdiff_xnext.conservativeResize(ndx, ndx);
543 Jg_dx.conservativeResize(ndx, ndx);
544 Jg_dxnext.conservativeResize(ndx, ndx);
545 Jg_u.conservativeResize(ndx, ndx);
546 Jg_ic.conservativeResize(ndx, ndx);
547 FxJint_dx.conservativeResize(ndx, ndx);
548 Ldx.conservativeResize(ndx);
549 Ldxdx.conservativeResize(ndx, ndx);
550 Ldxu.conservativeResize(ndx, nu);
551 }
552
553 VectorXs x;
554 VectorXs xnext;
555 VectorXs dx;
556 VectorXs dxnext;
557 VectorXs x_diff;
558 VectorXs u;
559 MatrixXs Jint_dx;
560 MatrixXs
562 MatrixXs Jdiff_x;
563 MatrixXs Jdiff_xnext;
565 MatrixXs Jg_dx;
566 MatrixXs Jg_dxnext;
567 MatrixXs Jg_u;
568 MatrixXs Jg_ic;
569 MatrixXs FxJint_dx;
570 VectorXs Ldx;
571 MatrixXs Ldxdx;
572 MatrixXs Ldxu;
573};
574
575} // namespace crocoddyl
576
577/* --- Details -------------------------------------------------------------- */
578/* --- Details -------------------------------------------------------------- */
579/* --- Details -------------------------------------------------------------- */
580#include "crocoddyl/core/solvers/ipopt/ipopt-iface.hxx"
581
582#endif
virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number *x, bool init_z, Ipopt::Number *z_L, Ipopt::Number *z_U, Ipopt::Index m, bool init_lambda, Ipopt::Number *lambda)
Method to request the starting point before iterating.
virtual bool eval_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Number *g)
Method to request the constraint values.
const std::vector< VectorXs > & get_us() const
Return the control vector.
std::size_t get_nvar() const
Return the total number of optimization variables (states and controls).
void set_us(const std::vector< VectorXs > &us)
Modify the control vector.
void set_xs(const std::vector< VectorXs > &xs)
Modify the state vector.
virtual bool eval_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number &obj_value)
Method to request the value of the objective function.
virtual bool get_nlp_info(Ipopt::Index &n, Ipopt::Index &m, Ipopt::Index &nnz_jac_g, Ipopt::Index &nnz_h_lag, IndexStyleEnum &index_style)
Methods to gather information about the NLP.
virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index *iRow, Ipopt::Index *jCol, Ipopt::Number *values)
Method to request either the sparsity structure or the values of the Jacobian of the constraints.
virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number *grad_f)
Method to request the gradient of the objective function.
virtual bool eval_h(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number *lambda, bool new_lambda, Ipopt::Index nele_hess, Ipopt::Index *iRow, Ipopt::Index *jCol, Ipopt::Number *values)
Method to request either the sparsity structure or the values of the Hessian of the Lagrangian.
IpoptInterfaceTpl(const std::shared_ptr< ShootingProblem > &problem)
Initialize the Ipopt interface.
std::size_t get_nconst() const
Return the total number of constraints in the NLP.
const std::vector< VectorXs > & get_xs() const
Return the state vector.
bool intermediate_callback(Ipopt::AlgorithmMode mode, Ipopt::Index iter, Ipopt::Number obj_value, Ipopt::Number inf_pr, Ipopt::Number inf_du, Ipopt::Number mu, Ipopt::Number d_norm, Ipopt::Number regularization_size, Ipopt::Number alpha_du, Ipopt::Number alpha_pr, Ipopt::Index ls_trials, const Ipopt::IpoptData *ip_data, Ipopt::IpoptCalculatedQuantities *ip_cq)
Intermediate Callback method for the user.
virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number *x_l, Ipopt::Number *x_u, Ipopt::Index m, Ipopt::Number *g_l, Ipopt::Number *g_u)
Method to request bounds on the variables and constraints. T.
std::shared_ptr< IpoptInterfaceData > createData(const std::size_t nx, const std::size_t ndx, const std::size_t nu)
Create the data structure to store temporary computations.
const std::shared_ptr< ShootingProblem > & get_problem() const
Return the crocoddyl::ShootingProblem to be solved.
virtual void finalize_solution(Ipopt::SolverReturn status, Ipopt::Index n, const Ipopt::Number *x, const Ipopt::Number *z_L, const Ipopt::Number *z_U, Ipopt::Index m, const Ipopt::Number *g, const Ipopt::Number *lambda, Ipopt::Number obj_value, const Ipopt::IpoptData *ip_data, Ipopt::IpoptCalculatedQuantities *ip_cq)
This method is called when the algorithm has finished (successfully or not) so the TNLP can digest th...
This class encapsulates a shooting problem.
Definition shooting.hpp:32
VectorXs xnext
Integrated state at next node.
MatrixXs Jg_dxnext
Jacobian of the dynamic constraint w.r.t dxnext.
VectorXs Ldx
Jacobian of the cost w.r.t dx.
MatrixXs Jg_u
Jacobian of the dynamic constraint w.r.t u.
MatrixXs Jg_ic
Jacobian of the initial condition constraint w.r.t dx.
MatrixXs Ldxu
Hessian of the cost w.r.t dxu.
VectorXs x_diff
State difference.
MatrixXs Jg_dx
Jacobian of the dynamic constraint w.r.t dx.
VectorXs x
Integrated state.
MatrixXs FxJint_dx
Intermediate computation needed for Jg_ic.
MatrixXs Ldxdx
Hessian of the cost w.r.t dxdx.
MatrixXs Jint_dx
Jacobian of the sum operation w.r.t dx.
VectorXs dx
Increment in the tangent space.
MatrixXs Jdiff_x
Jacobian of the diff operation w.r.t the first element.
MatrixXs Jint_dxnext
Jacobian of the sum operation w.r.t dx at next node.
VectorXs dxnext
Increment in the tangent space at next node.