Crocoddyl
solver-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2023, 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 <vector>
14 
15 #include "crocoddyl/core/optctrl/shooting.hpp"
16 #include "crocoddyl/core/utils/stop-watch.hpp"
17 
18 namespace crocoddyl {
19 
20 class CallbackAbstract; // forward declaration
21 static std::vector<Eigen::VectorXd> DEFAULT_VECTOR;
22 
23 enum FeasibilityNorm { LInf = 0, L1 };
24 
62  public:
63  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
64 
70  explicit SolverAbstract(boost::shared_ptr<ShootingProblem> problem);
71  virtual ~SolverAbstract();
72 
93  virtual bool solve(
94  const std::vector<Eigen::VectorXd>& init_xs = DEFAULT_VECTOR,
95  const std::vector<Eigen::VectorXd>& init_us = DEFAULT_VECTOR,
96  const std::size_t maxiter = 100, const bool is_feasible = false,
97  const double reg_init = NAN) = 0;
98 
115  virtual void computeDirection(const bool recalc) = 0;
116 
129  virtual double tryStep(const double steplength = 1) = 0;
130 
139  virtual double stoppingCriteria() = 0;
140 
148  virtual const Eigen::Vector2d& expectedImprovement() = 0;
149 
156  virtual void resizeData();
157 
170  double computeDynamicFeasibility();
171 
182 
193 
210  void setCandidate(
211  const std::vector<Eigen::VectorXd>& xs_warm = DEFAULT_VECTOR,
212  const std::vector<Eigen::VectorXd>& us_warm = DEFAULT_VECTOR,
213  const bool is_feasible = false);
214 
223  void setCallbacks(
224  const std::vector<boost::shared_ptr<CallbackAbstract> >& callbacks);
225 
229  const std::vector<boost::shared_ptr<CallbackAbstract> >& getCallbacks() const;
230 
234  const boost::shared_ptr<ShootingProblem>& get_problem() const;
235 
239  const std::vector<Eigen::VectorXd>& get_xs() const;
240 
244  const std::vector<Eigen::VectorXd>& get_us() const;
245 
249  const std::vector<Eigen::VectorXd>& get_fs() const;
250 
255  bool get_is_feasible() const;
256 
260  double get_cost() const;
261 
265  double get_merit() const;
266 
270  double get_stop() const;
271 
275  const Eigen::Vector2d& get_d() const;
276 
280  double get_dV() const;
281 
285  double get_dPhi() const;
286 
291  double get_dVexp() const;
292 
297  double get_dPhiexp() const;
298 
302  double get_dfeas() const;
303 
307  double get_feas() const;
308 
312  double get_ffeas() const;
313 
317  double get_gfeas() const;
318 
322  double get_hfeas() const;
323 
327  double get_ffeas_try() const;
328 
332  double get_gfeas_try() const;
333 
337  double get_hfeas_try() const;
338 
342  double get_preg() const;
343 
347  double get_dreg() const;
348 
349  DEPRECATED("Use get_preg for primal-variable regularization",
350  double get_xreg() const;)
351  DEPRECATED("Use get_preg for primal-variable regularization",
352  double get_ureg() const;)
353 
357  double get_steplength() const;
358 
362  double get_th_acceptstep() const;
363 
367  double get_th_stop() const;
368 
372  double get_th_gaptol() const;
373 
378  FeasibilityNorm get_feasnorm() const;
379 
383  std::size_t get_iter() const;
384 
388  void set_xs(const std::vector<Eigen::VectorXd>& xs);
389 
393  void set_us(const std::vector<Eigen::VectorXd>& us);
394 
398  void set_preg(const double preg);
399 
403  void set_dreg(const double dreg);
404 
405  DEPRECATED("Use set_preg for primal-variable regularization",
406  void set_xreg(const double xreg);)
407  DEPRECATED("Use set_preg for primal-variable regularization",
408  void set_ureg(const double ureg);)
409 
413  void set_th_acceptstep(const double th_acceptstep);
414 
418  void set_th_stop(const double th_stop);
419 
423  void set_th_gaptol(const double th_gaptol);
424 
429  void set_feasnorm(const FeasibilityNorm feas_norm);
430 
431  protected:
432  boost::shared_ptr<ShootingProblem> problem_;
433  std::vector<Eigen::VectorXd> xs_;
434  std::vector<Eigen::VectorXd> us_;
435  std::vector<Eigen::VectorXd> fs_;
436  std::vector<boost::shared_ptr<CallbackAbstract> >
441  double cost_;
442  double merit_;
443  double stop_;
444  Eigen::Vector2d d_;
445  double dV_;
446  double dPhi_;
447  double dVexp_;
448  double dPhiexp_;
449  double dfeas_;
450  double feas_;
451  double
453  double gfeas_;
455  double hfeas_;
457  double ffeas_try_;
459  double gfeas_try_;
461  double hfeas_try_;
463  double preg_;
464  double dreg_;
465  DEPRECATED("Use preg_ for primal-variable regularization",
466  double xreg_;)
467  DEPRECATED("Use dreg_ for primal-variable regularization",
468  double ureg_;)
469  double steplength_;
470  double th_acceptstep_;
471  double th_stop_;
472  double th_gaptol_;
473  enum FeasibilityNorm feasnorm_;
475  std::size_t iter_;
476  double tmp_feas_;
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:61
double get_cost() const
Return the cost for the current guess.
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()
boost::shared_ptr< ShootingProblem > problem_
optimal control problem
const std::vector< boost::shared_ptr< CallbackAbstract > > & getCallbacks() const
Return the list of callback functions using for diagnostic.
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.
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()
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:79
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.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SolverAbstract(boost::shared_ptr< ShootingProblem > problem)
Initialize the solver.
Definition: solver-base.cpp:19
void setCallbacks(const std::vector< boost::shared_ptr< CallbackAbstract > > &callbacks)
Set a list of callback functions using for the solver diagnostic.
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
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.
const boost::shared_ptr< ShootingProblem > & get_problem() const
Return the shooting problem.
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:68
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.
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.
std::vector< boost::shared_ptr< CallbackAbstract > > callbacks_
Callback functions.
double computeEqualityFeasibility()
Compute the feasibility of the equality constraints for the current guess.