| 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 | #define SCALAR_float32 | ||
| 16 | |||
| 17 | namespace crocoddyl { | ||
| 18 | namespace python { | ||
| 19 | |||
| 20 | ✗ | void exposeSolverBoxQP() { | |
| 21 | #ifdef SCALAR_float64 | ||
| 22 | bp::register_ptr_to_python<std::shared_ptr<BoxQPSolution> >(); | ||
| 23 | |||
| 24 | bp::class_<BoxQPSolution>( | ||
| 25 | "BoxQPSolution", "Solution data of the box QP.\n\n", | ||
| 26 | bp::init<Eigen::MatrixXd, Eigen::VectorXd, std::vector<size_t>, | ||
| 27 | std::vector<size_t> >( | ||
| 28 | bp::args("self", "Hff_inv", "x", "free_idx", "clamped_idx"), | ||
| 29 | "Initialize the data for the box-QP solution.\n\n" | ||
| 30 | ":param Hff_inv: inverse of the free Hessian\n" | ||
| 31 | ":param x: decision variable\n" | ||
| 32 | ":param free_idx: free indexes\n" | ||
| 33 | ":param clamped_idx: clamped indexes")) | ||
| 34 | .add_property("Hff_inv", | ||
| 35 | bp::make_getter(&BoxQPSolution::Hff_inv, | ||
| 36 | bp::return_internal_reference<>()), | ||
| 37 | bp::make_setter(&BoxQPSolution::Hff_inv), | ||
| 38 | "inverse of the free Hessian matrix") | ||
| 39 | .add_property( | ||
| 40 | "x", | ||
| 41 | bp::make_getter(&BoxQPSolution::x, bp::return_internal_reference<>()), | ||
| 42 | bp::make_setter(&BoxQPSolution::x), "decision variable") | ||
| 43 | .add_property( | ||
| 44 | "free_idx", | ||
| 45 | bp::make_getter(&BoxQPSolution::free_idx, | ||
| 46 | bp::return_value_policy<bp::return_by_value>()), | ||
| 47 | bp::make_setter(&BoxQPSolution::free_idx), "free indexes") | ||
| 48 | .add_property( | ||
| 49 | "clamped_idx", | ||
| 50 | bp::make_getter(&BoxQPSolution::clamped_idx, | ||
| 51 | bp::return_value_policy<bp::return_by_value>()), | ||
| 52 | bp::make_setter(&BoxQPSolution::clamped_idx), "clamped indexes") | ||
| 53 | .def(CopyableVisitor<BoxQPSolution>()); | ||
| 54 | |||
| 55 | bp::register_ptr_to_python<std::shared_ptr<BoxQP> >(); | ||
| 56 | |||
| 57 | bp::class_<BoxQP>( | ||
| 58 | "BoxQP", | ||
| 59 | "Projected-Newton QP for only bound constraints.\n\n" | ||
| 60 | "It solves a QP problem with bound constraints of the form:\n" | ||
| 61 | " x = argmin 0.5 x^T H x + q^T x\n" | ||
| 62 | " subject to: lb <= x <= ub" | ||
| 63 | "where nx is the number of decision variables.", | ||
| 64 | bp::init<std::size_t, bp::optional<std::size_t, double, double, double> >( | ||
| 65 | bp::args("self", "nx", "maxiter", "th_acceptstep", "th_grad", "reg"), | ||
| 66 | "Initialize the Projected-Newton QP for bound constraints.\n\n" | ||
| 67 | ":param nx: dimension of the decision vector\n" | ||
| 68 | ":param maxiter: maximum number of allowed iterations (default 100)\n" | ||
| 69 | ":param th_acceptstep: acceptance step condition (default 0.1)\n" | ||
| 70 | ":param th_grad: gradient tolerance condition (default 1e-9)\n" | ||
| 71 | ":param reg: regularization (default 1e-9)")) | ||
| 72 | .def("solve", &BoxQP::solve, | ||
| 73 | bp::return_value_policy<bp::return_by_value>(), | ||
| 74 | bp::args("H", "q", "lb", "ub", "xinit"), | ||
| 75 | "Compute the solution of bound-constrained QP based on Newton " | ||
| 76 | "projection.\n\n" | ||
| 77 | ":param H: Hessian (dimension nx * nx)\n" | ||
| 78 | ":param q: gradient (dimension nx)\n" | ||
| 79 | ":param lb: lower bound (dimension nx)\n" | ||
| 80 | ":param ub: upper bound (dimension nx)\n" | ||
| 81 | ":param xinit: initial guess") | ||
| 82 | .add_property("solution", | ||
| 83 | bp::make_function( | ||
| 84 | &BoxQP::get_solution, | ||
| 85 | bp::return_value_policy<bp::copy_const_reference>()), | ||
| 86 | "QP solution.") | ||
| 87 | .add_property("nx", bp::make_function(&BoxQP::get_nx), | ||
| 88 | bp::make_function(&BoxQP::set_nx), | ||
| 89 | "dimension of the decision vector.") | ||
| 90 | .add_property("maxIter", bp::make_function(&BoxQP::get_maxiter), | ||
| 91 | bp::make_function(&BoxQP::set_maxiter), | ||
| 92 | "maximum number of allowed iterations.") | ||
| 93 | .add_property("maxiter", | ||
| 94 | bp::make_function(&BoxQP::get_maxiter, | ||
| 95 | deprecated<>("Deprecated. Use maxIter")), | ||
| 96 | bp::make_function(&BoxQP::set_maxiter, | ||
| 97 | deprecated<>("Deprecated. Use maxIter")), | ||
| 98 | "maximum number of allowed iterations.") | ||
| 99 | .add_property("th_acceptStep", | ||
| 100 | bp::make_function(&BoxQP::get_th_acceptstep), | ||
| 101 | bp::make_function(&BoxQP::set_th_acceptstep), | ||
| 102 | "acceptable reduction ration.") | ||
| 103 | .add_property("th_grad", bp::make_function(&BoxQP::get_th_grad), | ||
| 104 | bp::make_function(&BoxQP::set_th_grad), | ||
| 105 | "convergence tolerance.") | ||
| 106 | .add_property("reg", bp::make_function(&BoxQP::get_reg), | ||
| 107 | bp::make_function(&BoxQP::set_reg), "regularization value.") | ||
| 108 | .add_property("alphas", | ||
| 109 | bp::make_function( | ||
| 110 | &BoxQP::get_alphas, | ||
| 111 | bp::return_value_policy<bp::copy_const_reference>()), | ||
| 112 | bp::make_function(&BoxQP::set_alphas), | ||
| 113 | "list of step length (alpha) values") | ||
| 114 | .def(CopyableVisitor<BoxQP>()); | ||
| 115 | #endif | ||
| 116 | ✗ | } | |
| 117 | |||
| 118 | } // namespace python | ||
| 119 | } // namespace crocoddyl | ||
| 120 |