Crocoddyl
Loading...
Searching...
No Matches
solver-base.hpp
1
2// 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
17namespace crocoddyl {
18
19template <typename Scalar>
21 static const std::vector<Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> value;
22};
23
24template <typename Scalar>
25const std::vector<Eigen::Matrix<Scalar, Eigen::Dynamic, 1>>
26 DefaultVector<Scalar>::value = {};
27
28#define DEFAULT_VECTOR DefaultVector<double>::value
29
30enum FeasibilityNorm { LInf = 0, L1 = 1, L2 = 2 };
31
32class SolverBase {
33 public:
34 virtual ~SolverBase() = default;
35
36 CROCODDYL_BASE_CAST(SolverBase, SolverAbstractTpl)
37};
38
75template <typename _Scalar>
77 public:
78 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
79
80 typedef _Scalar Scalar;
81 typedef ShootingProblemTpl<Scalar> ShootingProblem;
82 typedef typename ShootingProblem::ActionModelAbstract ActionModelAbstract;
83 typedef typename ShootingProblem::ActionDataAbstract ActionDataAbstract;
84 typedef CallbackAbstractTpl<Scalar> CallbackAbstract;
85 typedef MathBaseTpl<Scalar> MathBase;
86 typedef typename MathBase::VectorXs VectorXs;
87 typedef typename MathBase::Vector3s Vector3s;
88
94 explicit SolverAbstractTpl(std::shared_ptr<ShootingProblem> problem);
95 virtual ~SolverAbstractTpl() = default;
96
117 virtual bool solve(
118 const std::vector<VectorXs>& init_xs = DefaultVector<Scalar>::value,
119 const std::vector<VectorXs>& init_us = DefaultVector<Scalar>::value,
120 const std::size_t maxiter = 100, const bool is_feasible = false,
121 const Scalar reg_init = std::numeric_limits<Scalar>::quiet_NaN());
122
142 virtual void computeDirection(const bool recalc) = 0;
143
158 virtual Scalar tryStep(const Scalar steplength = Scalar(1.));
159
171 virtual void computeCandidate(const Scalar steplength = Scalar(1.));
172
181 virtual Scalar stoppingCriteria() = 0;
182
192 virtual Vector3s expectedImprovement() = 0;
193
202
212
216 virtual void updateMeritFunction();
217
223 virtual bool checkAcceptance();
224
236 virtual void calcDir();
237
256 virtual void setCandidate(
257 const std::vector<VectorXs>& xs_warm = DefaultVector<Scalar>::value,
258 const std::vector<VectorXs>& us_warm = DefaultVector<Scalar>::value,
259 const bool is_feasible = false);
260
264 virtual void updateCandidate();
265
270
275
280 virtual void increaseRegularization();
281
286 virtual void decreaseRegularization();
287
294 virtual void resizeData();
295
309 Scalar computeFeasibility(const std::vector<VectorXs>& fs);
310
324
339 Scalar computeStateFeasibility(const std::vector<VectorXs>& xs);
340
355 Scalar computeControlFeasibility(const std::vector<VectorXs>& us);
356
367
378
388 const std::vector<std::shared_ptr<CallbackAbstract>>& callbacks);
389
393 const std::vector<std::shared_ptr<CallbackAbstract>>& getCallbacks() const;
394
398 const std::shared_ptr<ShootingProblem>& get_problem() const;
399
403 const std::vector<Scalar>& get_alphas() const;
404
408 const std::vector<VectorXs>& get_xs() const;
409
413 const std::vector<VectorXs>& get_us() const;
414
418 const std::vector<VectorXs>& get_fs() const;
419
423 const std::vector<VectorXs>& get_dxs() const;
424
428 const std::vector<VectorXs>& get_dus() const;
429
433 const std::vector<VectorXs>& get_xs_try() const;
434
438 const std::vector<VectorXs>& get_us_try() const;
439
443 const std::vector<VectorXs>& get_fs_try() const;
444
449 bool get_is_feasible() const;
450
454 Scalar get_cost() const;
455
459 Scalar get_merit() const;
460
464 Scalar get_stop() const;
465
470 const Vector3s& get_DV() const;
471
475 Scalar get_dV() const;
476
480 Scalar get_dPhi() const;
481
486 Scalar get_dVexp() const;
487
492 Scalar get_dPhiexp() const;
493
497 Scalar get_dfeas() const;
498
502 Scalar get_feas() const;
503
507 Scalar get_ffeas() const;
508
512 Scalar get_gfeas() const;
513
517 Scalar get_hfeas() const;
518
522 Scalar get_ffeas_try() const;
523
527 Scalar get_gfeas_try() const;
528
532 Scalar get_hfeas_try() const;
533
537 Scalar get_preg() const;
538
542 Scalar get_dreg() const;
543
544 DEPRECATED(
545 "Use get_preg for primal-variable regularization",
546 Scalar get_xreg() const { return preg_; })
547 DEPRECATED(
548 "Use get_preg for primal-variable regularization",
549 Scalar get_ureg() const { return preg_; })
550
554 Scalar get_reg_min() const;
555
559 Scalar get_reg_max() const;
560
564 Scalar get_steplength() const;
565
569 Scalar get_th_acceptstep() const;
570
574 Scalar get_th_stop() const;
575
580 "Do not use this threshold. It is not needed by our solvers",
581 Scalar get_th_gaptol() const { return th_gaptol_; })
582
587 FeasibilityNorm get_feasnorm() const;
588
592 std::size_t get_iter() const;
593
597 void set_alphas(const std::vector<Scalar>& alphas);
598
602 void set_xs(const std::vector<VectorXs>& xs);
603
607 void set_us(const std::vector<VectorXs>& us);
608
612 void set_preg(const Scalar preg);
613
617 void set_dreg(const Scalar dreg);
618
622 void set_reg_min(const Scalar regmin);
623
627 void set_reg_max(const Scalar regmax);
628
629 DEPRECATED(
630 "Use set_preg for primal-variable regularization",
631 void set_xreg(const Scalar xreg) {
632 if (xreg < Scalar(0.)) {
633 throw_pretty(
634 "Invalid argument: " << "xreg value has to be positive.");
635 }
636 xreg_ = xreg;
637 preg_ = xreg;
638 })
639 DEPRECATED(
640 "Use set_preg for primal-variable regularization",
641 void set_ureg(const Scalar ureg) {
642 if (ureg < Scalar(0.)) {
643 throw_pretty(
644 "Invalid argument: " << "ureg value has to be positive.");
645 }
646 ureg_ = ureg;
647 preg_ = ureg;
648 })
649
653 void set_th_acceptstep(const Scalar th_acceptstep);
654
658 void set_th_stop(const Scalar th_stop);
659
663 DEPRECATED("Do not use this threshold. It is not needed by our solvers",
664 void set_th_gaptol(const Scalar th_gaptol);)
665
670 void set_feasnorm(const FeasibilityNorm feas_norm);
671
672 protected:
677
686 virtual void resizeRunningData();
687
696 virtual void resizeTerminalData();
697
698 std::shared_ptr<ShootingProblem> problem_;
699 std::vector<std::shared_ptr<CallbackAbstract>>
701 std::vector<Scalar>
704 Scalar th_stop_;
705 DEPRECATED("Do not use this threshold. It is not needed by our solvers",
706 Scalar th_gaptol_;)
707 enum FeasibilityNorm feasnorm_;
709
710 // allocate data
711 Scalar dImpr_;
713 std::vector<VectorXs> xs_;
714 std::vector<VectorXs> us_;
715 std::vector<VectorXs> fs_;
716 std::vector<VectorXs>
718 std::vector<VectorXs>
720 std::vector<VectorXs> fs_try_;
722 std::vector<VectorXs> dxs_;
723 std::vector<VectorXs> dus_;
727 Scalar cost_;
728 Scalar cost_try_;
729 Scalar merit_;
730 Scalar stop_;
731 std::size_t iter_;
732 Vector3s DV_;
733 Scalar dV_;
734 Scalar dPhi_;
737 Scalar dVexp_;
739 Scalar dPhiexp_;
741 Scalar dfeas_;
742 Scalar feas_;
743 Scalar
745 Scalar gfeas_;
747 Scalar hfeas_;
749 Scalar ffeas_try_;
751 Scalar gfeas_try_;
753 Scalar hfeas_try_;
755 Scalar tmp_feas_;
756 Scalar preg_;
757 Scalar dreg_;
758 DEPRECATED("Use preg_ for primal-variable regularization",
759 Scalar xreg_;)
760 DEPRECATED("Use dreg_ for primal-variable regularization",
761 Scalar ureg_;)
762 Scalar reg_min_;
763 Scalar reg_max_;
765 std::vector<VectorXs> g_adj_;
766 std::vector<VectorXs> x_adj_;
767 std::vector<VectorXs> u_adj_;
768 std::size_t nh_T_;
769 std::size_t ng_T_;
770
771 bool acceptstep_;
772 bool recalcdir_;
773
774 private:
775 SolverAbstractTpl() : problem_(nullptr) {}
776};
777
779 public:
780 virtual ~CallbackBase() = default;
781
782 CROCODDYL_BASE_CAST(CallbackBase, CallbackAbstractTpl)
783};
784
792template <typename _Scalar>
794 public:
795 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
796
797 typedef _Scalar Scalar;
798 typedef SolverAbstractTpl<Scalar> SolverAbstract;
799
804 virtual ~CallbackAbstractTpl() = default;
805
811 virtual void operator()(SolverAbstract& solver) = 0;
812};
813
814template <typename Scalar>
815bool raiseIfNaN(const Scalar value);
816
817} // namespace crocoddyl
818
819/* --- Details -------------------------------------------------------------- */
820/* --- Details -------------------------------------------------------------- */
821/* --- Details -------------------------------------------------------------- */
822#include "crocoddyl/core/solver-base.hxx"
823
824CROCODDYL_DISABLE_WARNING_DEPRECATED
825
826CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::SolverAbstractTpl)
827
828CROCODDYL_ENABLE_WARNING_DEPRECATED
829
830#endif // CROCODDYL_CORE_SOLVER_BASE_HPP_
Abstract class for solver callbacks.
CallbackAbstractTpl()
Initialize the callback function.
virtual void operator()(SolverAbstract &solver)=0
Run the callback function given a solver.
This class encapsulates a shooting problem.
Definition shooting.hpp:32
Abstract class for optimal control solvers.
Scalar get_dPhiexp() const
Return the expected reduction in the merit function .
Scalar get_hfeas_try() const
Return the equality feasibility for the current step length.
DEPRECATED("Do not use this threshold. It is not needed by our solvers", Scalar th_gaptol_;) enum FeasibilityNorm feasnorm_
virtual void resizeData()
Resizing the solver data.
virtual void updateMeritFunction()
Update the merit function value for the current guess.
virtual void computeCandidate(const Scalar steplength=Scalar(1.))
Compute new candidate solution using step length.
std::size_t get_iter() const
Return the number of iterations performed by the solver.
const std::vector< VectorXs > & get_dus() const
Return the feedforward gains .
void set_th_acceptstep(const Scalar th_acceptstep)
Modify the threshold used for accepting step.
Scalar get_ffeas_try() const
Return the dynamic feasibility for the current step length.
void set_dreg(const Scalar dreg)
Modify the dual-variable regularization value.
virtual Vector3s expectedImprovement()=0
Return the expected improvement from a given current search direction .
Scalar get_steplength() const
Return the step length .
const std::vector< VectorXs > & get_us() const
Return the control trajectory .
Scalar computeDynamicFeasibility()
Compute the dynamic feasibility for the current guess .
virtual void computeMeritFunctionImprovement()
Compute the merit function improvement for the current step.
void set_us(const std::vector< VectorXs > &us)
Modify the control trajectory .
void set_xs(const std::vector< VectorXs > &xs)
Modify the state trajectory .
void set_preg(const Scalar preg)
Modify the primal-variable regularization value.
virtual bool decreaseRegularizationCriteria()
Criteria used to decrease regularization.
Scalar get_reg_max() const
Return the maximum regularization value.
Scalar get_dV() const
Return the reduction in the cost function .
virtual void computeExpectedMeritFunctionImprovement()
Compute the expected merit function improvement for the current step.
virtual void resizeRunningData()
Resize data associated with the running models of the shooting problem.
Scalar get_gfeas_try() const
Return the inequality feasibility for the current step length.
std::shared_ptr< ShootingProblem > problem_
Scalar get_dVexp() const
Return the expected reduction in the cost function .
void allocateData()
Allocate all the basic data needed in solvers.
Scalar get_stop() const
Return the stopping-criteria value computed by stoppingCriteria().
virtual bool checkAcceptance()
Check if we should accept or not the step.
Scalar get_cost() const
Return the cost for the current guess.
Scalar get_gfeas() const
Return the inequality feasibility for the current guess.
virtual void resizeTerminalData()
Resize data associated with the terminal model of the shooting problem.
Scalar get_merit() const
Return the merit for the current guess.
const std::vector< std::shared_ptr< CallbackAbstract > > & getCallbacks() const
Return the list of callback functions using for diagnostic.
SolverAbstractTpl(std::shared_ptr< ShootingProblem > problem)
Initialize the solver.
Scalar get_preg() const
Return the primal-variable regularization.
const std::vector< VectorXs > & get_us_try() const
Return the trial control trajectory .
void set_reg_min(const Scalar regmin)
Modify the minimum regularization value.
void set_th_stop(const Scalar th_stop)
Modify the tolerance for stopping the algorithm.
const std::vector< Scalar > & get_alphas() const
Return the set of step lengths using by the line-search procedure.
virtual void computeDirection(const bool recalc)=0
Compute the search direction for the current guess .
Scalar get_reg_min() const
Return the minimum regularization value.
Scalar computeControlFeasibility(const std::vector< VectorXs > &us)
Compute the control box-constraint feasibility from a given control trajectory.
Scalar get_ffeas() const
Return the dynamic feasibility for the current guess.
const std::vector< VectorXs > & get_fs_try() const
Return the trail dynamic infeasibility .
DEPRECATED("Do not use this threshold. It is not needed by our solvers", Scalar get_th_gaptol() const { return th_gaptol_;}) FeasibilityNorm get_feasnorm() const
Return the threshold for accepting a gap as non-zero.
std::vector< std::shared_ptr< CallbackAbstract > > callbacks_
Scalar get_th_acceptstep() const
Return the threshold used for accepting a step.
Scalar get_dfeas() const
Return the reduction in the feasibility.
virtual void decreaseRegularization()
Decrease the state and control regularization values by a regfactor_ factor.
Scalar get_dreg() const
Return the dual-variable regularization.
virtual void calcDir()
Refresh the linearization of the optimal control problem around the current candidate.
Scalar computeEqualityFeasibility()
Compute the feasibility of the equality constraints for the current guess.
void setCallbacks(const std::vector< std::shared_ptr< CallbackAbstract > > &callbacks)
Set a list of callback functions using for the solver diagnostic.
Scalar get_hfeas() const
Return the equality feasibility for the current guess.
const std::vector< VectorXs > & get_xs_try() const
Return the trial state trajectory .
Scalar computeInequalityFeasibility()
Compute the feasibility of the inequality constraints for the current guess.
Scalar get_dPhi() const
Return the reduction in the merit function .
Scalar get_feas() const
Return the total feasibility for the current guess.
virtual bool increaseRegularizationCriteria()
Criteria used to increase regularization.
const Vector3s & get_DV() const
Return the constant, linear, and quadratic terms of the expected improvement.
const std::vector< VectorXs > & get_xs() const
Return the state trajectory .
bool get_is_feasible() const
Return the feasibility status of the trajectory.
const std::vector< VectorXs > & get_fs() const
Return the dynamic infeasibility .
virtual void setCandidate(const std::vector< VectorXs > &xs_warm=DefaultVector< Scalar >::value, const std::vector< VectorXs > &us_warm=DefaultVector< Scalar >::value, const bool is_feasible=false)
Set the solver candidate trajectories .
virtual bool solve(const std::vector< VectorXs > &init_xs=DefaultVector< Scalar >::value, const std::vector< VectorXs > &init_us=DefaultVector< Scalar >::value, const std::size_t maxiter=100, const bool is_feasible=false, const Scalar reg_init=std::numeric_limits< Scalar >::quiet_NaN())
Compute the optimal trajectory as lists of and terms.
virtual void increaseRegularization()
Increase the state and control regularization values by a regfactor_ factor.
void set_reg_max(const Scalar regmax)
Modify the maximum regularization value.
virtual void updateCandidate()
Update the candidate solution: cost, feasibilities, and merit value.
Scalar computeStateFeasibility(const std::vector< VectorXs > &xs)
Compute the state box-constraint feasibility from a given state trajectory.
Scalar computeFeasibility(const std::vector< VectorXs > &fs)
Compute the feasibility from a given residual vector.
Scalar get_th_stop() const
Return the tolerance for stopping the algorithm.
DEPRECATED("Do not use this threshold. It is not needed by our solvers", void set_th_gaptol(const Scalar th_gaptol);) void set_feasnorm(const FeasibilityNorm feas_norm)
Modify the threshold for accepting a gap as non-zero.
const std::vector< VectorXs > & get_dxs() const
Return the linear update in .
const std::shared_ptr< ShootingProblem > & get_problem() const
Return the shooting problem.
void set_alphas(const std::vector< Scalar > &alphas)
Modify the set of step lengths using by the line-search procedure.
virtual Scalar tryStep(const Scalar steplength=Scalar(1.))
Try a predefined step length and compute its cost improvement and merit improvement .
virtual Scalar stoppingCriteria()=0
Return a positive value that quantifies the algorithm termination.