Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/core/solvers/box-qp.cpp |
Date: | 2025-01-16 08:47:40 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 48 | 53 | 90.6% |
Branches: | 47 | 94 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2020-2023, University of Edinburgh, Heriot-Watt University | ||
5 | // Copyright note valid unless otherwise stated in individual files. | ||
6 | // All rights reserved. | ||
7 | /////////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | #include "crocoddyl/core/solvers/box-qp.hpp" | ||
10 | |||
11 | #include "python/crocoddyl/core/core.hpp" | ||
12 | #include "python/crocoddyl/utils/copyable.hpp" | ||
13 | #include "python/crocoddyl/utils/deprecate.hpp" | ||
14 | |||
15 | namespace crocoddyl { | ||
16 | namespace python { | ||
17 | |||
18 | 10 | void exposeSolverBoxQP() { | |
19 | 10 | bp::register_ptr_to_python<boost::shared_ptr<BoxQPSolution> >(); | |
20 | |||
21 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<BoxQPSolution>( |
22 | "BoxQPSolution", "Solution data of the box QP.\n\n", | ||
23 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::init<Eigen::MatrixXd, Eigen::VectorXd, std::vector<size_t>, |
24 | std::vector<size_t> >( | ||
25 | 20 | bp::args("self", "Hff_inv", "x", "free_idx", "clamped_idx"), | |
26 | "Initialize the data for the box-QP solution.\n\n" | ||
27 | ":param Hff_inv: inverse of the free Hessian\n" | ||
28 | ":param x: decision variable\n" | ||
29 | ":param free_idx: free indexes\n" | ||
30 | ":param clamped_idx: clamped indexes")) | ||
31 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Hff_inv", |
32 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&BoxQPSolution::Hff_inv, |
33 | ✗ | bp::return_internal_reference<>()), | |
34 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&BoxQPSolution::Hff_inv), |
35 | "inverse of the free Hessian matrix") | ||
36 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
37 | "x", | ||
38 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&BoxQPSolution::x, bp::return_internal_reference<>()), |
39 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&BoxQPSolution::x), "decision variable") |
40 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
41 | "free_idx", | ||
42 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&BoxQPSolution::free_idx, |
43 | ✗ | bp::return_value_policy<bp::return_by_value>()), | |
44 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&BoxQPSolution::free_idx), "free indexes") |
45 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
46 | "clamped_idx", | ||
47 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&BoxQPSolution::clamped_idx, |
48 | ✗ | bp::return_value_policy<bp::return_by_value>()), | |
49 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&BoxQPSolution::clamped_idx), "clamped indexes") |
50 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(CopyableVisitor<BoxQPSolution>()); |
51 | |||
52 | 10 | bp::register_ptr_to_python<boost::shared_ptr<BoxQP> >(); | |
53 | |||
54 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<BoxQP>( |
55 | "BoxQP", | ||
56 | "Projected-Newton QP for only bound constraints.\n\n" | ||
57 | "It solves a QP problem with bound constraints of the form:\n" | ||
58 | " x = argmin 0.5 x^T H x + q^T x\n" | ||
59 | " subject to: lb <= x <= ub" | ||
60 | "where nx is the number of decision variables.", | ||
61 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::init<std::size_t, bp::optional<std::size_t, double, double, double> >( |
62 | 20 | bp::args("self", "nx", "maxiter", "th_acceptstep", "th_grad", "reg"), | |
63 | "Initialize the Projected-Newton QP for bound constraints.\n\n" | ||
64 | ":param nx: dimension of the decision vector\n" | ||
65 | ":param maxiter: maximum number of allowed iterations (default 100)\n" | ||
66 | ":param th_acceptstep: acceptance step condition (default 0.1)\n" | ||
67 | ":param th_grad: gradient tolerance condition (default 1e-9)\n" | ||
68 | ":param reg: regularization (default 1e-9)")) | ||
69 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def("solve", &BoxQP::solve, |
70 | ✗ | bp::return_value_policy<bp::return_by_value>(), | |
71 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("H", "q", "lb", "ub", "xinit"), |
72 | "Compute the solution of bound-constrained QP based on Newton " | ||
73 | "projection.\n\n" | ||
74 | ":param H: Hessian (dimension nx * nx)\n" | ||
75 | ":param q: gradient (dimension nx)\n" | ||
76 | ":param lb: lower bound (dimension nx)\n" | ||
77 | ":param ub: upper bound (dimension nx)\n" | ||
78 | ":param xinit: initial guess") | ||
79 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("solution", |
80 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function( |
81 | &BoxQP::get_solution, | ||
82 | 10 | bp::return_value_policy<bp::copy_const_reference>()), | |
83 | "QP solution.") | ||
84 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | .add_property("nx", bp::make_function(&BoxQP::get_nx), |
85 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&BoxQP::set_nx), |
86 | "dimension of the decision vector.") | ||
87 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | .add_property("maxIter", bp::make_function(&BoxQP::get_maxiter), |
88 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&BoxQP::set_maxiter), |
89 | "maximum number of allowed iterations.") | ||
90 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("maxiter", |
91 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&BoxQP::get_maxiter, |
92 |
2/4✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
|
20 | deprecated<>("Deprecated. Use maxIter")), |
93 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&BoxQP::set_maxiter, |
94 |
2/4✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
|
20 | deprecated<>("Deprecated. Use maxIter")), |
95 | "maximum number of allowed iterations.") | ||
96 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("th_acceptStep", |
97 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&BoxQP::get_th_acceptstep), |
98 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&BoxQP::set_th_acceptstep), |
99 | "acceptable reduction ration.") | ||
100 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | .add_property("th_grad", bp::make_function(&BoxQP::get_th_grad), |
101 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&BoxQP::set_th_grad), |
102 | "convergence tolerance.") | ||
103 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | .add_property("reg", bp::make_function(&BoxQP::get_reg), |
104 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&BoxQP::set_reg), "regularization value.") |
105 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("alphas", |
106 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function( |
107 | &BoxQP::get_alphas, | ||
108 | ✗ | bp::return_value_policy<bp::copy_const_reference>()), | |
109 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&BoxQP::set_alphas), |
110 | "list of step length (alpha) values") | ||
111 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(CopyableVisitor<BoxQP>()); |
112 | 10 | } | |
113 | |||
114 | } // namespace python | ||
115 | } // namespace crocoddyl | ||
116 |