GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/core/solvers/box-qp.cpp Lines: 48 53 90.6 %
Date: 2024-02-13 11:12:33 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
10
  bp::class_<BoxQPSolution>(
22
      "BoxQPSolution", "Solution data of the box QP.\n\n",
23
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
      .add_property("Hff_inv",
32
10
                    bp::make_getter(&BoxQPSolution::Hff_inv,
33
                                    bp::return_internal_reference<>()),
34
20
                    bp::make_setter(&BoxQPSolution::Hff_inv),
35
10
                    "inverse of the free Hessian matrix")
36
      .add_property(
37
          "x",
38
10
          bp::make_getter(&BoxQPSolution::x, bp::return_internal_reference<>()),
39

30
          bp::make_setter(&BoxQPSolution::x), "decision variable")
40
      .add_property(
41
          "free_idx",
42
10
          bp::make_getter(&BoxQPSolution::free_idx,
43
                          bp::return_value_policy<bp::return_by_value>()),
44

30
          bp::make_setter(&BoxQPSolution::free_idx), "free indexes")
45
      .add_property(
46
          "clamped_idx",
47
10
          bp::make_getter(&BoxQPSolution::clamped_idx,
48
                          bp::return_value_policy<bp::return_by_value>()),
49

30
          bp::make_setter(&BoxQPSolution::clamped_idx), "clamped indexes")
50
10
      .def(CopyableVisitor<BoxQPSolution>());
51
52
10
  bp::register_ptr_to_python<boost::shared_ptr<BoxQP> >();
53
54
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
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
      .def("solve", &BoxQP::solve,
70
           bp::return_value_policy<bp::return_by_value>(),
71
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
10
           ":param xinit: initial guess")
79
      .add_property("solution",
80
10
                    bp::make_function(
81
                        &BoxQP::get_solution,
82
10
                        bp::return_value_policy<bp::copy_const_reference>()),
83
10
                    "QP solution.")
84
10
      .add_property("nx", bp::make_function(&BoxQP::get_nx),
85
20
                    bp::make_function(&BoxQP::set_nx),
86
10
                    "dimension of the decision vector.")
87
10
      .add_property("maxIter", bp::make_function(&BoxQP::get_maxiter),
88
20
                    bp::make_function(&BoxQP::set_maxiter),
89
10
                    "maximum number of allowed iterations.")
90
      .add_property("maxiter",
91
10
                    bp::make_function(&BoxQP::get_maxiter,
92

20
                                      deprecated<>("Deprecated. Use maxIter")),
93
20
                    bp::make_function(&BoxQP::set_maxiter,
94

20
                                      deprecated<>("Deprecated. Use maxIter")),
95
10
                    "maximum number of allowed iterations.")
96
      .add_property("th_acceptStep",
97
10
                    bp::make_function(&BoxQP::get_th_acceptstep),
98
20
                    bp::make_function(&BoxQP::set_th_acceptstep),
99
10
                    "acceptable reduction ration.")
100
10
      .add_property("th_grad", bp::make_function(&BoxQP::get_th_grad),
101
20
                    bp::make_function(&BoxQP::set_th_grad),
102
10
                    "convergence tolerance.")
103
10
      .add_property("reg", bp::make_function(&BoxQP::get_reg),
104

30
                    bp::make_function(&BoxQP::set_reg), "regularization value.")
105
      .add_property("alphas",
106
10
                    bp::make_function(
107
                        &BoxQP::get_alphas,
108
                        bp::return_value_policy<bp::copy_const_reference>()),
109
20
                    bp::make_function(&BoxQP::set_alphas),
110
10
                    "list of step length (alpha) values")
111
10
      .def(CopyableVisitor<BoxQP>());
112
10
}
113
114
}  // namespace python
115
}  // namespace crocoddyl