admm-solver.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2024 INRIA
3 //
4 
5 #include <eigenpy/memory.hpp>
8 
10 
15 
19 
20 namespace pinocchio
21 {
22  namespace python
23  {
24  namespace bp = boost::python;
25 
26  typedef ADMMContactSolverTpl<context::Scalar> Solver;
31  typedef const Eigen::Ref<const VectorXs> ConstRefVectorXs;
34 
35 #ifdef PINOCCHIO_PYTHON_PLAIN_SCALAR_TYPE
36 
37  template<typename DelassusDerived>
38  static bool solve_wrapper(
39  Solver & solver,
40  DelassusDerived & delassus,
41  const context::VectorXs & g,
42  const context::CoulombFrictionConeVector & cones,
43  const context::VectorXs & R,
44  const boost::optional<ConstRefVectorXs> primal_solution = boost::none,
45  const boost::optional<ConstRefVectorXs> dual_solution = boost::none,
46  bool compute_largest_eigen_values = true,
47  bool stat_record = false)
48  {
49  return solver.solve(
50  delassus, g, cones, R, primal_solution, dual_solution, compute_largest_eigen_values,
51  stat_record);
52  }
53 
54  template<typename DelassusDerived>
55  static bool solve_wrapper2(
56  Solver & solver,
57  DelassusDerived & delassus,
58  const context::VectorXs & g,
59  const context::CoulombFrictionConeVector & cones,
60  Eigen::Ref<context::VectorXs> x)
61  {
62  return solver.solve(delassus, g, cones, x);
63  }
64 #endif
65 
66 #ifndef PINOCCHIO_PYTHON_SKIP_CASADI_UNSUPPORTED
67 
69  const context::CoulombFrictionConeVector & cones, const context::VectorXs & forces)
70  {
71  context::VectorXs res(forces.size());
73  return res;
74  }
75 
77  const context::CoulombFrictionConeVector & cones, const context::VectorXs & velocities)
78  {
79  context::VectorXs res(velocities.size());
81  return res;
82  }
83 
85  const context::CoulombFrictionConeVector & cones, const context::VectorXs & forces)
86  {
88  }
89 
91  const context::CoulombFrictionConeVector & cones,
92  const context::VectorXs & forces,
93  const context::VectorXs & velocities)
94  {
96  }
97 
99  const context::CoulombFrictionConeVector & cones, const context::VectorXs & velocities)
100  {
101  context::VectorXs res(velocities.size());
103  return res;
104  }
105 #endif // PINOCCHIO_PYTHON_SKIP_CASADI_UNSUPPORTED
106 
108  {
109 #ifdef PINOCCHIO_PYTHON_PLAIN_SCALAR_TYPE
110  bp::class_<Solver> cl(
111  "ADMMContactSolver",
112  "Alternating Direction Method of Multi-pliers solver for contact dynamics.",
113  bp::init<int, Scalar, Scalar, Scalar, Scalar, Scalar, int>(
114  (bp::arg("self"), bp::arg("problem_dim"), bp::arg("mu_prox") = Scalar(1e-6),
115  bp::arg("tau") = Scalar(0.5), bp::arg("rho_power") = Scalar(0.2),
116  bp::arg("rho_power_factor") = Scalar(0.05), bp::arg("ratio_primal_dual") = Scalar(10),
117  bp::arg("max_it_largest_eigen_value_solver") = 20),
118  "Default constructor."));
120 
121  .def(
122  "solve", solve_wrapper<ContactCholeskyDecomposition::DelassusCholeskyExpression>,
123  (bp::args("self", "delassus", "g", "cones", "R"),
124  bp::arg("primal_solution") = boost::none, bp::arg("dual_solution") = boost::none,
125  bp::arg("compute_largest_eigen_values") = true, bp::arg("stat_record") = false),
126  "Solve the constrained conic problem, starting from the optional initial guess.")
127  .def(
128  "solve", solve_wrapper<context::DelassusOperatorDense>,
129  (bp::args("self", "delassus", "g", "cones", "R"),
130  bp::arg("primal_solution") = boost::none, bp::arg("dual_solution") = boost::none,
131  bp::arg("compute_largest_eigen_values") = true, bp::arg("stat_record") = false),
132  "Solve the constrained conic problem, starting from the optional initial guess.")
133  .def(
134  "solve", solve_wrapper<context::DelassusOperatorSparse>,
135  (bp::args("self", "delassus", "g", "cones", "R"),
136  bp::arg("primal_solution") = boost::none, bp::arg("dual_solution") = boost::none,
137  bp::arg("compute_largest_eigen_values") = true, bp::arg("stat_record") = false),
138  "Solve the constrained conic problem, starting from the optional initial guess.")
139 
140  .def("setRho", &Solver::setRho, bp::args("self", "rho"), "Set the ADMM penalty value.")
141  .def("getRho", &Solver::getRho, bp::arg("self"), "Get the ADMM penalty value.")
142 
143  .def(
144  "setRhoPower", &Solver::setRhoPower, bp::args("self", "rho_power"),
145  "Set the power associated to the problem conditionning.")
146  .def(
147  "getRhoPower", &Solver::getRhoPower, bp::arg("self"),
148  "Get the power associated to the problem conditionning.")
149 
150  .def(
151  "setRhoPowerFactor", &Solver::setRhoPowerFactor, bp::args("self", "rho_power_factor"),
152  "Set the power factor associated to the problem conditionning.")
153  .def(
154  "getRhoPowerFactor", &Solver::getRhoPowerFactor, bp::arg("self"),
155  "Get the power factor associated to the problem conditionning.")
156 
157  .def(
158  "setTau", &Solver::setTau, bp::args("self", "tau"), "Set the tau linear scaling factor.")
159  .def("getTau", &Solver::getTau, bp::arg("self"), "Get the tau linear scaling factor.")
160 
161  .def(
162  "setProximalValue", &Solver::setProximalValue, bp::args("self", "mu"),
163  "Set the proximal value.")
164  .def(
165  "getProximalValue", &Solver::getProximalValue, bp::arg("self"), "Get the proximal value.")
166 
167  .def(
168  "setRatioPrimalDual", &Solver::setRatioPrimalDual, bp::args("self", "ratio_primal_dual"),
169  "Set the primal/dual ratio.")
170  .def(
171  "getRatioPrimalDual", &Solver::getRatioPrimalDual, bp::arg("self"),
172  "Get the primal/dual ratio.")
173 
174  .def(
175  "getPrimalSolution", &Solver::getPrimalSolution, bp::arg("self"),
176  "Returns the primal solution of the problem.", bp::return_internal_reference<>())
177 
178  .def(
179  "getDualSolution", &Solver::getDualSolution, bp::arg("self"),
180  "Returns the dual solution of the problem.", bp::return_internal_reference<>())
181 
182  .def(
183  "getCholeskyUpdateCount", &Solver::getCholeskyUpdateCount, bp::arg("self"),
184  "Returns the number of updates of the Cholesky factorization due to rho updates.")
185 
186  .def(
187  "computeRho", &Solver::computeRho, bp::args("L", "m", "rho_power"),
188  "Compute the penalty ADMM value from the current largest and lowest eigenvalues and "
189  "the scaling spectral factor.")
190  .staticmethod("computeRho")
191  .def(
192  "computeRhoPower", &Solver::computeRhoPower, bp::args("L", "m", "rho"),
193  "Compute the scaling spectral factor of the ADMM penalty term from the current "
194  "largest and lowest eigenvalues and the ADMM penalty term.")
195  .staticmethod("computeRhoPower")
196 
197  .def(
198  "getPowerIterationAlgo", &Solver::getPowerIterationAlgo, bp::arg("self"),
199  bp::return_internal_reference<>())
200 
201  .def("getStats", &Solver::getStats, bp::arg("self"), bp::return_internal_reference<>());
202 
203  #ifdef PINOCCHIO_WITH_ACCELERATE_SUPPORT
204  {
205  typedef Eigen::AccelerateLLT<context::SparseMatrix> AccelerateLLT;
207  DelassusOperatorSparseAccelerate;
208  cl.def(
209  "solve", solve_wrapper<DelassusOperatorSparseAccelerate>,
210  (bp::args("self", "delassus", "g", "cones", "R"),
211  bp::arg("primal_solution") = boost::none, bp::arg("dual_solution") = boost::none,
212  bp::arg("compute_largest_eigen_values") = true, bp::arg("stat_record") = false),
213  "Solve the constrained conic problem, starting from the optional initial guess.");
214  }
215  #endif
216 
217  bp::def(
218  "computeConeProjection", computeConeProjection_wrapper, bp::args("cones", "forces"),
219  "Project a vector on the cartesian product of cones.");
220 
221  bp::def(
222  "computeDualConeProjection", computeDualConeProjection_wrapper,
223  bp::args("cones", "velocities"),
224  "Project a vector on the cartesian product of dual cones.");
225 
226  bp::def(
227  "computePrimalFeasibility", computePrimalFeasibility_wrapper, bp::args("cones", "forces"),
228  "Compute the primal feasibility.");
229 
230  bp::def(
231  "computeReprojectionError", computeReprojectionError_wrapper,
232  bp::args("cones", "forces", "velocities"), "Compute the reprojection error.");
233 
234  bp::def(
235  "computeComplementarityShift", computeComplementarityShift_wrapper,
236  bp::args("cones", "velocities"),
237  "Compute the complementarity shift associated to the De SaxĂ© function.");
238 
239  {
240  bp::class_<SolverStats>(
241  "SolverStats", "",
242  bp::init<int>((bp::arg("self"), bp::arg("max_it")), "Default constructor"))
243  .def("reset", &SolverStats::reset, bp::arg("self"), "Reset the stasts.")
244  .def(
245  "size", &SolverStats::size, bp::arg("self"),
246  "Size of the vectors stored in the structure.")
247 
248  .PINOCCHIO_ADD_PROPERTY_READONLY(SolverStats, primal_feasibility, "")
249  .PINOCCHIO_ADD_PROPERTY_READONLY(SolverStats, dual_feasibility, "")
250  .PINOCCHIO_ADD_PROPERTY_READONLY(SolverStats, dual_feasibility_ncp, "")
251  .PINOCCHIO_ADD_PROPERTY_READONLY(SolverStats, complementarity, "")
252  .PINOCCHIO_ADD_PROPERTY_READONLY(SolverStats, rho, "")
253  .PINOCCHIO_ADD_PROPERTY_READONLY(
254  SolverStats, it, "Number of iterations performed by the algorithm.")
255  .PINOCCHIO_ADD_PROPERTY_READONLY(
256  SolverStats, cholesky_update_count,
257  "Number of Cholesky updates performed by the algorithm.");
258  }
259 
260  {
262  bp::class_<PowerIterationAlgo>(
263  "PowerIterationAlgo", "",
264  bp::init<Eigen::DenseIndex, int, Scalar>(
265  (bp::arg("self"), bp::arg("size"), bp::arg("max_it") = 10, bp::arg("rel_tol") = 1e-8),
266  "Default constructor"))
267  .def("run", &PowerIterationAlgo::run<context::MatrixXs>, bp::arg("self"), "")
268  .def(
269  "lowest", &PowerIterationAlgo::lowest<context::MatrixXs>,
270  (bp::arg("self"), bp::arg("compute_largest") = true), "")
271  .def("reset", &PowerIterationAlgo::reset, bp::arg("self"))
272 
273  .PINOCCHIO_ADD_PROPERTY(PowerIterationAlgo, principal_eigen_vector, "")
274  .PINOCCHIO_ADD_PROPERTY(PowerIterationAlgo, lowest_eigen_vector, "")
275  .PINOCCHIO_ADD_PROPERTY(PowerIterationAlgo, max_it, "")
276  .PINOCCHIO_ADD_PROPERTY(PowerIterationAlgo, rel_tol, "")
277  .PINOCCHIO_ADD_PROPERTY_READONLY(PowerIterationAlgo, largest_eigen_value, "")
278  .PINOCCHIO_ADD_PROPERTY_READONLY(PowerIterationAlgo, lowest_eigen_value, "")
279  .PINOCCHIO_ADD_PROPERTY_READONLY(PowerIterationAlgo, it, "")
280  .PINOCCHIO_ADD_PROPERTY_READONLY(PowerIterationAlgo, convergence_criteria, "");
281  }
282 #endif
283  }
284 
285  } // namespace python
286 } // namespace pinocchio
contact-solver-base.hpp
pinocchio::python::ContactSolverBasePythonVisitor
Definition: bindings/python/algorithm/contact-solver-base.hpp:18
boost::python
pinocchio::python::ContactCholeskyDecomposition
ContactCholeskyDecompositionTpl< context::Scalar, context::Options > ContactCholeskyDecomposition
Definition: admm-solver.cpp:33
contact-cholesky.hpp
pinocchio::internal::computeDualConeProjection
void computeDualConeProjection(const std::vector< CoulombFrictionConeTpl< Scalar >, ConstraintAllocator > &cones, const Eigen::DenseBase< VectorLikeIn > &x, const Eigen::DenseBase< VectorLikeOut > &x_proj_)
Project a vector x on the dual of the cones contained in the vector of cones.
Definition: contact-solver-utils.hpp:47
delassus-operator-sparse.hpp
pinocchio::python::computePrimalFeasibility_wrapper
static context::Scalar computePrimalFeasibility_wrapper(const context::CoulombFrictionConeVector &cones, const context::VectorXs &forces)
Definition: admm-solver.cpp:84
pinocchio::internal::computeComplementarityShift
void computeComplementarityShift(const std::vector< CoulombFrictionConeTpl< Scalar >, ConstraintAllocator > &cones, const Eigen::DenseBase< VectorLikeIn > &velocities, const Eigen::DenseBase< VectorLikeOut > &shift_)
Definition: contact-solver-utils.hpp:92
eigen-from-python.hpp
pinocchio::python::PowerIterationAlgo
Solver::PowerIterationAlgo PowerIterationAlgo
Definition: admm-solver.cpp:27
pinocchio::DelassusOperatorSparseTpl
Definition: delassus-operator-sparse.hpp:127
pinocchio::python::Scalar
context::Scalar Scalar
Definition: admm-solver.cpp:29
eigen-to-python.hpp
pinocchio::python::ConstRefVectorXs
const typedef Eigen::Ref< const VectorXs > ConstRefVectorXs
Definition: admm-solver.cpp:31
pinocchio::internal::computeReprojectionError
Scalar computeReprojectionError(const std::vector< CoulombFrictionConeTpl< Scalar >, ConstraintAllocator > &cones, const Eigen::DenseBase< ForceVector > &forces, const Eigen::DenseBase< VelocityVector > &velocities)
Definition: contact-solver-utils.hpp:134
R
R
pinocchio::python::VectorXs
context::VectorXs VectorXs
Definition: admm-solver.cpp:30
pinocchio::python::computeDualConeProjection_wrapper
static context::VectorXs computeDualConeProjection_wrapper(const context::CoulombFrictionConeVector &cones, const context::VectorXs &velocities)
Definition: admm-solver.cpp:76
pinocchio::context::VectorXs
Eigen::Matrix< Scalar, Eigen::Dynamic, 1, Options > VectorXs
Definition: context/generic.hpp:47
pinocchio::PowerIterationAlgoTpl
Compute the largest eigenvalues and the associated principle eigenvector via power iteration.
Definition: eigenvalues.hpp:17
python
pinocchio::python::Solver
ADMMContactSolverTpl< context::Scalar > Solver
Definition: admm-solver.cpp:26
size
FCL_REAL size() const
pinocchio::internal::computeConeProjection
void computeConeProjection(const std::vector< CoulombFrictionConeTpl< Scalar >, ConstraintAllocator > &cones, const Eigen::DenseBase< VectorLikeIn > &x, const Eigen::DenseBase< VectorLikeOut > &x_proj_)
Project a vector x on the vector of cones.
Definition: contact-solver-utils.hpp:25
pinocchio::python::exposeADMMContactSolver
void exposeADMMContactSolver()
Definition: admm-solver.cpp:107
macros.hpp
pinocchio::internal::computePrimalFeasibility
Scalar computePrimalFeasibility(const std::vector< CoulombFrictionConeTpl< Scalar >, ConstraintAllocator > &cones, const Eigen::DenseBase< VectorLikeIn > &forces)
Definition: contact-solver-utils.hpp:109
admm-solver.hpp
pinocchio::python::SolverStats
Solver::SolverStats SolverStats
Definition: admm-solver.cpp:28
delassus-operator-dense.hpp
cl
cl
memory.hpp
fwd.hpp
std-vector.hpp
pinocchio::python::computeReprojectionError_wrapper
static context::Scalar computeReprojectionError_wrapper(const context::CoulombFrictionConeVector &cones, const context::VectorXs &forces, const context::VectorXs &velocities)
Definition: admm-solver.cpp:90
pinocchio::ContactCholeskyDecompositionTpl< context::Scalar, context::Options >
pinocchio::python::computeComplementarityShift_wrapper
static context::VectorXs computeComplementarityShift_wrapper(const context::CoulombFrictionConeVector &cones, const context::VectorXs &velocities)
Definition: admm-solver.cpp:98
pinocchio::python::res
ReturnType res
Definition: bindings/python/spatial/explog.hpp:68
pinocchio::python::computeConeProjection_wrapper
static context::VectorXs computeConeProjection_wrapper(const context::CoulombFrictionConeVector &cones, const context::VectorXs &forces)
Definition: admm-solver.cpp:68
pinocchio::python::context::Scalar
PINOCCHIO_PYTHON_SCALAR_TYPE Scalar
Definition: bindings/python/context/generic.hpp:37
simulation-closed-kinematic-chains.rho
int rho
Definition: simulation-closed-kinematic-chains.py:110
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Wed Jun 19 2024 02:41:09