crocoddyl  1.9.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
solver-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2021, LAAS-CNRS, University of Edinburgh
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_SOLVER_BASE_HPP_
10 #define CROCODDYL_CORE_SOLVER_BASE_HPP_
11 
12 #include <vector>
13 
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 
52  public:
53  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
54 
60  explicit SolverAbstract(boost::shared_ptr<ShootingProblem> problem);
61  virtual ~SolverAbstract();
62 
79  virtual bool solve(const std::vector<Eigen::VectorXd>& init_xs = DEFAULT_VECTOR,
80  const std::vector<Eigen::VectorXd>& init_us = DEFAULT_VECTOR, const std::size_t maxiter = 100,
81  const bool is_feasible = false, const double reg_init = 1e-9) = 0;
82 
94  virtual void computeDirection(const bool recalc) = 0;
95 
106  virtual double tryStep(const double steplength = 1) = 0;
107 
115  virtual double stoppingCriteria() = 0;
116 
123  virtual const Eigen::Vector2d& expectedImprovement() = 0;
124 
131  virtual void resizeData();
132 
142  double computeDynamicFeasibility();
143 
156  void setCandidate(const std::vector<Eigen::VectorXd>& xs_warm = DEFAULT_VECTOR,
157  const std::vector<Eigen::VectorXd>& us_warm = DEFAULT_VECTOR, const bool is_feasible = false);
158 
167  void setCallbacks(const std::vector<boost::shared_ptr<CallbackAbstract> >& callbacks);
168 
172  const std::vector<boost::shared_ptr<CallbackAbstract> >& getCallbacks() const;
173 
177  const boost::shared_ptr<ShootingProblem>& get_problem() const;
178 
182  const std::vector<Eigen::VectorXd>& get_xs() const;
183 
187  const std::vector<Eigen::VectorXd>& get_us() const;
188 
192  const std::vector<Eigen::VectorXd>& get_fs() const;
193 
197  bool get_is_feasible() const;
198 
202  double get_cost() const;
203 
207  double get_stop() const;
208 
212  const Eigen::Vector2d& get_d() const;
213 
217  double get_xreg() const;
218 
222  double get_ureg() const;
223 
227  double get_steplength() const;
228 
232  double get_dV() const;
233 
237  double get_dVexp() const;
238 
242  double get_th_acceptstep() const;
243 
247  double get_th_stop() const;
248 
252  std::size_t get_iter() const;
253 
257  double get_th_gaptol() const;
258 
263  double get_ffeas() const;
264 
268  bool get_inffeas() const;
269 
273  void set_xs(const std::vector<Eigen::VectorXd>& xs);
274 
278  void set_us(const std::vector<Eigen::VectorXd>& us);
279 
283  void set_xreg(const double xreg);
284 
288  void set_ureg(const double ureg);
289 
293  void set_th_acceptstep(const double th_acceptstep);
294 
298  void set_th_stop(const double th_stop);
299 
303  void set_th_gaptol(const double th_gaptol);
304 
308  void set_inffeas(const bool inffeas);
309 
310  protected:
311  boost::shared_ptr<ShootingProblem> problem_;
312  std::vector<Eigen::VectorXd> xs_;
313  std::vector<Eigen::VectorXd> us_;
314  std::vector<Eigen::VectorXd> fs_;
315  std::vector<boost::shared_ptr<CallbackAbstract> > callbacks_;
318  double cost_;
319  double stop_;
320  Eigen::Vector2d d_;
321  double xreg_;
322  double ureg_;
323  double steplength_;
324  double dV_;
325  double dVexp_;
326  double th_acceptstep_;
327  double th_stop_;
328  std::size_t iter_;
329  double th_gaptol_;
330  double ffeas_;
331  bool inffeas_;
332  double tmp_feas_;
334 };
335 
343  public:
348  virtual ~CallbackAbstract() {}
349 
355  virtual void operator()(SolverAbstract& solver) = 0;
356 };
357 
358 bool raiseIfNaN(const double value);
359 
360 } // namespace crocoddyl
361 
362 #endif // CROCODDYL_CORE_SOLVER_BASE_HPP_
crocoddyl::SolverAbstract::th_acceptstep_
double th_acceptstep_
Threshold used for accepting step.
Definition: solver-base.hpp:326
crocoddyl::SolverAbstract::get_d
const Eigen::Vector2d & get_d() const
Return the LQ approximation of the expected improvement.
Definition: solver-base.cpp:180
crocoddyl::SolverAbstract::SolverAbstract
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SolverAbstract(boost::shared_ptr< ShootingProblem > problem)
Initialize the solver.
Definition: solver-base.cpp:18
crocoddyl::CallbackAbstract
Abstract class for solver callbacks.
Definition: solver-base.hpp:342
crocoddyl::SolverAbstract::get_steplength
double get_steplength() const
Return the step length .
Definition: solver-base.cpp:186
crocoddyl::SolverAbstract
Abstract class for optimal control solvers.
Definition: solver-base.hpp:51
crocoddyl::SolverAbstract::computeDynamicFeasibility
double computeDynamicFeasibility()
Compute the dynamic feasibility for the current guess .
Definition: solver-base.cpp:66
crocoddyl::SolverAbstract::set_th_acceptstep
void set_th_acceptstep(const double th_acceptstep)
Modify the threshold used for accepting step.
Definition: solver-base.cpp:263
crocoddyl::SolverAbstract::th_gaptol_
double th_gaptol_
Threshold limit to check non-zero gaps.
Definition: solver-base.hpp:329
crocoddyl::SolverAbstract::get_th_gaptol
double get_th_gaptol() const
Return the threshold for accepting a gap as non-zero.
Definition: solver-base.cpp:198
crocoddyl::SolverAbstract::is_feasible_
bool is_feasible_
Label that indicates is the iteration is feasible.
Definition: solver-base.hpp:316
crocoddyl::SolverAbstract::d_
Eigen::Vector2d d_
LQ approximation of the expected improvement.
Definition: solver-base.hpp:320
crocoddyl::SolverAbstract::inffeas_
bool inffeas_
Definition: solver-base.hpp:331
crocoddyl::SolverAbstract::set_th_gaptol
void set_th_gaptol(const double th_gaptol)
Modify the threshold for accepting a gap as non-zero.
Definition: solver-base.cpp:279
crocoddyl::SolverAbstract::get_cost
double get_cost() const
Return the total cost.
Definition: solver-base.cpp:176
crocoddyl::CallbackAbstract::operator()
virtual void operator()(SolverAbstract &solver)=0
Run the callback function given a solver.
crocoddyl::CallbackAbstract::CallbackAbstract
CallbackAbstract()
Initialize the callback function.
Definition: solver-base.hpp:347
crocoddyl::SolverAbstract::resizeData
virtual void resizeData()
Resizing the solver data.
Definition: solver-base.cpp:56
crocoddyl::SolverAbstract::iter_
std::size_t iter_
Number of iteration performed by the solver.
Definition: solver-base.hpp:328
crocoddyl::SolverAbstract::set_xs
void set_xs(const std::vector< Eigen::VectorXd > &xs)
Modify the state trajectory .
Definition: solver-base.cpp:204
crocoddyl::SolverAbstract::ureg_
double ureg_
Current control regularization values.
Definition: solver-base.hpp:322
crocoddyl::SolverAbstract::stop_
double stop_
Value computed by stoppingCriteria()
Definition: solver-base.hpp:319
crocoddyl::SolverAbstract::getCallbacks
const std::vector< boost::shared_ptr< CallbackAbstract > > & getCallbacks() const
Return the list of callback functions using for diagnostic.
Definition: solver-base.cpp:164
crocoddyl::SolverAbstract::set_th_stop
void set_th_stop(const double th_stop)
Modify the tolerance for stopping the algorithm.
Definition: solver-base.cpp:271
crocoddyl::SolverAbstract::get_is_feasible
bool get_is_feasible() const
Return the feasibility status of the trajectory.
Definition: solver-base.cpp:174
crocoddyl::SolverAbstract::stoppingCriteria
virtual double stoppingCriteria()=0
Return a positive value that quantifies the algorithm termination.
crocoddyl::SolverAbstract::get_dV
double get_dV() const
Return the cost reduction .
Definition: solver-base.cpp:188
crocoddyl::SolverAbstract::steplength_
double steplength_
Current applied step-length.
Definition: solver-base.hpp:323
crocoddyl::SolverAbstract::setCandidate
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 .
Definition: solver-base.cpp:103
crocoddyl::SolverAbstract::th_stop_
double th_stop_
Tolerance for stopping the algorithm.
Definition: solver-base.hpp:327
crocoddyl::SolverAbstract::computeDirection
virtual void computeDirection(const bool recalc)=0
Compute the search direction for the current guess .
crocoddyl::SolverAbstract::get_xreg
double get_xreg() const
Return the state regularization value.
Definition: solver-base.cpp:182
crocoddyl::SolverAbstract::get_inffeas
bool get_inffeas() const
Return the norm used for the computing the feasibility (true for , false for )
Definition: solver-base.cpp:202
crocoddyl::SolverAbstract::tmp_feas_
double tmp_feas_
Temporal variables used for computed the feasibility.
Definition: solver-base.hpp:333
crocoddyl::SolverAbstract::tryStep
virtual double tryStep(const double steplength=1)=0
Try a predefined step length and compute its cost improvement .
crocoddyl::SolverAbstract::ffeas_
double ffeas_
Feasibility of the dynamic constraints.
Definition: solver-base.hpp:330
crocoddyl::SolverAbstract::problem_
boost::shared_ptr< ShootingProblem > problem_
optimal control problem
Definition: solver-base.hpp:311
crocoddyl::SolverAbstract::get_th_acceptstep
double get_th_acceptstep() const
Return the threshold used for accepting a step.
Definition: solver-base.cpp:192
crocoddyl::SolverAbstract::callbacks_
std::vector< boost::shared_ptr< CallbackAbstract > > callbacks_
Callback functions.
Definition: solver-base.hpp:315
crocoddyl::SolverAbstract::setCallbacks
void setCallbacks(const std::vector< boost::shared_ptr< CallbackAbstract > > &callbacks)
Set a list of callback functions using for the solver diagnostic.
Definition: solver-base.cpp:160
crocoddyl::SolverAbstract::get_xs
const std::vector< Eigen::VectorXd > & get_xs() const
Return the state trajectory .
Definition: solver-base.cpp:168
crocoddyl::SolverAbstract::xreg_
double xreg_
Current state regularization value.
Definition: solver-base.hpp:321
crocoddyl::SolverAbstract::get_ureg
double get_ureg() const
Return the control regularization value.
Definition: solver-base.cpp:184
crocoddyl::SolverAbstract::dVexp_
double dVexp_
Expected cost reduction.
Definition: solver-base.hpp:325
crocoddyl::SolverAbstract::get_fs
const std::vector< Eigen::VectorXd > & get_fs() const
Return the gaps .
Definition: solver-base.cpp:172
crocoddyl::SolverAbstract::solve
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=1e-9)=0
Compute the optimal trajectory as lists of and terms.
crocoddyl::SolverAbstract::us_
std::vector< Eigen::VectorXd > us_
Control trajectory.
Definition: solver-base.hpp:313
crocoddyl::SolverAbstract::get_dVexp
double get_dVexp() const
Return the expected cost reduction .
Definition: solver-base.cpp:190
crocoddyl::SolverAbstract::get_iter
std::size_t get_iter() const
Return the number of iterations performed by the solver.
Definition: solver-base.cpp:196
crocoddyl::SolverAbstract::fs_
std::vector< Eigen::VectorXd > fs_
Gaps/defects between shooting nodes.
Definition: solver-base.hpp:314
crocoddyl::SolverAbstract::cost_
double cost_
Total cost.
Definition: solver-base.hpp:318
crocoddyl::SolverAbstract::expectedImprovement
virtual const Eigen::Vector2d & expectedImprovement()=0
Return the expected improvement from a given current search direction .
crocoddyl::SolverAbstract::xs_
std::vector< Eigen::VectorXd > xs_
State trajectory.
Definition: solver-base.hpp:312
crocoddyl::SolverAbstract::set_xreg
void set_xreg(const double xreg)
Modify the state regularization value.
Definition: solver-base.cpp:247
crocoddyl::SolverAbstract::get_th_stop
double get_th_stop() const
Return the tolerance for stopping the algorithm.
Definition: solver-base.cpp:194
crocoddyl::SolverAbstract::get_stop
double get_stop() const
Return the value computed by stoppingCriteria()
Definition: solver-base.cpp:178
crocoddyl::SolverAbstract::get_problem
const boost::shared_ptr< ShootingProblem > & get_problem() const
Return the shooting problem.
Definition: solver-base.cpp:166
crocoddyl::SolverAbstract::set_ureg
void set_ureg(const double ureg)
Modify the control regularization value.
Definition: solver-base.cpp:255
crocoddyl::SolverAbstract::dV_
double dV_
Cost reduction obtained by tryStep()
Definition: solver-base.hpp:324
crocoddyl::SolverAbstract::set_inffeas
void set_inffeas(const bool inffeas)
Modify the current norm used for computed the feasibility.
Definition: solver-base.cpp:287
crocoddyl::SolverAbstract::get_us
const std::vector< Eigen::VectorXd > & get_us() const
Return the control trajectory .
Definition: solver-base.cpp:170
crocoddyl::SolverAbstract::was_feasible_
bool was_feasible_
Label that indicates in the previous iterate was feasible.
Definition: solver-base.hpp:317
crocoddyl::SolverAbstract::get_ffeas
double get_ffeas() const
Return the feasibility of the dynamic constraints of the current guess.
Definition: solver-base.cpp:200
crocoddyl::SolverAbstract::set_us
void set_us(const std::vector< Eigen::VectorXd > &us)
Modify the control trajectory .
Definition: solver-base.cpp:227