pinocchio 4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
constraint-solver-base.hpp
1//
2// Copyright (c) 2022-2024 INRIA
3//
4
5#pragma once
6
7#include "pinocchio/bindings/python/fwd.hpp"
8#include "pinocchio/algorithm/solvers/constraint-solver-base.hpp"
9
10#include <eigenpy/eigen-from-python.hpp>
11#include <eigenpy/eigen-to-python.hpp>
12
13namespace pinocchio
14{
15 namespace python
16 {
17
18 namespace bp = boost::python;
19
20 // ============================================================================
21 // ConstraintSolverSettingsBasePythonVisitor
22 // Injects base settings fields and methods into a Python class.
23 // ============================================================================
24
25 template<typename Settings>
27 : public boost::python::def_visitor<ConstraintSolverSettingsBasePythonVisitor<Settings>>
28 {
29 template<class PyClass>
30 void visit(PyClass & cl) const
31 {
32 cl.def_readwrite(
33 "max_iterations", &Settings::max_iterations, "Maximum number of solver iterations.")
34 .def_readwrite(
35 "absolute_feasibility_tol", &Settings::absolute_feasibility_tol,
36 "Absolute tolerance on primal/dual feasibility.")
37 .def_readwrite(
38 "relative_feasibility_tol", &Settings::relative_feasibility_tol,
39 "Relative tolerance on primal/dual feasibility.")
40 .def_readwrite(
41 "absolute_complementarity_tol", &Settings::absolute_complementarity_tol,
42 "Absolute tolerance on complementarity (duality gap).")
43 .def_readwrite(
44 "relative_complementarity_tol", &Settings::relative_complementarity_tol,
45 "Relative tolerance on complementarity (duality gap).")
46 .def_readwrite(
47 "solve_ncp", &Settings::solve_ncp,
48 "Whether to solve the NCP. If false, the equivalent CCP is solved.")
49 .def_readwrite(
50 "measure_timings", &Settings::measure_timings, "Whether to measure solve timings.")
51 .def_readwrite(
52 "stat_record", &Settings::stat_record, "Whether to record per-iteration stats.")
53 .def(
54 "checkValidity", &Settings::checkValidity, bp::arg("self"),
55 "Throw if the settings are not valid.");
56 }
57 }; // struct ConstraintSolverSettingsBasePythonVisitor
58
59 // ============================================================================
60 // ConstraintSolverResultBasePythonVisitor
61 // Injects base result fields and methods into a Python class.
62 // ============================================================================
63
64 template<typename Result>
66 : public boost::python::def_visitor<ConstraintSolverResultBasePythonVisitor<Result>>
67 {
68 typedef typename Result::Scalar Scalar;
69 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Result::Options> VectorXs;
70
71 static VectorXs retrieveConstraintImpulses_wrapper(const Result & self)
72 {
73 VectorXs out(static_cast<Eigen::Index>(self.constraintSize()));
74 self.retrieveConstraintImpulses(out);
75 return out;
76 }
77
78 static VectorXs retrieveConstraintVelocities_wrapper(const Result & self)
79 {
80 VectorXs out(static_cast<Eigen::Index>(self.constraintSize()));
81 self.retrieveConstraintVelocities(out);
82 return out;
83 }
84
85 static void setConstraintImpulseGuess_wrapper(Result & self, const VectorXs & impulse_guess)
86 {
87 self.setConstraintImpulseGuess(impulse_guess);
88 }
89
90 static void setConstraintVelocityGuess_wrapper(Result & self, const VectorXs & velocity_guess)
91 {
92 self.setConstraintVelocityGuess(velocity_guess);
93 }
94
95 template<class PyClass>
96 void visit(PyClass & cl) const
97 {
98 cl.def_readwrite("iterations", &Result::iterations, "Number of solver iterations.")
99 .def_readwrite("converged", &Result::converged, "Whether the solver has converged.")
100 .def_readonly(
101 "primal_feasibility", &Result::primal_feasibility, "Value of the primal feasibility.")
102 .def_readonly(
103 "dual_feasibility", &Result::dual_feasibility, "Value of the dual feasibility.")
104 .def_readonly(
105 "complementarity", &Result::complementarity, "Value of the complementarity.")
106 .def(
107 "isValid", &Result::isValid, bp::arg("self"),
108 "Return True if the result represents a valid (post-solve) state.")
109 .def(
110 "constraintSize", &Result::constraintSize, bp::arg("self"),
111 "Size of quantities related to constraints contained in result (typically constraint "
112 "impulses or velocities).")
113 .def(
114 "reset", &Result::reset, bp::arg("self"),
115 "Reset the result to an invalid (pre-solve) state.")
116 .def(
117 "retrieveConstraintImpulses", retrieveConstraintImpulses_wrapper, bp::arg("self"),
118 "Return the primal solution (constraint impulses) as a new vector.")
119 .def(
120 "retrieveConstraintVelocities", retrieveConstraintVelocities_wrapper, bp::arg("self"),
121 "Return the dual solution (constraint velocities) as a new vector.")
122 .def(
123 "setConstraintImpulseGuess", setConstraintImpulseGuess_wrapper,
124 bp::args("self", "impulse_guess"),
125 "Set the constraint impulse warmstart guess for the next solve call.")
126 .def(
127 "clearConstraintImpulseGuess", &Result::clearConstraintImpulseGuess, bp::arg("self"),
128 "Clear the constraint impulse warmstart guess.")
129 .def(
130 "setConstraintVelocityGuess", setConstraintVelocityGuess_wrapper,
131 bp::args("self", "velocity_guess"),
132 "Set the constraint velocity warmstart guess for the next solve call.")
133 .def(
134 "clearConstraintVelocityGuess", &Result::clearConstraintVelocityGuess, bp::arg("self"),
135 "Clear the constraint velocity warmstart guess.");
136 }
137 }; // struct ConstraintSolverResultBasePythonVisitor
138
139 // ============================================================================
140 // ConstraintSolverStatsBasePythonVisitor
141 // Injects base stats fields and methods into a Python class.
142 // ============================================================================
143
144 template<typename Stats>
146 : public boost::python::def_visitor<ConstraintSolverStatsBasePythonVisitor<Stats>>
147 {
148 template<class PyClass>
149 void visit(PyClass & cl) const
150 {
151 cl.def_readwrite("iterations", &Stats::iterations, "Total number of iterations tracked.")
152 .def_readonly(
153 "primal_feasibility", &Stats::primal_feasibility,
154 "Per-iteration history of primal feasibility values.")
155 .def_readonly(
156 "dual_feasibility", &Stats::dual_feasibility,
157 "Per-iteration history of dual feasibility values.")
158 .def_readonly(
159 "dual_feasibility_ncp", &Stats::dual_feasibility_ncp,
160 "Per-iteration history of NCP dual feasibility values.")
161 .def_readonly(
162 "complementarity", &Stats::complementarity,
163 "Per-iteration history of complementarity values.")
164 .def(
165 "size", &Stats::size, bp::arg("self"),
166 "Return the number of iterations tracked (size of history vectors).")
167 .def(
168 "reserve", &Stats::reserve, bp::args("self", "max_iterations"),
169 "Reserve storage for up to max_iterations iterations.")
170 .def(
171 "reset", &Stats::reset, bp::arg("self"),
172 "Clear all history and reset the iteration counter to zero.");
173 }
174 }; // struct ConstraintSolverStatsBasePythonVisitor
175
176 // ============================================================================
177 // ConstraintSolverBasePythonVisitor
178 // Injects base solver methods into a Python class.
179 // ============================================================================
180
181 template<typename Solver>
183 : public boost::python::def_visitor<ConstraintSolverBasePythonVisitor<Solver>>
184 {
185 template<class PyClass>
186 void visit(PyClass & cl) const
187 {
188 cl.def("reset", &Solver::reset, bp::arg("self"), "Reset the solver to its initial state.");
189
190 cl.def(
191 "getElapsedTime", &Solver::getElapsedTime, bp::arg("self"),
192 "Return last solve call elapsed time in microseconds.");
193 }
194 }; // struct ConstraintSolverBasePythonVisitor
195
196 } // namespace python
197} // namespace pinocchio
Main pinocchio namespace.
Definition treeview.dox:11