Exotica
feasibility_driven_ddp_solver.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019-2020, LAAS-CNRS, University of Edinburgh, University of Oxford
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef EXOTICA_DDP_SOLVER_FEASIBILITY_DRIVEN_DDP_SOLVER_H_
31 #define EXOTICA_DDP_SOLVER_FEASIBILITY_DRIVEN_DDP_SOLVER_H_
32 
34 #include <exotica_ddp_solver/feasibility_driven_ddp_solver_initializer.h>
35 
36 namespace exotica
37 {
38 // Feasibility-driven DDP solver
39 // Based on the implementation in Crocoddyl: https://github.com/loco-3d/crocoddyl.git
40 // Cf. https://arxiv.org/abs/1909.04947
42 {
43 public:
44  void Solve(Eigen::MatrixXd& solution) override;
45  void SpecifyProblem(PlanningProblemPtr pointer) override;
46 
47  const std::vector<Eigen::VectorXd>& get_fs() const { return fs_; };
48  const std::vector<Eigen::VectorXd>& get_xs() const { return xs_; };
49  const std::vector<Eigen::VectorXd>& get_us() const { return us_; };
50 
51 protected:
52  int NDX_;
53  int last_T_ = -1;
54 
55  void IncreaseRegularization() override;
56  void DecreaseRegularization() override;
57  const Eigen::Vector2d& ExpectedImprovement();
59  inline bool IsNaN(const double value)
60  {
61  if (std::isnan(value) || std::isinf(value) || value >= 1e30)
62  {
63  return true;
64  }
65  else
66  {
67  return false;
68  }
69  }
70  void SetCandidate(const std::vector<Eigen::VectorXd>& xs_warm, const std::vector<Eigen::VectorXd>& us_warm, const bool is_feasible);
71  double CheckStoppingCriteria();
72 
73  double CalcDiff();
74  bool ComputeDirection(const bool recalcDiff);
75  virtual bool BackwardPassFDDP();
76  void BackwardPass() override { return (void)BackwardPassFDDP(); }
77  virtual void ComputeGains(const int t);
78  void ForwardPass(const double steplength);
79  double TryStep(const double steplength);
80 
81  virtual void AllocateData();
82 
83  Eigen::MatrixXd control_limits_;
84  double initial_regularization_rate_ = 1e-9; // Set from parameters on Instantiate
85  bool clamp_to_control_limits_in_forward_pass_ = false; // Set from parameters on Instantiate
86 
87  double steplength_;
88  Eigen::Vector2d d_;
89  double dV_;
90  double dVexp_;
91  double th_acceptstep_ = 0.1;
92  double th_stop_ = 1e-9;
93  double th_gradient_tolerance_ = 0.;
94  double stop_;
95 
96  double dg_ = 0.;
97  double dq_ = 0.;
98  double dv_ = 0.;
99  double th_acceptnegstep_ = 2.;
100  std::vector<Eigen::VectorXd> us_;
101  std::vector<Eigen::VectorXd> xs_;
102  bool is_feasible_ = false;
103  double xreg_ = 1e-9;
104  double ureg_ = 1e-9;
105  double regmin_ = 1e-9;
106  double regmax_ = 1e9;
107  double regfactor_ = 10.;
108 
109  std::vector<Eigen::VectorXd> xs_try_;
110  std::vector<Eigen::VectorXd> us_try_;
111  std::vector<Eigen::VectorXd> dx_;
112 
113  // allocate data
114  std::vector<Eigen::VectorXd> fs_;
115 
116  Eigen::VectorXd xnext_;
117  Eigen::MatrixXd FxTVxx_p_;
118  std::vector<Eigen::MatrixXd> FuTVxx_p_;
119  std::vector<Eigen::MatrixXd> Qxu_;
120  Eigen::VectorXd fTVxx_p_;
121  std::vector<Eigen::LDLT<Eigen::MatrixXd> > Quu_ldlt_;
122  std::vector<Eigen::VectorXd> Quuk_;
123  double th_grad_ = 1e-12;
124  double th_stepdec_ = 0.5;
125  double th_stepinc_ = 0.01;
126  bool was_feasible_ = false;
127 };
128 
129 class FeasibilityDrivenDDPSolver : public AbstractFeasibilityDrivenDDPSolver, public Instantiable<FeasibilityDrivenDDPSolverInitializer>
130 {
131 public:
132  void Instantiate(const FeasibilityDrivenDDPSolverInitializer& init) override;
133 };
134 
135 } // namespace exotica
136 
137 #endif // EXOTICA_DDP_SOLVER_FEASIBILITY_DRIVEN_DDP_SOLVER_H_
exotica::AbstractFeasibilityDrivenDDPSolver::clamp_to_control_limits_in_forward_pass_
bool clamp_to_control_limits_in_forward_pass_
Definition: feasibility_driven_ddp_solver.h:85
exotica::FeasibilityDrivenDDPSolver
Definition: feasibility_driven_ddp_solver.h:129
exotica::AbstractFeasibilityDrivenDDPSolver::CalcDiff
double CalcDiff()
exotica::AbstractFeasibilityDrivenDDPSolver::us_try_
std::vector< Eigen::VectorXd > us_try_
Control trajectory computed by line-search procedure.
Definition: feasibility_driven_ddp_solver.h:110
exotica::AbstractFeasibilityDrivenDDPSolver::Solve
void Solve(Eigen::MatrixXd &solution) override
Solves the problem.
exotica::AbstractFeasibilityDrivenDDPSolver::ExpectedImprovement
const Eigen::Vector2d & ExpectedImprovement()
exotica::AbstractFeasibilityDrivenDDPSolver::ComputeGains
virtual void ComputeGains(const int t)
exotica::AbstractFeasibilityDrivenDDPSolver::fTVxx_p_
Eigen::VectorXd fTVxx_p_
Definition: feasibility_driven_ddp_solver.h:120
exotica::AbstractFeasibilityDrivenDDPSolver::ForwardPass
void ForwardPass(const double steplength)
exotica::AbstractFeasibilityDrivenDDPSolver::BackwardPassFDDP
virtual bool BackwardPassFDDP()
exotica::AbstractFeasibilityDrivenDDPSolver::fs_
std::vector< Eigen::VectorXd > fs_
Gaps/defects between shooting nodes.
Definition: feasibility_driven_ddp_solver.h:114
exotica::AbstractFeasibilityDrivenDDPSolver::NDX_
int NDX_
Definition: feasibility_driven_ddp_solver.h:49
exotica::FeasibilityDrivenDDPSolver::Instantiate
void Instantiate(const FeasibilityDrivenDDPSolverInitializer &init) override
exotica::AbstractFeasibilityDrivenDDPSolver::th_stepinc_
double th_stepinc_
Step-length threshold used to increase regularization.
Definition: feasibility_driven_ddp_solver.h:125
exotica::AbstractFeasibilityDrivenDDPSolver
Definition: feasibility_driven_ddp_solver.h:41
exotica::Instantiable
Definition: property.h:110
exotica::AbstractFeasibilityDrivenDDPSolver::steplength_
double steplength_
Current applied step-length.
Definition: feasibility_driven_ddp_solver.h:87
exotica::AbstractFeasibilityDrivenDDPSolver::xs_try_
std::vector< Eigen::VectorXd > xs_try_
State trajectory computed by line-search procedure.
Definition: feasibility_driven_ddp_solver.h:109
exotica
Definition: cartpole_dynamics_solver.h:38
exotica::AbstractFeasibilityDrivenDDPSolver::get_us
const std::vector< Eigen::VectorXd > & get_us() const
Definition: feasibility_driven_ddp_solver.h:49
exotica::AbstractFeasibilityDrivenDDPSolver::is_feasible_
bool is_feasible_
Definition: feasibility_driven_ddp_solver.h:102
exotica::AbstractFeasibilityDrivenDDPSolver::ureg_
double ureg_
Control regularization.
Definition: feasibility_driven_ddp_solver.h:104
exotica::AbstractFeasibilityDrivenDDPSolver::regmax_
double regmax_
Maximum regularization (to exit by divergence)
Definition: feasibility_driven_ddp_solver.h:106
exotica::AbstractFeasibilityDrivenDDPSolver::th_acceptstep_
double th_acceptstep_
Threshold used for accepting step.
Definition: feasibility_driven_ddp_solver.h:91
exotica::AbstractFeasibilityDrivenDDPSolver::dg_
double dg_
Definition: feasibility_driven_ddp_solver.h:96
exotica::AbstractFeasibilityDrivenDDPSolver::UpdateExpectedImprovement
void UpdateExpectedImprovement()
exotica::AbstractFeasibilityDrivenDDPSolver::dq_
double dq_
Definition: feasibility_driven_ddp_solver.h:97
exotica::AbstractFeasibilityDrivenDDPSolver::initial_regularization_rate_
double initial_regularization_rate_
Definition: feasibility_driven_ddp_solver.h:84
exotica::AbstractFeasibilityDrivenDDPSolver::xreg_
double xreg_
State regularization.
Definition: feasibility_driven_ddp_solver.h:103
exotica::AbstractFeasibilityDrivenDDPSolver::th_stop_
double th_stop_
Tolerance for stopping the algorithm.
Definition: feasibility_driven_ddp_solver.h:92
exotica::AbstractFeasibilityDrivenDDPSolver::dV_
double dV_
Cost reduction obtained by TryStep.
Definition: feasibility_driven_ddp_solver.h:89
exotica::AbstractDDPSolver
Definition: abstract_ddp_solver.h:43
exotica::AbstractFeasibilityDrivenDDPSolver::dVexp_
double dVexp_
Expected cost reduction.
Definition: feasibility_driven_ddp_solver.h:90
exotica::AbstractFeasibilityDrivenDDPSolver::regfactor_
double regfactor_
Factor by which the regularization gets increased/decreased.
Definition: feasibility_driven_ddp_solver.h:107
exotica::AbstractFeasibilityDrivenDDPSolver::last_T_
int last_T_
Definition: feasibility_driven_ddp_solver.h:53
exotica::AbstractFeasibilityDrivenDDPSolver::d_
Eigen::Vector2d d_
LQ approximation of the expected improvement.
Definition: feasibility_driven_ddp_solver.h:88
abstract_ddp_solver.h
exotica::AbstractFeasibilityDrivenDDPSolver::dv_
double dv_
Definition: feasibility_driven_ddp_solver.h:98
exotica::AbstractFeasibilityDrivenDDPSolver::Quuk_
std::vector< Eigen::VectorXd > Quuk_
Definition: feasibility_driven_ddp_solver.h:122
exotica::AbstractFeasibilityDrivenDDPSolver::SpecifyProblem
void SpecifyProblem(PlanningProblemPtr pointer) override
Binds the solver to a specific problem which must be pre-initalised.
exotica::AbstractFeasibilityDrivenDDPSolver::control_limits_
Eigen::MatrixXd control_limits_
Definition: feasibility_driven_ddp_solver.h:83
exotica::AbstractFeasibilityDrivenDDPSolver::xnext_
Eigen::VectorXd xnext_
Definition: feasibility_driven_ddp_solver.h:116
exotica::AbstractFeasibilityDrivenDDPSolver::Qxu_
std::vector< Eigen::MatrixXd > Qxu_
Definition: feasibility_driven_ddp_solver.h:119
exotica::AbstractFeasibilityDrivenDDPSolver::CheckStoppingCriteria
double CheckStoppingCriteria()
exotica::AbstractFeasibilityDrivenDDPSolver::xs_
std::vector< Eigen::VectorXd > xs_
Definition: feasibility_driven_ddp_solver.h:101
exotica::AbstractFeasibilityDrivenDDPSolver::DecreaseRegularization
void DecreaseRegularization() override
exotica::AbstractFeasibilityDrivenDDPSolver::was_feasible_
bool was_feasible_
Label that indicates in the previous iterate was feasible.
Definition: feasibility_driven_ddp_solver.h:126
exotica::AbstractFeasibilityDrivenDDPSolver::stop_
double stop_
Value computed by CheckStoppingCriteria.
Definition: feasibility_driven_ddp_solver.h:94
exotica::AbstractFeasibilityDrivenDDPSolver::IsNaN
bool IsNaN(const double value)
Definition: feasibility_driven_ddp_solver.h:59
exotica::PlanningProblemPtr
std::shared_ptr< PlanningProblem > PlanningProblemPtr
Definition: planning_problem.h:116
exotica::AbstractFeasibilityDrivenDDPSolver::AllocateData
virtual void AllocateData()
exotica::AbstractFeasibilityDrivenDDPSolver::th_grad_
double th_grad_
Tolerance of the expected gradient used for testing the step.
Definition: feasibility_driven_ddp_solver.h:123
exotica::AbstractFeasibilityDrivenDDPSolver::SetCandidate
void SetCandidate(const std::vector< Eigen::VectorXd > &xs_warm, const std::vector< Eigen::VectorXd > &us_warm, const bool is_feasible)
exotica::AbstractFeasibilityDrivenDDPSolver::th_gradient_tolerance_
double th_gradient_tolerance_
Gradient tolerance.
Definition: feasibility_driven_ddp_solver.h:93
exotica::AbstractFeasibilityDrivenDDPSolver::BackwardPass
void BackwardPass() override
Computes the control gains for a the trajectory in the associated DynamicTimeIndexedProblem.
Definition: feasibility_driven_ddp_solver.h:76
exotica::AbstractFeasibilityDrivenDDPSolver::get_xs
const std::vector< Eigen::VectorXd > & get_xs() const
Definition: feasibility_driven_ddp_solver.h:48
exotica::AbstractFeasibilityDrivenDDPSolver::us_
std::vector< Eigen::VectorXd > us_
Definition: feasibility_driven_ddp_solver.h:100
exotica::AbstractFeasibilityDrivenDDPSolver::regmin_
double regmin_
Minimum regularization (will not decrease lower)
Definition: feasibility_driven_ddp_solver.h:105
exotica::AbstractFeasibilityDrivenDDPSolver::Quu_ldlt_
std::vector< Eigen::LDLT< Eigen::MatrixXd > > Quu_ldlt_
Definition: feasibility_driven_ddp_solver.h:121
exotica::AbstractFeasibilityDrivenDDPSolver::ComputeDirection
bool ComputeDirection(const bool recalcDiff)
exotica::AbstractFeasibilityDrivenDDPSolver::FxTVxx_p_
Eigen::MatrixXd FxTVxx_p_
Definition: feasibility_driven_ddp_solver.h:117
exotica::AbstractFeasibilityDrivenDDPSolver::th_stepdec_
double th_stepdec_
Step-length threshold used to decrease regularization.
Definition: feasibility_driven_ddp_solver.h:124
exotica::AbstractFeasibilityDrivenDDPSolver::get_fs
const std::vector< Eigen::VectorXd > & get_fs() const
Definition: feasibility_driven_ddp_solver.h:47
exotica::AbstractFeasibilityDrivenDDPSolver::TryStep
double TryStep(const double steplength)
exotica::AbstractFeasibilityDrivenDDPSolver::th_acceptnegstep_
double th_acceptnegstep_
Threshold used for accepting step along ascent direction.
Definition: feasibility_driven_ddp_solver.h:99
exotica::AbstractFeasibilityDrivenDDPSolver::IncreaseRegularization
void IncreaseRegularization() override
exotica::AbstractFeasibilityDrivenDDPSolver::FuTVxx_p_
std::vector< Eigen::MatrixXd > FuTVxx_p_
Definition: feasibility_driven_ddp_solver.h:118
exotica::AbstractFeasibilityDrivenDDPSolver::dx_
std::vector< Eigen::VectorXd > dx_
Definition: feasibility_driven_ddp_solver.h:111