analytic_ddp_solver.cpp
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 
31 
33 
34 namespace exotica
35 {
36 void AnalyticDDPSolver::Instantiate(const AnalyticDDPSolverInitializer& init)
37 {
38  parameters_ = init;
39  base_parameters_ = AbstractDDPSolverInitializer(AnalyticDDPSolverInitializer(parameters_));
40 }
41 
42 void AnalyticDDPSolver::BackwardPass()
43 {
44  // NB: The DynamicTimeIndexedShootingProblem assumes row-major notation for derivatives
45  // The solvers follow DDP papers where we have a column-major notation => there will be transposes.
46  Vx_.back() = prob_->GetStateCostJacobian(T_ - 1);
47  Vxx_.back() = prob_->GetStateCostHessian(T_ - 1);
48 
49  // Regularization as introduced in Tassa's thesis, Eq. 24(a)
50  if (lambda_ != 0.0)
51  {
52  Vxx_.back().diagonal().array() += lambda_;
53  }
54 
55  // concatenation axis for tensor products
56  // See https://eigen.tuxfamily.org/dox-devel/unsupported/eigen_tensors.html#title14
57  const Eigen::array<Eigen::IndexPair<int>, 1> dims = {Eigen::IndexPair<int>(1, 0)};
58  Eigen::Tensor<double, 1> Vx_tensor;
59 
60  Eigen::VectorXd x(NX_), u(NU_); // TODO: Replace
61  // Quu_llt_ = Eigen::LLT<Eigen::MatrixXd>(NU_); // TODO: Allocate outside
62  for (int t = T_ - 2; t >= 0; t--)
63  {
64  x = prob_->get_X(t); // (NX,1)
65  u = prob_->get_U(t); // (NU,1)
66 
67  // NB: ComputeDerivatives computes the derivatives of the state transition function which includes the selected integration scheme.
68  dynamics_solver_->ComputeDerivatives(x, u);
69  fx_[t].noalias() = dynamics_solver_->get_Fx(); // (NDX,NDX)
70  fu_[t].noalias() = dynamics_solver_->get_Fu(); // (NDX,NU)
71 
72  //
73  // NB: We use a modified cost function to compare across different
74  // time horizons - the running cost is scaled by dt_
75  //
76  Qx_[t].noalias() = dt_ * prob_->GetStateCostJacobian(t); // Eq. 20(a) (1,NDX)^T => (NDX,1)
77  Qx_[t].noalias() += fx_[t].transpose() * Vx_[t + 1]; // lx + fx_ @ Vx_ (NDX,NDX)^T*(NDX,1)
78  Qu_[t].noalias() = dt_ * prob_->GetControlCostJacobian(t); // Eq. 20(b) (1,NU)^T => (NU,1)
79  Qu_[t].noalias() += fu_[t].transpose() * Vx_[t + 1]; // (NU,NDX)*(NDX,1) => (NU,1)
80 
81  Qxx_[t].noalias() = dt_ * prob_->GetStateCostHessian(t); // Eq. 20(c) (NDX,NDX)^T => (NDX,NDX)
82  Qxx_[t].noalias() += fx_[t].transpose() * Vxx_[t + 1] * fx_[t]; // + (NDX,NDX)^T*(NDX,NDX)*(NDX,NDX)
83  Quu_[t].noalias() = dt_ * prob_->GetControlCostHessian(t); // Eq. 20(d) (NU,NU)^T
84  Quu_[t].noalias() += fu_[t].transpose() * Vxx_[t + 1] * fu_[t]; // + (NDX,NU)^T*(NDX,NDX)*(NDX,NU)
85  // Qux_[t].noalias() = dt_ * prob_->GetStateControlCostHessian(); // Eq. 20(e) (NU,NDX)
86  // NB: This assumes that Lux is always 0.
87  Qux_[t].noalias() = fu_[t].transpose() * Vxx_[t + 1] * fx_[t]; // + (NDX,NU)^T*(NDX,NDX) =>(NU,NDX)
88 
89  // The tensor product terms need to be added if second-order dynamics are considered.
90  if (parameters_.UseSecondOrderDynamics && dynamics_solver_->get_has_second_order_derivatives())
91  {
92  Vx_tensor = Eigen::TensorMap<Eigen::Tensor<double, 1>>(Vx_[t + 1].data(), NDX_);
93 
94  Qxx_[t] += Eigen::TensorToMatrix((Eigen::Tensor<double, 2>)dynamics_solver_->fxx(x, u).contract(Vx_tensor, dims), NDX_, NDX_) * dt_;
95 
96  Quu_[t] += Eigen::TensorToMatrix((Eigen::Tensor<double, 2>)dynamics_solver_->fuu(x, u).contract(Vx_tensor, dims), NU_, NU_) * dt_;
97 
98  Qux_[t] += Eigen::TensorToMatrix((Eigen::Tensor<double, 2>)dynamics_solver_->fxu(x, u).contract(Vx_tensor, dims), NU_, NDX_) * dt_; // transpose?
99  }
100 
101  // Control regularization for numerical stability
102  Quu_[t].diagonal().array() += lambda_;
103 
104  // Compute gains using Cholesky decomposition
105  Quu_inv_[t] = Quu_[t].llt().solve(Eigen::MatrixXd::Identity(NU_, NU_));
106  k_[t].noalias() = -Quu_inv_[t] * Qu_[t];
107  K_[t].noalias() = -Quu_inv_[t] * Qux_[t];
108 
109  // Using Cholesky decomposition:
110  // Quu_llt_.compute(Quu_);
111  // K_[t] = -Qux_;
112  // Quu_llt_.solveInPlace(K_[t]);
113  // k_[t] = -Qu_;
114  // Quu_llt_.solveInPlace(k_[t]);
115 
116  // V = Q - 0.5 * (Qu_.transpose() * Quu_inv_ * Qu_)(0);
117 
118  // With regularisation:
119  Vx_[t] = Qx_[t] + K_[t].transpose() * Quu_[t] * k_[t] + K_[t].transpose() * Qu_[t] + Qux_[t].transpose() * k_[t]; // Eq. 25(b)
120  Vxx_[t] = Qxx_[t] + K_[t].transpose() * Quu_[t] * K_[t] + K_[t].transpose() * Qux_[t] + Qux_[t].transpose() * K_[t]; // Eq. 25(c)
121  Vxx_[t] = 0.5 * (Vxx_[t] + Vxx_[t].transpose()).eval(); // Ensure the Hessian of the value function is symmetric.
122 
123  // Regularization as introduced in Tassa's thesis, Eq. 24(a)
124  if (lambda_ != 0.0)
125  {
126  Vxx_[t].diagonal().array() += lambda_;
127  }
128  }
129 }
130 
131 } // namespace exotica
#define REGISTER_MOTIONSOLVER_TYPE(TYPE, DERIV)
MatrixType< Scalar > TensorToMatrix(const Eigen::Tensor< Scalar, rank > &tensor, const sizeType rows, const sizeType cols)
double x


exotica_ddp_solver
Author(s): Traiko Dinev
autogenerated on Sat Apr 10 2021 02:36:21