9 #include "crocoddyl/core/solvers/box-fddp.hpp"
13 #include "crocoddyl/core/utils/exception.hpp"
17 SolverBoxFDDP::SolverBoxFDDP(std::shared_ptr<ShootingProblem> problem)
18 : SolverFDDP(problem),
19 qp_(problem->get_runningModels()[0]->get_nu(), 100, 0.1, 1e-5, 0.) {
22 const std::size_t n_alphas = 10;
23 alphas_.resize(n_alphas);
24 for (std::size_t n = 0; n < n_alphas; ++n) {
25 alphas_[n] = 1. / pow(2.,
static_cast<double>(n));
34 SolverBoxFDDP::~SolverBoxFDDP() {}
36 void SolverBoxFDDP::resizeData() {
37 START_PROFILER(
"SolverBoxFDDP::resizeData");
38 SolverFDDP::resizeData();
40 const std::size_t T = problem_->get_T();
41 const std::vector<std::shared_ptr<ActionModelAbstract> >& models =
42 problem_->get_runningModels();
43 for (std::size_t t = 0; t < T; ++t) {
44 const std::shared_ptr<ActionModelAbstract>& model = models[t];
45 const std::size_t nu = model->get_nu();
46 Quu_inv_[t].conservativeResize(nu, nu);
47 du_lb_[t].conservativeResize(nu);
48 du_ub_[t].conservativeResize(nu);
50 STOP_PROFILER(
"SolverBoxFDDP::resizeData");
53 void SolverBoxFDDP::allocateData() {
54 SolverFDDP::allocateData();
56 const std::size_t T = problem_->get_T();
60 const std::vector<std::shared_ptr<ActionModelAbstract> >& models =
61 problem_->get_runningModels();
62 for (std::size_t t = 0; t < T; ++t) {
63 const std::shared_ptr<ActionModelAbstract>& model = models[t];
64 const std::size_t nu = model->get_nu();
65 Quu_inv_[t] = Eigen::MatrixXd::Zero(nu, nu);
66 du_lb_[t] = Eigen::VectorXd::Zero(nu);
67 du_ub_[t] = Eigen::VectorXd::Zero(nu);
71 void SolverBoxFDDP::computeGains(
const std::size_t t) {
72 const std::size_t nu = problem_->get_runningModels()[t]->get_nu();
74 if (!problem_->get_runningModels()[t]->get_has_control_limits() ||
77 SolverFDDP::computeGains(t);
81 du_lb_[t] = problem_->get_runningModels()[t]->get_u_lb() - us_[t];
82 du_ub_[t] = problem_->get_runningModels()[t]->get_u_ub() - us_[t];
84 const BoxQPSolution& boxqp_sol =
85 qp_.solve(Quu_[t], Qu_[t], du_lb_[t], du_ub_[t], k_[t]);
88 Quu_inv_[t].setZero();
89 for (std::size_t i = 0; i < boxqp_sol.free_idx.size(); ++i) {
90 for (std::size_t j = 0; j < boxqp_sol.free_idx.size(); ++j) {
91 Quu_inv_[t](boxqp_sol.free_idx[i], boxqp_sol.free_idx[j]) =
92 boxqp_sol.Hff_inv(i, j);
95 K_[t].noalias() = Quu_inv_[t] * Qxu_[t].transpose();
100 for (std::size_t i = 0; i < boxqp_sol.clamped_idx.size(); ++i) {
101 Qu_[t](boxqp_sol.clamped_idx[i]) = 0.;
106 void SolverBoxFDDP::forwardPass(
const double steplength) {
107 if (steplength > 1. || steplength < 0.) {
108 throw_pretty(
"Invalid argument: "
109 <<
"invalid step length, value is between 0. to 1.");
112 xnext_ = problem_->get_x0();
113 const std::size_t T = problem_->get_T();
114 const std::vector<std::shared_ptr<ActionModelAbstract> >& models =
115 problem_->get_runningModels();
116 const std::vector<std::shared_ptr<ActionDataAbstract> >& datas =
117 problem_->get_runningDatas();
118 if ((is_feasible_) || (steplength == 1)) {
119 for (std::size_t t = 0; t < T; ++t) {
120 const std::shared_ptr<ActionModelAbstract>& m = models[t];
121 const std::shared_ptr<ActionDataAbstract>& d = datas[t];
122 const std::size_t nu = m->get_nu();
125 m->get_state()->diff(xs_[t], xs_try_[t], dx_[t]);
127 us_try_[t].noalias() = us_[t] - k_[t] * steplength - K_[t] * dx_[t];
128 if (m->get_has_control_limits()) {
130 us_try_[t].cwiseMax(m->get_u_lb()).cwiseMin(m->get_u_ub());
132 m->calc(d, xs_try_[t], us_try_[t]);
134 m->calc(d, xs_try_[t]);
137 cost_try_ += d->cost;
139 if (raiseIfNaN(cost_try_)) {
140 throw_pretty(
"forward_error");
142 if (raiseIfNaN(xnext_.lpNorm<Eigen::Infinity>())) {
143 throw_pretty(
"forward_error");
147 const std::shared_ptr<ActionModelAbstract>& m =
148 problem_->get_terminalModel();
149 const std::shared_ptr<ActionDataAbstract>& d = problem_->get_terminalData();
150 xs_try_.back() = xnext_;
151 m->calc(d, xs_try_.back());
152 cost_try_ += d->cost;
154 if (raiseIfNaN(cost_try_)) {
155 throw_pretty(
"forward_error");
158 for (std::size_t t = 0; t < T; ++t) {
159 const std::shared_ptr<ActionModelAbstract>& m = models[t];
160 const std::shared_ptr<ActionDataAbstract>& d = datas[t];
161 const std::size_t nu = m->get_nu();
162 m->get_state()->integrate(xnext_, fs_[t] * (steplength - 1), xs_try_[t]);
163 m->get_state()->diff(xs_[t], xs_try_[t], dx_[t]);
165 us_try_[t].noalias() = us_[t] - k_[t] * steplength - K_[t] * dx_[t];
166 if (m->get_has_control_limits()) {
168 us_try_[t].cwiseMax(m->get_u_lb()).cwiseMin(m->get_u_ub());
170 m->calc(d, xs_try_[t], us_try_[t]);
172 m->calc(d, xs_try_[t]);
175 cost_try_ += d->cost;
177 if (raiseIfNaN(cost_try_)) {
178 throw_pretty(
"forward_error");
180 if (raiseIfNaN(xnext_.lpNorm<Eigen::Infinity>())) {
181 throw_pretty(
"forward_error");
185 const std::shared_ptr<ActionModelAbstract>& m =
186 problem_->get_terminalModel();
187 const std::shared_ptr<ActionDataAbstract>& d = problem_->get_terminalData();
188 m->get_state()->integrate(xnext_, fs_.back() * (steplength - 1),
190 m->calc(d, xs_try_.back());
191 cost_try_ += d->cost;
193 if (raiseIfNaN(cost_try_)) {
194 throw_pretty(
"forward_error");
199 const std::vector<Eigen::MatrixXd>& SolverBoxFDDP::get_Quu_inv()
const {