Crocoddyl
Loading...
Searching...
No Matches
kkt.hpp
1
2// BSD 3-Clause License
3//
4// Copyright (C) 2019-2025, LAAS-CNRS, New York University,
5// Max Planck Gesellschaft, University of Edinburgh
6// Heriot-Watt University
7// Copyright note valid unless otherwise stated in individual files.
8// All rights reserved.
10
11#ifndef CROCODDYL_CORE_SOLVERS_KKT_HPP_
12#define CROCODDYL_CORE_SOLVERS_KKT_HPP_
13
14#include "crocoddyl/core/solver-base.hpp"
15
16namespace crocoddyl {
17
18template <typename _Scalar>
19class SolverKKTTpl : public SolverAbstractTpl<_Scalar> {
20 public:
21 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
22 CROCODDYL_DERIVED_FLOATINGPOINT_CAST(SolverBase, SolverKKTTpl)
23
24 typedef _Scalar Scalar;
25 typedef SolverAbstractTpl<Scalar> SolverAbstract;
26 typedef ShootingProblemTpl<Scalar> ShootingProblem;
27 typedef typename ShootingProblem::ActionModelAbstract ActionModelAbstract;
28 typedef typename ShootingProblem::ActionDataAbstract ActionDataAbstract;
29 typedef CallbackAbstractTpl<Scalar> CallbackAbstract;
30 typedef MathBaseTpl<Scalar> MathBase;
31 typedef typename MathBase::VectorXs VectorXs;
32 typedef typename MathBase::MatrixXs MatrixXs;
33 typedef typename MathBase::Vector3s Vector3s;
35
36 explicit SolverKKTTpl(std::shared_ptr<ShootingProblem> problem);
37 virtual ~SolverKKTTpl() = default;
38
39 virtual bool solve(
40 const std::vector<VectorXs>& init_xs = DefaultVector<Scalar>::value,
41 const std::vector<VectorXs>& init_us = DefaultVector<Scalar>::value,
42 const std::size_t maxiter = 100, const bool is_feasible = false,
43 const Scalar regInit = 1e-9) override;
44 virtual void computeDirection(const bool recalc = true) override;
45 virtual Scalar tryStep(const Scalar steplength = Scalar(1.)) override;
46 virtual Scalar stoppingCriteria() override;
47 virtual Vector3s expectedImprovement() override;
48
57 template <typename NewScalar>
58 SolverKKTTpl<NewScalar> cast() const;
59
60 const MatrixXs& get_kkt() const;
61 const VectorXs& get_kktref() const;
62 const VectorXs& get_primaldual() const;
63 const std::vector<VectorXs>& get_dxs() const;
64 const std::vector<VectorXs>& get_dus() const;
65 const std::vector<VectorXs>& get_lambdas() const;
66 std::size_t get_nx() const;
67 std::size_t get_ndx() const;
68 std::size_t get_nu() const;
69
73 void set_alphas(const std::vector<Scalar>& alphas);
74
78 void set_reg_incfactor(const Scalar reg_factor);
79
83 void set_reg_decfactor(const Scalar reg_factor);
84
88 void set_reg_min(const Scalar regmin);
89
93 void set_reg_max(const Scalar regmax);
94
99 void set_th_grad(const Scalar th_grad);
100
101 protected:
102 void allocateData();
122
123 Scalar reg_incfactor_;
124 Scalar reg_decfactor_;
125 Scalar reg_min_;
126 Scalar reg_max_;
127 std::vector<Scalar> alphas_;
128 Scalar th_grad_;
129 bool was_feasible_;
130
131 private:
132 Scalar calcDiff();
133 void computePrimalDual();
134
135 std::size_t nx_;
136 std::size_t ndx_;
137 std::size_t nu_;
138 std::vector<VectorXs> dxs_;
139 std::vector<VectorXs> dus_;
140 std::vector<VectorXs> lambdas_;
141
142 // allocate data
143 MatrixXs kkt_;
144 VectorXs kktref_;
145 VectorXs primaldual_;
146 VectorXs primal_;
147 VectorXs dual_;
148 VectorXs kkt_primal_;
149 VectorXs dF;
150};
151
152} // namespace crocoddyl
153
154/* --- Details -------------------------------------------------------------- */
155/* --- Details -------------------------------------------------------------- */
156/* --- Details -------------------------------------------------------------- */
157#include "crocoddyl/core/solvers/kkt.hxx"
158
159#endif // CROCODDYL_CORE_SOLVERS_KKT_HPP_
Abstract class for solver callbacks.
This class encapsulates a shooting problem.
Definition shooting.hpp:32
const std::vector< VectorXs > & get_dus() const
Return the feedforward gains .
std::shared_ptr< ShootingProblem > problem_
void allocateData()
Allocate all the basic data needed in solvers.
SolverAbstractTpl(std::shared_ptr< ShootingProblem > problem)
Initialize the solver.
std::vector< std::shared_ptr< CallbackAbstract > > callbacks_
virtual void setCandidate(const std::vector< VectorXs > &xs_warm=DefaultVector< Scalar >::value, const std::vector< VectorXs > &us_warm=DefaultVector< Scalar >::value, const bool is_feasible=false)
const std::vector< VectorXs > & get_dxs() const
Return the linear update in .
void set_reg_incfactor(const Scalar reg_factor)
Modify the regularization factor used to increase the damping value.
SolverKKTTpl< NewScalar > cast() const
Cast the KKT solver to a different scalar type.
virtual void computeDirection(const bool recalc=true) override
Compute the search direction for the current guess .
void set_reg_decfactor(const Scalar reg_factor)
Modify the regularization factor used to decrease the damping value.
void set_reg_min(const Scalar regmin)
Modify the minimum regularization value.
virtual bool solve(const std::vector< VectorXs > &init_xs=DefaultVector< Scalar >::value, const std::vector< VectorXs > &init_us=DefaultVector< Scalar >::value, const std::size_t maxiter=100, const bool is_feasible=false, const Scalar regInit=1e-9) override
Compute the optimal trajectory as lists of and terms.
virtual Scalar tryStep(const Scalar steplength=Scalar(1.)) override
Try a predefined step length and compute its cost improvement and merit improvement .
void set_reg_max(const Scalar regmax)
Modify the maximum regularization value.
virtual Vector3s expectedImprovement() override
Return the expected improvement from a given current search direction .
virtual Scalar stoppingCriteria() override
Return a positive value that quantifies the algorithm termination.
void set_th_grad(const Scalar th_grad)
Modify the tolerance of the expected gradient used for testing the step.
void set_alphas(const std::vector< Scalar > &alphas)
Modify the set of step lengths using by the line-search procedure.