Exotica
abstract_ddp_solver.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019-2020, 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_ABSTRACT_DDP_SOLVER_H_
31 #define EXOTICA_DDP_SOLVER_ABSTRACT_DDP_SOLVER_H_
32 
35 #include <exotica_core/server.h>
37 #include <exotica_ddp_solver/abstract_ddp_solver_initializer.h>
38 
39 namespace exotica
40 {
41 // \brief Base DDP Solver class that implements the forward pass.
42 // and utility functions.
44 {
45 public:
48  void Solve(Eigen::MatrixXd& solution) override;
49 
53  void SpecifyProblem(PlanningProblemPtr pointer) override;
54 
55  Eigen::VectorXd GetFeedbackControl(Eigen::VectorXdRefConst x, int t) const override;
56 
57  const std::vector<Eigen::MatrixXd>& get_Vxx() const;
58  const std::vector<Eigen::VectorXd>& get_Vx() const;
59  const std::vector<Eigen::MatrixXd>& get_Qxx() const;
60  const std::vector<Eigen::MatrixXd>& get_Qux() const;
61  const std::vector<Eigen::MatrixXd>& get_Quu() const;
62  const std::vector<Eigen::VectorXd>& get_Qx() const;
63  const std::vector<Eigen::VectorXd>& get_Qu() const;
64  const std::vector<Eigen::MatrixXd>& get_K() const;
65  const std::vector<Eigen::VectorXd>& get_k() const;
66 
67  const std::vector<Eigen::VectorXd>& get_X_try() const;
68  const std::vector<Eigen::VectorXd>& get_U_try() const;
69 
70  const std::vector<Eigen::VectorXd>& get_X_ref() const;
71  const std::vector<Eigen::VectorXd>& get_U_ref() const;
72 
73  const std::vector<Eigen::MatrixXd>& get_Quu_inv() const;
74  const std::vector<Eigen::MatrixXd>& get_fx() const;
75  const std::vector<Eigen::MatrixXd>& get_fu() const;
76 
77  std::vector<double> get_control_cost_evolution() const;
78  void set_control_cost_evolution(const int index, const double cost);
79 
80  std::vector<double> get_steplength_evolution() const;
81  // void set_steplength_evolution(const int index, const double cost);
82 
83  std::vector<double> get_regularization_evolution() const;
84  // void set_regularization_evolution(const int index, const double cost);
85 
86 protected:
89 
92  virtual void BackwardPass() = 0;
93 
99  double ForwardPass(const double alpha);
100 
101  AbstractDDPSolverInitializer base_parameters_;
102 
103  virtual void IncreaseRegularization()
104  {
105  lambda_ *= 10.;
106  }
107 
108  virtual void DecreaseRegularization()
109  {
110  lambda_ /= 10.;
111  }
112 
113  // Local variables used in the solver - copies get updated at the beginning of solve:
114  Eigen::VectorXd alpha_space_;
115  double lambda_;
116  int T_;
117  int NU_;
118  int NX_;
119  int NDX_;
120  int NV_;
121  double dt_;
122  double cost_;
123  double control_cost_;
124 
125  double cost_try_;
127 
128  double cost_prev_;
129  double alpha_best_;
131 
132  std::vector<Eigen::MatrixXd> Vxx_;
133  std::vector<Eigen::VectorXd> Vx_;
134  std::vector<Eigen::MatrixXd> Qxx_;
135  std::vector<Eigen::MatrixXd> Qux_;
136  std::vector<Eigen::MatrixXd> Quu_;
137  std::vector<Eigen::VectorXd> Qx_;
138  std::vector<Eigen::VectorXd> Qu_;
139  std::vector<Eigen::MatrixXd> K_;
140  std::vector<Eigen::VectorXd> k_;
141 
142  std::vector<Eigen::VectorXd> X_try_;
143  std::vector<Eigen::VectorXd> U_try_;
144 
145  std::vector<Eigen::VectorXd> X_ref_;
146  std::vector<Eigen::VectorXd> U_ref_;
147 
148  std::vector<Eigen::MatrixXd> Quu_inv_;
149  std::vector<Eigen::MatrixXd> fx_;
150  std::vector<Eigen::MatrixXd> fu_;
151 
152  std::vector<double> control_cost_evolution_;
153  std::vector<double> steplength_evolution_;
154  std::vector<double> regularization_evolution_;
155 };
156 
157 } // namespace exotica
158 
159 #endif // EXOTICA_DDP_SOLVER_ABSTRACT_DDP_SOLVER_H_
exotica::AbstractDDPSolver::Qxx_
std::vector< Eigen::MatrixXd > Qxx_
Hessian of the Hamiltonian.
Definition: abstract_ddp_solver.h:134
dynamic_time_indexed_shooting_problem.h
exotica::AbstractDDPSolver::fu_
std::vector< Eigen::MatrixXd > fu_
Derivative of the dynamics f w.r.t. u.
Definition: abstract_ddp_solver.h:150
exotica::AbstractDDPSolver::Qux_
std::vector< Eigen::MatrixXd > Qux_
Hessian of the Hamiltonian.
Definition: abstract_ddp_solver.h:135
exotica::AbstractDDPSolver::U_try_
std::vector< Eigen::VectorXd > U_try_
Updated control trajectory during iteration (computed by line-search)
Definition: abstract_ddp_solver.h:143
exotica::AbstractDDPSolver::get_K
const std::vector< Eigen::MatrixXd > & get_K() const
exotica::AbstractDDPSolver::get_Vx
const std::vector< Eigen::VectorXd > & get_Vx() const
exotica::AbstractDDPSolver::alpha_best_
double alpha_best_
Line-search step taken.
Definition: abstract_ddp_solver.h:129
exotica::AbstractDDPSolver::Qu_
std::vector< Eigen::VectorXd > Qu_
Gradient of the Hamiltonian.
Definition: abstract_ddp_solver.h:138
exotica::AbstractDDPSolver::time_taken_backward_pass_
double time_taken_backward_pass_
Definition: abstract_ddp_solver.h:130
exotica::AbstractDDPSolver::DecreaseRegularization
virtual void DecreaseRegularization()
Definition: abstract_ddp_solver.h:108
exotica::AbstractDDPSolver::Vx_
std::vector< Eigen::VectorXd > Vx_
Gradient of the Value function.
Definition: abstract_ddp_solver.h:133
exotica::AbstractDDPSolver::cost_prev_
double cost_prev_
Cost during previous iteration.
Definition: abstract_ddp_solver.h:128
exotica::AbstractDDPSolver::U_ref_
std::vector< Eigen::VectorXd > U_ref_
Reference control trajectory for feedback control.
Definition: abstract_ddp_solver.h:146
exotica::AbstractDDPSolver::control_cost_try_
double control_cost_try_
Total control cost computed by line-search procedure.
Definition: abstract_ddp_solver.h:126
exotica::AbstractDDPSolver::dynamics_solver_
DynamicsSolverPtr dynamics_solver_
Shared pointer to the dynamics solver.
Definition: abstract_ddp_solver.h:88
exotica::AbstractDDPSolver::Solve
void Solve(Eigen::MatrixXd &solution) override
Solves the problem.
exotica::AbstractDDPSolver::Quu_inv_
std::vector< Eigen::MatrixXd > Quu_inv_
Inverse of the Hessian of the Hamiltonian.
Definition: abstract_ddp_solver.h:148
exotica::AbstractDDPSolver::Qx_
std::vector< Eigen::VectorXd > Qx_
Gradient of the Hamiltonian.
Definition: abstract_ddp_solver.h:137
exotica::AbstractDDPSolver::NDX_
int NDX_
Size of tangent vector to the state vector.
Definition: abstract_ddp_solver.h:119
exotica::FeedbackMotionSolver
Definition: feedback_motion_solver.h:37
exotica::DynamicsSolverPtr
std::shared_ptr< exotica::DynamicsSolver > DynamicsSolverPtr
Definition: dynamics_solver.h:269
exotica::AbstractDDPSolver::X_try_
std::vector< Eigen::VectorXd > X_try_
Updated state trajectory during iteration (computed by line-search)
Definition: abstract_ddp_solver.h:142
exotica::AbstractDDPSolver::get_regularization_evolution
std::vector< double > get_regularization_evolution() const
exotica::AbstractDDPSolver::prob_
DynamicTimeIndexedShootingProblemPtr prob_
Shared pointer to the planning problem.
Definition: abstract_ddp_solver.h:87
exotica
Definition: cartpole_dynamics_solver.h:38
conversions.h
exotica::AbstractDDPSolver::get_Qu
const std::vector< Eigen::VectorXd > & get_Qu() const
exotica::AbstractDDPSolver::ForwardPass
double ForwardPass(const double alpha)
Forward simulates the dynamics using the gains computed in the last BackwardPass;.
exotica::AbstractDDPSolver::BackwardPass
virtual void BackwardPass()=0
Computes the control gains for a the trajectory in the associated DynamicTimeIndexedProblem.
exotica::AbstractDDPSolver::time_taken_forward_pass_
double time_taken_forward_pass_
Definition: abstract_ddp_solver.h:130
exotica::AbstractDDPSolver::K_
std::vector< Eigen::MatrixXd > K_
Feedback gains.
Definition: abstract_ddp_solver.h:139
exotica::AbstractDDPSolver::get_U_try
const std::vector< Eigen::VectorXd > & get_U_try() const
exotica::AbstractDDPSolver
Definition: abstract_ddp_solver.h:43
exotica::AbstractDDPSolver::base_parameters_
AbstractDDPSolverInitializer base_parameters_
Definition: abstract_ddp_solver.h:101
Eigen::VectorXdRefConst
const typedef Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:52
exotica::AbstractDDPSolver::steplength_evolution_
std::vector< double > steplength_evolution_
Evolution of the steplength.
Definition: abstract_ddp_solver.h:153
exotica::AbstractDDPSolver::get_Qxx
const std::vector< Eigen::MatrixXd > & get_Qxx() const
exotica::AbstractDDPSolver::get_Quu_inv
const std::vector< Eigen::MatrixXd > & get_Quu_inv() const
exotica::AbstractDDPSolver::get_Qx
const std::vector< Eigen::VectorXd > & get_Qx() const
exotica::AbstractDDPSolver::get_Vxx
const std::vector< Eigen::MatrixXd > & get_Vxx() const
exotica::DynamicTimeIndexedShootingProblemPtr
std::shared_ptr< exotica::DynamicTimeIndexedShootingProblem > DynamicTimeIndexedShootingProblemPtr
Definition: dynamic_time_indexed_shooting_problem.h:231
exotica::AbstractDDPSolver::alpha_space_
Eigen::VectorXd alpha_space_
Backtracking line-search steplengths.
Definition: abstract_ddp_solver.h:114
exotica::AbstractDDPSolver::lambda_
double lambda_
Regularization (Vxx, Quu)
Definition: abstract_ddp_solver.h:115
exotica::AbstractDDPSolver::Quu_
std::vector< Eigen::MatrixXd > Quu_
Hessian of the Hamiltonian.
Definition: abstract_ddp_solver.h:136
exotica::AbstractDDPSolver::dt_
double dt_
Integration time-step.
Definition: abstract_ddp_solver.h:121
exotica::AbstractDDPSolver::cost_try_
double cost_try_
Total cost computed by line-search procedure.
Definition: abstract_ddp_solver.h:125
exotica::AbstractDDPSolver::get_Qux
const std::vector< Eigen::MatrixXd > & get_Qux() const
exotica::PlanningProblemPtr
std::shared_ptr< PlanningProblem > PlanningProblemPtr
Definition: planning_problem.h:116
exotica::AbstractDDPSolver::regularization_evolution_
std::vector< double > regularization_evolution_
Evolution of the regularization (xreg/ureg)
Definition: abstract_ddp_solver.h:154
exotica::AbstractDDPSolver::GetFeedbackControl
Eigen::VectorXd GetFeedbackControl(Eigen::VectorXdRefConst x, int t) const override
exotica::AbstractDDPSolver::NV_
int NV_
Size of velocity vector (tangent vector to the configuration)
Definition: abstract_ddp_solver.h:120
exotica::AbstractDDPSolver::get_X_try
const std::vector< Eigen::VectorXd > & get_X_try() const
exotica::AbstractDDPSolver::NX_
int NX_
Size of state vector.
Definition: abstract_ddp_solver.h:118
exotica::AbstractDDPSolver::get_control_cost_evolution
std::vector< double > get_control_cost_evolution() const
exotica::AbstractDDPSolver::get_X_ref
const std::vector< Eigen::VectorXd > & get_X_ref() const
exotica::AbstractDDPSolver::get_fx
const std::vector< Eigen::MatrixXd > & get_fx() const
exotica::AbstractDDPSolver::IncreaseRegularization
virtual void IncreaseRegularization()
Definition: abstract_ddp_solver.h:103
exotica::AbstractDDPSolver::fx_
std::vector< Eigen::MatrixXd > fx_
Derivative of the dynamics f w.r.t. x.
Definition: abstract_ddp_solver.h:149
exotica::AbstractDDPSolver::get_fu
const std::vector< Eigen::MatrixXd > & get_fu() const
exotica::AbstractDDPSolver::SpecifyProblem
void SpecifyProblem(PlanningProblemPtr pointer) override
Binds the solver to a specific problem which must be pre-initalised.
exotica::AbstractDDPSolver::get_k
const std::vector< Eigen::VectorXd > & get_k() const
feedback_motion_solver.h
exotica::AbstractDDPSolver::Vxx_
std::vector< Eigen::MatrixXd > Vxx_
Hessian of the Value function.
Definition: abstract_ddp_solver.h:132
exotica::AbstractDDPSolver::NU_
int NU_
Size of control vector.
Definition: abstract_ddp_solver.h:117
server.h
exotica::AbstractDDPSolver::X_ref_
std::vector< Eigen::VectorXd > X_ref_
Reference state trajectory for feedback control.
Definition: abstract_ddp_solver.h:145
exotica::AbstractDDPSolver::set_control_cost_evolution
void set_control_cost_evolution(const int index, const double cost)
exotica::AbstractDDPSolver::cost_
double cost_
Cost during iteration.
Definition: abstract_ddp_solver.h:122
exotica::AbstractDDPSolver::get_steplength_evolution
std::vector< double > get_steplength_evolution() const
exotica::AbstractDDPSolver::T_
int T_
Length of shooting problem, i.e., state trajectory. The control trajectory has length T_-1.
Definition: abstract_ddp_solver.h:116
exotica::AbstractDDPSolver::control_cost_
double control_cost_
Control cost during iteration.
Definition: abstract_ddp_solver.h:123
exotica::AbstractDDPSolver::k_
std::vector< Eigen::VectorXd > k_
Feed-forward terms.
Definition: abstract_ddp_solver.h:140
exotica::AbstractDDPSolver::get_Quu
const std::vector< Eigen::MatrixXd > & get_Quu() const
exotica::AbstractDDPSolver::control_cost_evolution_
std::vector< double > control_cost_evolution_
Evolution of the control cost (control regularization)
Definition: abstract_ddp_solver.h:152
exotica::AbstractDDPSolver::get_U_ref
const std::vector< Eigen::VectorXd > & get_U_ref() const