Crocoddyl
solver-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh,
5 // Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
9 
10 #ifndef CROCODDYL_CORE_SOLVER_BASE_HPP_
11 #define CROCODDYL_CORE_SOLVER_BASE_HPP_
12 
13 #include "crocoddyl/core/fwd.hpp"
14 #include "crocoddyl/core/optctrl/shooting.hpp"
15 #include "crocoddyl/core/utils/stop-watch.hpp"
16 
17 namespace crocoddyl {
18 
19 class CallbackAbstract; // forward declaration
20 static std::vector<Eigen::VectorXd> DEFAULT_VECTOR;
21 
22 enum FeasibilityNorm { LInf = 0, L1 };
23 
61  public:
62  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
63 
69  explicit SolverAbstract(std::shared_ptr<ShootingProblem> problem);
70  virtual ~SolverAbstract();
71 
92  virtual bool solve(
93  const std::vector<Eigen::VectorXd>& init_xs = DEFAULT_VECTOR,
94  const std::vector<Eigen::VectorXd>& init_us = DEFAULT_VECTOR,
95  const std::size_t maxiter = 100, const bool is_feasible = false,
96  const double reg_init = NAN) = 0;
97 
114  virtual void computeDirection(const bool recalc) = 0;
115 
128  virtual double tryStep(const double steplength = 1) = 0;
129 
138  virtual double stoppingCriteria() = 0;
139 
147  virtual const Eigen::Vector2d& expectedImprovement() = 0;
148 
155  virtual void resizeData();
156 
169  double computeDynamicFeasibility();
170 
181 
192 
209  void setCandidate(
210  const std::vector<Eigen::VectorXd>& xs_warm = DEFAULT_VECTOR,
211  const std::vector<Eigen::VectorXd>& us_warm = DEFAULT_VECTOR,
212  const bool is_feasible = false);
213 
222  void setCallbacks(
223  const std::vector<std::shared_ptr<CallbackAbstract> >& callbacks);
224 
228  const std::vector<std::shared_ptr<CallbackAbstract> >& getCallbacks() const;
229 
233  const std::shared_ptr<ShootingProblem>& get_problem() const;
234 
238  const std::vector<Eigen::VectorXd>& get_xs() const;
239 
243  const std::vector<Eigen::VectorXd>& get_us() const;
244 
248  const std::vector<Eigen::VectorXd>& get_fs() const;
249 
254  bool get_is_feasible() const;
255 
259  double get_cost() const;
260 
264  double get_merit() const;
265 
269  double get_stop() const;
270 
274  const Eigen::Vector2d& get_d() const;
275 
279  double get_dV() const;
280 
284  double get_dPhi() const;
285 
290  double get_dVexp() const;
291 
296  double get_dPhiexp() const;
297 
301  double get_dfeas() const;
302 
306  double get_feas() const;
307 
311  double get_ffeas() const;
312 
316  double get_gfeas() const;
317 
321  double get_hfeas() const;
322 
326  double get_ffeas_try() const;
327 
331  double get_gfeas_try() const;
332 
336  double get_hfeas_try() const;
337 
341  double get_preg() const;
342 
346  double get_dreg() const;
347 
348  DEPRECATED("Use get_preg for primal-variable regularization",
349  double get_xreg() const;)
350  DEPRECATED("Use get_preg for primal-variable regularization",
351  double get_ureg() const;)
352 
356  double get_steplength() const;
357 
361  double get_th_acceptstep() const;
362 
366  double get_th_stop() const;
367 
371  double get_th_gaptol() const;
372 
377  FeasibilityNorm get_feasnorm() const;
378 
382  std::size_t get_iter() const;
383 
387  void set_xs(const std::vector<Eigen::VectorXd>& xs);
388 
392  void set_us(const std::vector<Eigen::VectorXd>& us);
393 
397  void set_preg(const double preg);
398 
402  void set_dreg(const double dreg);
403 
404  DEPRECATED("Use set_preg for primal-variable regularization",
405  void set_xreg(const double xreg);)
406  DEPRECATED("Use set_preg for primal-variable regularization",
407  void set_ureg(const double ureg);)
408 
412  void set_th_acceptstep(const double th_acceptstep);
413 
417  void set_th_stop(const double th_stop);
418 
422  void set_th_gaptol(const double th_gaptol);
423 
428  void set_feasnorm(const FeasibilityNorm feas_norm);
429 
430  protected:
431  std::shared_ptr<ShootingProblem> problem_;
432  std::vector<Eigen::VectorXd> xs_;
433  std::vector<Eigen::VectorXd> us_;
434  std::vector<Eigen::VectorXd> fs_;
435  std::vector<std::shared_ptr<CallbackAbstract> >
440  double cost_;
441  double merit_;
442  double stop_;
443  Eigen::Vector2d d_;
444  double dV_;
445  double dPhi_;
446  double dVexp_;
447  double dPhiexp_;
448  double dfeas_;
449  double feas_;
450  double
452  double gfeas_;
454  double hfeas_;
456  double ffeas_try_;
458  double gfeas_try_;
460  double hfeas_try_;
462  double preg_;
463  double dreg_;
464  DEPRECATED("Use preg_ for primal-variable regularization",
465  double xreg_;)
466  DEPRECATED("Use dreg_ for primal-variable regularization",
467  double ureg_;)
468  double steplength_;
469  double th_acceptstep_;
470  double th_stop_;
471  double th_gaptol_;
472  enum FeasibilityNorm feasnorm_;
474  std::size_t iter_;
475  double tmp_feas_;
476  std::vector<Eigen::VectorXd> g_adj_;
477 };
478 
487  public:
492  virtual ~CallbackAbstract() {}
493 
499  virtual void operator()(SolverAbstract& solver) = 0;
500 };
501 
502 bool raiseIfNaN(const double value);
503 
504 } // namespace crocoddyl
505 
506 #endif // CROCODDYL_CORE_SOLVER_BASE_HPP_
Abstract class for solver callbacks.
CallbackAbstract()
Initialize the callback function.
virtual void operator()(SolverAbstract &solver)=0
Run the callback function given a solver.
Abstract class for optimal control solvers.
Definition: solver-base.hpp:60
double get_cost() const
Return the cost for the current guess.
std::vector< Eigen::VectorXd > g_adj_
Adjusted inequality bound.
double get_dPhi() const
Return the reduction in the merit function .
double get_th_gaptol() const
Return the threshold for accepting a gap as non-zero.
double dVexp_
Expected reduction in the cost function.
std::vector< Eigen::VectorXd > xs_
State trajectory.
std::size_t get_iter() const
Return the number of iterations performed by the solver.
double get_hfeas() const
Return the equality feasibility for the current guess.
void set_th_stop(const double th_stop)
Modify the tolerance for stopping the algorithm.
double stop_
Value computed by stoppingCriteria()
void set_xs(const std::vector< Eigen::VectorXd > &xs)
Modify the state trajectory .
double get_dVexp() const
Return the expected reduction in the cost function .
double dreg_
Current dual-variable regularization value.
double feas_
Total feasibility for the current guess.
bool is_feasible_
Label that indicates is the iteration is feasible.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SolverAbstract(std::shared_ptr< ShootingProblem > problem)
Initialize the solver.
Definition: solver-base.cpp:18
std::shared_ptr< ShootingProblem > problem_
optimal control problem
std::vector< Eigen::VectorXd > us_
Control trajectory.
double get_dPhiexp() const
Return the expected reduction in the merit function .
double th_acceptstep_
Threshold used for accepting step.
double get_steplength() const
Return the step length .
virtual const Eigen::Vector2d & expectedImprovement()=0
Return the expected improvement from a given current search direction .
void set_th_gaptol(const double th_gaptol)
Modify the threshold for accepting a gap as non-zero.
double get_merit() const
Return the merit for the current guess.
double dPhi_
Reduction in the merit function computed by tryStep()
const std::vector< std::shared_ptr< CallbackAbstract > > & getCallbacks() const
Return the list of callback functions using for diagnostic.
double computeInequalityFeasibility()
Compute the feasibility of the inequality constraints for the current guess.
double get_preg() const
Return the primal-variable regularization.
double get_hfeas_try() const
Return the equality feasibility for the current step length.
double th_stop_
Tolerance for stopping the algorithm.
double computeDynamicFeasibility()
Compute the dynamic feasibility for the current guess .
Definition: solver-base.cpp:91
virtual void computeDirection(const bool recalc)=0
Compute the search direction for the current guess .
const Eigen::Vector2d & get_d() const
Return the linear and quadratic terms of the expected improvement.
double dPhiexp_
Expected reduction in the merit function.
enum FeasibilityNorm feasnorm_
void setCandidate(const std::vector< Eigen::VectorXd > &xs_warm=DEFAULT_VECTOR, const std::vector< Eigen::VectorXd > &us_warm=DEFAULT_VECTOR, const bool is_feasible=false)
Set the solver candidate trajectories .
double get_th_stop() const
Return the tolerance for stopping the algorithm.
double dfeas_
Reduction in the feasibility.
void set_feasnorm(const FeasibilityNorm feas_norm)
Modify the current norm used for computed the dynamic and constraint feasibility.
double get_ffeas() const
Return the dynamic feasibility for the current guess.
double get_gfeas() const
Return the inequality feasibility for the current guess.
double cost_
Cost for the current guess.
std::vector< std::shared_ptr< CallbackAbstract > > callbacks_
Callback functions.
virtual double tryStep(const double steplength=1)=0
Try a predefined step length and compute its cost improvement .
void set_us(const std::vector< Eigen::VectorXd > &us)
Modify the control trajectory .
double steplength_
< Current control regularization values
void setCallbacks(const std::vector< std::shared_ptr< CallbackAbstract > > &callbacks)
Set a list of callback functions using for the solver diagnostic.
double th_gaptol_
Threshold limit to check non-zero gaps.
std::size_t iter_
Number of iteration performed by the solver.
double get_feas() const
Return the total feasibility for the current guess.
double dV_
Reduction in the cost function computed by tryStep()
double get_stop() const
Return the stopping-criteria value computed by stoppingCriteria()
double get_ffeas_try() const
Return the dynamic feasibility for the current step length.
void set_dreg(const double dreg)
Modify the dual-variable regularization value.
Eigen::Vector2d d_
LQ approximation of the expected improvement.
double get_dV() const
Return the reduction in the cost function .
const std::vector< Eigen::VectorXd > & get_fs() const
Return the dynamic infeasibility .
const std::vector< Eigen::VectorXd > & get_xs() const
Return the state trajectory .
double get_dfeas() const
Return the reduction in the feasibility.
void set_preg(const double preg)
Modify the primal-variable regularization value.
double ffeas_
Feasibility of the dynamic constraints for the current guess.
double get_gfeas_try() const
Return the inequality feasibility for the current step length.
bool get_is_feasible() const
Return the feasibility status of the trajectory.
double preg_
Current primal-variable regularization value.
const std::vector< Eigen::VectorXd > & get_us() const
Return the control trajectory .
double merit_
Merit for the current guess.
virtual void resizeData()
Resizing the solver data.
Definition: solver-base.cpp:72
virtual bool solve(const std::vector< Eigen::VectorXd > &init_xs=DEFAULT_VECTOR, const std::vector< Eigen::VectorXd > &init_us=DEFAULT_VECTOR, const std::size_t maxiter=100, const bool is_feasible=false, const double reg_init=NAN)=0
Compute the optimal trajectory as lists of and terms.
std::vector< Eigen::VectorXd > fs_
Gaps/defects between shooting nodes.
virtual double stoppingCriteria()=0
Return a positive value that quantifies the algorithm termination.
void set_th_acceptstep(const double th_acceptstep)
Modify the threshold used for accepting step.
const std::shared_ptr< ShootingProblem > & get_problem() const
Return the shooting problem.
double tmp_feas_
Temporal variables used for computed the feasibility.
double get_th_acceptstep() const
Return the threshold used for accepting a step.
FeasibilityNorm get_feasnorm() const
Return the type of norm used to evaluate the dynamic and constraints feasibility.
double get_dreg() const
Return the dual-variable regularization.
double computeEqualityFeasibility()
Compute the feasibility of the equality constraints for the current guess.