11 #include "crocoddyl/core/solvers/kkt.hpp"
15 SolverKKT::SolverKKT(boost::shared_ptr<ShootingProblem> problem)
16 : SolverAbstract(problem),
23 was_feasible_(false) {
25 const std::size_t n_alphas = 10;
28 alphas_.resize(n_alphas);
29 for (std::size_t n = 0; n < n_alphas; ++n) {
30 alphas_[n] = 1. / pow(2., (
double)n);
34 SolverKKT::~SolverKKT() {}
36 bool SolverKKT::solve(
const std::vector<Eigen::VectorXd>& init_xs,
37 const std::vector<Eigen::VectorXd>& init_us,
38 const std::size_t maxiter,
const bool is_feasible,
40 setCandidate(init_xs, init_us, is_feasible);
42 for (iter_ = 0; iter_ < maxiter; ++iter_) {
45 computeDirection(recalc);
46 }
catch (std::exception& e) {
48 if (preg_ == reg_max_) {
57 expectedImprovement();
58 for (std::vector<double>::const_iterator it = alphas_.begin();
59 it != alphas_.end(); ++it) {
62 dV_ = tryStep(steplength_);
63 }
catch (std::exception& e) {
66 dVexp_ = steplength_ * d_[0] + 0.5 * steplength_ * steplength_ * d_[1];
67 if (d_[0] < th_grad_ || !is_feasible_ || dV_ > th_acceptstep_ * dVexp_) {
68 was_feasible_ = is_feasible_;
69 setCandidate(xs_try_, us_try_,
true);
75 const std::size_t n_callbacks = callbacks_.size();
76 if (n_callbacks != 0) {
77 for (std::size_t c = 0; c < n_callbacks; ++c) {
82 if (was_feasible_ && stop_ < th_stop_) {
89 void SolverKKT::computeDirection(
const bool recalc) {
90 const std::size_t T = problem_->get_T();
95 const Eigen::VectorBlock<Eigen::VectorXd, Eigen::Dynamic> p_x =
96 primal_.segment(0, ndx_);
97 const Eigen::VectorBlock<Eigen::VectorXd, Eigen::Dynamic> p_u =
98 primal_.segment(ndx_, nu_);
102 const std::vector<boost::shared_ptr<ActionModelAbstract> >& models =
103 problem_->get_runningModels();
104 for (std::size_t t = 0; t < T; ++t) {
105 const std::size_t ndxi = models[t]->get_state()->get_ndx();
106 const std::size_t nui = models[t]->get_nu();
107 dxs_[t] = p_x.segment(ix, ndxi);
108 dus_[t] = p_u.segment(iu, nui);
109 lambdas_[t] = dual_.segment(ix, ndxi);
113 const std::size_t ndxi =
114 problem_->get_terminalModel()->get_state()->get_ndx();
115 dxs_.back() = p_x.segment(ix, ndxi);
116 lambdas_.back() = dual_.segment(ix, ndxi);
119 double SolverKKT::tryStep(
const double steplength) {
120 const std::size_t T = problem_->get_T();
121 const std::vector<boost::shared_ptr<ActionModelAbstract> >& models =
122 problem_->get_runningModels();
123 for (std::size_t t = 0; t < T; ++t) {
124 const boost::shared_ptr<ActionModelAbstract>& m = models[t];
126 m->get_state()->integrate(xs_[t], steplength * dxs_[t], xs_try_[t]);
127 if (m->get_nu() != 0) {
129 us_try_[t] += steplength * dus_[t];
132 const boost::shared_ptr<ActionModelAbstract> m =
133 problem_->get_terminalModel();
134 m->get_state()->integrate(xs_[T], steplength * dxs_[T], xs_try_[T]);
135 cost_try_ = problem_->calc(xs_try_, us_try_);
136 return cost_ - cost_try_;
139 double SolverKKT::stoppingCriteria() {
140 const std::size_t T = problem_->get_T();
143 const std::vector<boost::shared_ptr<ActionModelAbstract> >& models =
144 problem_->get_runningModels();
145 const std::vector<boost::shared_ptr<ActionDataAbstract> >& datas =
146 problem_->get_runningDatas();
147 for (std::size_t t = 0; t < T; ++t) {
148 const boost::shared_ptr<ActionDataAbstract>& d = datas[t];
149 const std::size_t ndxi = models[t]->get_state()->get_ndx();
150 const std::size_t nui = models[t]->get_nu();
152 dF.segment(ix, ndxi) = lambdas_[t];
153 dF.segment(ix, ndxi).noalias() -= d->Fx.transpose() * lambdas_[t + 1];
154 dF.segment(ndx_ + iu, nui).noalias() = -lambdas_[t + 1].transpose() * d->Fu;
158 const std::size_t ndxi =
159 problem_->get_terminalModel()->get_state()->get_ndx();
160 dF.segment(ix, ndxi) = lambdas_.back();
161 stop_ = (kktref_.segment(0, ndx_ + nu_) + dF).squaredNorm() +
162 kktref_.segment(ndx_ + nu_, ndx_).squaredNorm();
166 const Eigen::Vector2d& SolverKKT::expectedImprovement() {
167 d_ = Eigen::Vector2d::Zero();
169 d_(0) = -kktref_.segment(0, ndx_ + nu_).dot(primal_);
171 kkt_primal_.noalias() = kkt_.block(0, 0, ndx_ + nu_, ndx_ + nu_) * primal_;
172 d_(1) = -kkt_primal_.dot(primal_);
176 const Eigen::MatrixXd& SolverKKT::get_kkt()
const {
return kkt_; }
178 const Eigen::VectorXd& SolverKKT::get_kktref()
const {
return kktref_; }
180 const Eigen::VectorXd& SolverKKT::get_primaldual()
const {
return primaldual_; }
182 const std::vector<Eigen::VectorXd>& SolverKKT::get_dxs()
const {
return dxs_; }
184 const std::vector<Eigen::VectorXd>& SolverKKT::get_dus()
const {
return dus_; }
186 const std::vector<Eigen::VectorXd>& SolverKKT::get_lambdas()
const {
190 std::size_t SolverKKT::get_nx()
const {
return nx_; }
192 std::size_t SolverKKT::get_ndx()
const {
return ndx_; }
194 std::size_t SolverKKT::get_nu()
const {
return nu_; }
196 double SolverKKT::calcDiff() {
197 cost_ = problem_->calc(xs_, us_);
198 cost_ = problem_->calcDiff(xs_, us_);
201 const std::size_t cx0 =
202 problem_->get_runningModels()[0]->get_state()->get_ndx();
206 const std::size_t T = problem_->get_T();
207 kkt_.block(ndx_ + nu_, 0, ndx_, ndx_).setIdentity();
208 for (std::size_t t = 0; t < T; ++t) {
209 const boost::shared_ptr<ActionModelAbstract>& m =
210 problem_->get_runningModels()[t];
211 const boost::shared_ptr<ActionDataAbstract>& d =
212 problem_->get_runningDatas()[t];
213 const std::size_t ndxi = m->get_state()->get_ndx();
214 const std::size_t nui = m->get_nu();
218 m->get_state()->diff(problem_->get_x0(), xs_[0],
219 kktref_.segment(ndx_ + nu_, ndxi));
223 kkt_.block(ix, ix, ndxi, ndxi) = d->Lxx;
224 kkt_.block(ix, ndx_ + iu, ndxi, nui) = d->Lxu;
225 kkt_.block(ndx_ + iu, ix, nui, ndxi) = d->Lxu.transpose();
226 kkt_.block(ndx_ + iu, ndx_ + iu, nui, nui) = d->Luu;
227 kkt_.block(ndx_ + nu_ + cx0 + ix, ix, ndxi, ndxi) = -d->Fx;
228 kkt_.block(ndx_ + nu_ + cx0 + ix, ndx_ + iu, ndxi, nui) = -d->Fu;
231 kktref_.segment(ix, ndxi) = d->Lx;
232 kktref_.segment(ndx_ + iu, nui) = d->Lu;
233 m->get_state()->diff(d->xnext, xs_[t + 1],
234 kktref_.segment(ndx_ + nu_ + cx0 + ix, ndxi));
239 const boost::shared_ptr<ActionDataAbstract>& df =
240 problem_->get_terminalData();
241 const std::size_t ndxf =
242 problem_->get_terminalModel()->get_state()->get_ndx();
243 kkt_.block(ix, ix, ndxf, ndxf) = df->Lxx;
244 kktref_.segment(ix, ndxf) = df->Lx;
245 kkt_.block(0, ndx_ + nu_, ndx_ + nu_, ndx_) =
246 kkt_.block(ndx_ + nu_, 0, ndx_, ndx_ + nu_).transpose();
250 void SolverKKT::computePrimalDual() {
251 primaldual_ = kkt_.lu().solve(-kktref_);
252 primal_ = primaldual_.segment(0, ndx_ + nu_);
253 dual_ = primaldual_.segment(ndx_ + nu_, ndx_);
256 void SolverKKT::increaseRegularization() {
257 preg_ *= reg_incfactor_;
258 if (preg_ > reg_max_) {
264 void SolverKKT::decreaseRegularization() {
265 preg_ /= reg_decfactor_;
266 if (preg_ < reg_min_) {
272 void SolverKKT::allocateData() {
273 const std::size_t T = problem_->get_T();
276 lambdas_.resize(T + 1);
277 xs_try_.resize(T + 1);
283 const std::size_t nx = problem_->get_nx();
284 const std::size_t ndx = problem_->get_ndx();
285 const std::vector<boost::shared_ptr<ActionModelAbstract> >& models =
286 problem_->get_runningModels();
287 for (std::size_t t = 0; t < T; ++t) {
288 const boost::shared_ptr<ActionModelAbstract>& model = models[t];
290 xs_try_[t] = problem_->get_x0();
292 xs_try_[t] = Eigen::VectorXd::Constant(nx, NAN);
294 const std::size_t nu = model->get_nu();
295 us_try_[t] = Eigen::VectorXd::Constant(nu, NAN);
296 dxs_[t] = Eigen::VectorXd::Zero(ndx);
297 dus_[t] = Eigen::VectorXd::Zero(nu);
298 lambdas_[t] = Eigen::VectorXd::Zero(ndx);
305 xs_try_.back() = problem_->get_terminalModel()->get_state()->zero();
306 dxs_.back() = Eigen::VectorXd::Zero(ndx);
307 lambdas_.back() = Eigen::VectorXd::Zero(ndx);
310 kkt_.resize(2 * ndx_ + nu_, 2 * ndx_ + nu_);
312 kktref_.resize(2 * ndx_ + nu_);
314 primaldual_.resize(2 * ndx_ + nu_);
315 primaldual_.setZero();
316 primal_.resize(ndx_ + nu_);
318 kkt_primal_.resize(ndx_ + nu_);
319 kkt_primal_.setZero();
322 dF.resize(ndx_ + nu_);
Abstract class for solver callbacks.