16 #include <boost/fusion/container/vector/vector.hpp>
18 #include "pinocchio/macros.hpp"
19 #include "pinocchio/eigen-common.hpp"
21 #include "pinocchio/utils/check.hpp"
22 #include "pinocchio/utils/reference.hpp"
24 #include "pinocchio/container/eigen-storage.hpp"
26 #include "pinocchio/math.hpp"
28 #include "pinocchio/constraints.hpp"
30 #include "pinocchio/algorithm/solvers/fwd.hpp"
31 #include "pinocchio/algorithm/solvers/constraint-solver-base.hpp"
32 #include "pinocchio/algorithm/solvers/constraint-solver-utils.hpp"
42 template<
typename Scalar,
int Options>
43 struct PGSSolverWorkspaceTpl;
46 template<
typename _Scalar,
int _Options>
49 typedef _Scalar Scalar;
50 static constexpr
int Options = _Options;
57 template<
typename _Scalar,
int _Options>
60 typedef _Scalar Scalar;
61 static constexpr
int Options = _Options;
63 typedef ConstraintSolverBase<Self> Base;
64 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> VectorXs;
65 typedef Eigen::Ref<const VectorXs> RefConstVectorXs;
67 typedef internal::PGSSolverWorkspaceTpl<Scalar, Options> PGSSolverWorkspace;
73 using Base::timerStart;
74 using Base::timerStop;
102 typename DelassusDerived,
104 typename ConstraintModel,
105 typename ConstraintModelAllocator,
106 typename ConstraintData,
107 typename ConstraintDataAllocator>
109 DelassusOperatorBase<DelassusDerived> & delassus,
110 const Eigen::MatrixBase<VectorLike> & g,
111 const std::vector<ConstraintModel, ConstraintModelAllocator> & constraint_models,
112 const std::vector<ConstraintData, ConstraintDataAllocator> & constraint_datas,
138 template<
typename _Scalar>
141 typedef _Scalar Scalar;
146 template<
typename _Scalar>
149 typedef _Scalar Scalar;
151 typedef ConstraintSolverSettingsBase<Self> Base;
152 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> VectorXs;
156 std::size_t max_iterations = 1000,
157 Scalar absolute_feasibility_tol = Scalar(1e-6),
158 Scalar relative_feasibility_tol = Scalar(1e-6),
159 Scalar absolute_complementarity_tol = Scalar(1e-6),
160 Scalar relative_complementarity_tol = Scalar(1e-6),
161 bool solve_ncp =
true,
162 bool measure_timings =
false,
163 bool stat_record =
false,
167 absolute_feasibility_tol,
168 relative_feasibility_tol,
169 absolute_complementarity_tol,
170 relative_complementarity_tol,
178 void checkValidityImpl()
const
180 PINOCCHIO_CHECK_INPUT_ARGUMENT(
182 "over_relaxation should lie in ]0,2[.");
189 using Base::max_iterations;
192 using Base::absolute_feasibility_tol;
195 using Base::relative_feasibility_tol;
198 using Base::absolute_complementarity_tol;
201 using Base::relative_complementarity_tol;
205 using Base::solve_ncp;
208 using Base::measure_timings;
211 using Base::stat_record;
220 template<
typename _Scalar,
int _Options>
223 typedef _Scalar Scalar;
224 static constexpr
int Options = _Options;
231 template<
typename _Scalar,
int _Options>
234 typedef _Scalar Scalar;
235 static constexpr
int Options = _Options;
237 typedef ConstraintSolverResultBase<Self> Base;
239 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> VectorXs;
240 typedef Eigen::Ref<const VectorXs> RefConstVectorXs;
241 typedef internal::EigenStorageTpl<VectorXs> VectorXsStorage;
243 using Base::constraintSize;
244 using Base::setConstraintImpulseGuess;
245 using Base::setConstraintVelocityGuess;
266 Base::operator=(other);
281 clearConstraintImpulseGuessImpl();
298 x.setConstant(std::numeric_limits<Scalar>::quiet_NaN());
299 y.setConstant(std::numeric_limits<Scalar>::quiet_NaN());
307 Eigen::Index np =
static_cast<Eigen::Index
>(
problem_size);
313 template<
typename VectorLike>
316 auto & primal_solution = primal_solution_.const_cast_derived();
321 template<
typename VectorLike>
324 auto & dual_solution = dual_solution_.const_cast_derived();
328 template<
typename VectorLike>
329 void setConstraintImpulseGuessImpl(
const Eigen::MatrixBase<VectorLike> & impulse_guess_in)
332 m_impulse_guess = impulse_guess_in;
336 void clearConstraintImpulseGuessImpl()
342 template<
typename VectorLike>
343 void setConstraintVelocityGuessImpl(
const Eigen::MatrixBase<VectorLike> & )
348 void clearConstraintVelocityGuessImpl()
353 template<
typename VectorLike>
355 retrieveConstraintImpulsesImpl(
const Eigen::MatrixBase<VectorLike> & constraint_impulses_)
const
357 auto & constraint_impulses = constraint_impulses_.const_cast_derived();
358 constraint_impulses = x;
364 template<
typename VectorLike>
366 const Eigen::MatrixBase<VectorLike> & constraint_velocities_)
const
368 auto & constraint_velocities = constraint_velocities_.const_cast_derived();
369 constraint_velocities = y;
373 using Base::iterations;
376 using Base::converged;
379 using Base::primal_feasibility;
382 using Base::dual_feasibility;
385 using Base::complementarity;
403 typename VectorXsStorage::RefMapType x =
x_storage.map();
407 typename VectorXsStorage::RefMapType y =
y_storage.map();
414 template<
typename _Scalar>
417 typedef _Scalar Scalar;
422 template<
typename _Scalar>
425 typedef _Scalar Scalar;
427 typedef ConstraintSolverStatsBase<Self> Base;
439 : Base(max_iterations)
441 reserve(max_iterations);
444 void reserveImpl(std::size_t )
455 using Base::iterations;
458 using Base::primal_feasibility;
461 using Base::dual_feasibility;
464 using Base::dual_feasibility_ncp;
467 using Base::complementarity;
474 template<
typename _Scalar,
int _Options>
475 struct PGSSolverWorkspaceTpl
477 typedef _Scalar Scalar;
478 static constexpr
int Options = _Options;
479 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> VectorXs;
480 typedef internal::EigenStorageTpl<VectorXs> VectorXsStorage;
481 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Options> MatrixXs;
482 typedef internal::EigenStorageTpl<MatrixXs> MatrixXsStorage;
485 PGSSolverWorkspaceTpl(std::size_t problem_size = 0)
486 : problem_size(problem_size)
488 resize(problem_size);
493 PGSSolverWorkspaceTpl(
const PGSSolverWorkspaceTpl & other)
494 : PGSSolverWorkspaceTpl(0)
500 PGSSolverWorkspaceTpl & operator=(
const PGSSolverWorkspaceTpl & other)
504 problem_size = other.problem_size;
509 delassus_matrix_storage = other.delassus_matrix_storage;
510 x_storage = other.x_storage;
511 x_previous_storage = other.x_previous_storage;
512 y_storage = other.y_storage;
513 rhs_storage = other.rhs_storage;
514 tmp_storage = other.tmp_storage;
522 resize(problem_size);
525 delassus_matrix.setConstant(std::numeric_limits<Scalar>::quiet_NaN());
526 x.setConstant(std::numeric_limits<Scalar>::quiet_NaN());
527 x_previous.setConstant(std::numeric_limits<Scalar>::quiet_NaN());
528 y.setConstant(std::numeric_limits<Scalar>::quiet_NaN());
529 tmp.setConstant(std::numeric_limits<Scalar>::quiet_NaN());
530 rhs.setConstant(std::numeric_limits<Scalar>::quiet_NaN());
535 void resize(std::size_t problem_size_)
537 problem_size = problem_size_;
539 Eigen::Index np =
static_cast<Eigen::Index
>(problem_size);
540 delassus_matrix_storage.resize(np, np);
541 x_storage.resize(np);
542 x_previous_storage.resize(np);
543 y_storage.resize(np);
544 tmp_storage.resize(np);
545 rhs_storage.resize(np);
549 std::size_t problem_size;
552 MatrixXsStorage delassus_matrix_storage;
553 typename MatrixXsStorage::RefMapType delassus_matrix = delassus_matrix_storage.map();
556 VectorXsStorage x_storage;
557 typename VectorXsStorage::RefMapType x = x_storage.map();
560 VectorXsStorage x_previous_storage;
561 typename VectorXsStorage::RefMapType x_previous = x_previous_storage.map();
564 VectorXsStorage y_storage;
565 typename VectorXsStorage::RefMapType y = y_storage.map();
568 VectorXsStorage rhs_storage;
569 typename VectorXsStorage::RefMapType rhs = rhs_storage.map();
572 VectorXsStorage tmp_storage;
573 typename VectorXsStorage::RefMapType tmp = tmp_storage.map();
580 #include "pinocchio/src/algorithm/solvers/pgs-solver.hxx"
Main pinocchio namespace.
Projected Gauss Siedel solver.
bool m_is_valid
Flag to check whether or not the solver is in a reset state. If not, its stats are valid.
bool isValid() const
Returns true if solver is in a valid state (it has solved a constraint problem). If so,...
PGSConstraintSolverTpl(std::size_t max_problem_size=0)
Default constructor.
PGSSolverStats stats
Per-iteration stats of the PGS solver.
PGSSolverWorkspace m_workspace
Workspace of the PGS solver. This is an internal of the solver and is not meant to be accessed by use...
Struct describing the solution of the PGS constraint solver after calling the solve method....
PGSSolverResultTpl()
Default constructor.
VectorXsStorage x_storage
Primal solution.
void resize(std::size_t problem_size_)
Resize the primal/dual vectors of the solution.
std::optional< RefConstVectorXs > impulse_guess
Optional guess for the primal variable (impulses).
void retrieveDualSolution(const Eigen::MatrixBase< VectorLike > &dual_solution_) const
Retrieve dual solution.
PGSSolverResultTpl(const PGSSolverResultTpl &other)
Copy constructor.
int constraintSizeImpl() const
PGSSolverResultTpl & operator=(const PGSSolverResultTpl &other)
Assignment operator.
VectorXsStorage y_storage
Dual solution.
std::size_t problem_size
Size of primal/dual variables.
void retrievePrimalSolution(const Eigen::MatrixBase< VectorLike > &primal_solution_) const
Retrieve primal solution.
void retrieveConstraintVelocitiesImpl(const Eigen::MatrixBase< VectorLike > &constraint_velocities_) const
VectorXsStorage m_impulse_guess_storage
Storage for the optional impulse guess.
Settings for the PGS constraint solver loop.
PGSSolverSettingsTpl(std::size_t max_iterations=1000, Scalar absolute_feasibility_tol=Scalar(1e-6), Scalar relative_feasibility_tol=Scalar(1e-6), Scalar absolute_complementarity_tol=Scalar(1e-6), Scalar relative_complementarity_tol=Scalar(1e-6), bool solve_ncp=true, bool measure_timings=false, bool stat_record=false, Scalar over_relaxation=Scalar(1))
Default constructor.
Scalar over_relaxation
Over-relaxation of PGS step. Default value is 1.
PGSSolverStatsTpl(std::size_t max_iterations)
Constructor given a maximum iteration of the solver.
PGSSolverStatsTpl()
Default constructor.