| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/core/activations/quadratic-barrier.cpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 18 | 18 | 100.0% |
| Branches: | 49 | 98 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh | ||
| 5 | // Heriot-Watt University | ||
| 6 | // Copyright note valid unless otherwise stated in individual files. | ||
| 7 | // All rights reserved. | ||
| 8 | /////////////////////////////////////////////////////////////////////////////// | ||
| 9 | |||
| 10 | #include "crocoddyl/core/activations/quadratic-barrier.hpp" | ||
| 11 | |||
| 12 | #include "python/crocoddyl/core/activation-base.hpp" | ||
| 13 | #include "python/crocoddyl/core/core.hpp" | ||
| 14 | |||
| 15 | namespace crocoddyl { | ||
| 16 | namespace python { | ||
| 17 | |||
| 18 | template <typename Bounds> | ||
| 19 | struct ActivationBoundsVisitor | ||
| 20 | : public bp::def_visitor<ActivationBoundsVisitor<Bounds>> { | ||
| 21 | typedef typename Bounds::Scalar Scalar; | ||
| 22 | template <class PyClass> | ||
| 23 | 40 | void visit(PyClass& cl) const { | |
| 24 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
80 | cl.add_property( |
| 25 | "lb", bp::make_getter(&Bounds::lb), | ||
| 26 | 40 | bp::make_setter(&Bounds::lb, bp::return_internal_reference<>()), | |
| 27 | "lower bounds") | ||
| 28 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
80 | .add_property( |
| 29 | "ub", bp::make_getter(&Bounds::ub), | ||
| 30 | 40 | bp::make_setter(&Bounds::lb, bp::return_internal_reference<>()), | |
| 31 | "upper bounds") | ||
| 32 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
40 | .add_property("beta", &Bounds::beta, "beta"); |
| 33 | 40 | } | |
| 34 | }; | ||
| 35 | |||
| 36 | template <typename Model> | ||
| 37 | struct ActivationModelQuadraticVisitor | ||
| 38 | : public bp::def_visitor<ActivationModelQuadraticVisitor<Model>> { | ||
| 39 | typedef typename Model::Scalar Scalar; | ||
| 40 | template <class PyClass> | ||
| 41 | 40 | void visit(PyClass& cl) const { | |
| 42 | 40 | cl.def("calc", &Model::calc, bp::args("self", "data", "r"), | |
| 43 | "Compute the inequality activation.\n\n" | ||
| 44 | ":param data: activation data\n" | ||
| 45 | ":param r: residual vector") | ||
| 46 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("calcDiff", &Model::calcDiff, bp::args("self", "data", "r"), |
| 47 | "Compute the derivatives of inequality activation.\n\n" | ||
| 48 | ":param data: activation data\n" | ||
| 49 | "Note that the Hessian is constant, so we don't write again this " | ||
| 50 | "value. It assumes that calc has been run first.\n" | ||
| 51 | ":param r: residual vector \n") | ||
| 52 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("createData", &Model::createData, bp::args("self"), |
| 53 | "Create the weighted quadratic action data.") | ||
| 54 |
4/8✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 20 times.
✗ Branch 11 not taken.
|
80 | .add_property("bounds", |
| 55 | bp::make_function(&Model::get_bounds, | ||
| 56 | 40 | bp::return_internal_reference<>()), | |
| 57 | bp::make_function(&Model::set_bounds), | ||
| 58 | "bounds (beta, lower and upper bounds)"); | ||
| 59 | 40 | } | |
| 60 | }; | ||
| 61 | |||
| 62 | #define CROCODDYL_ACTIVATION_BOUNDS_PYTHON_BINDINGS(Scalar) \ | ||
| 63 | typedef ActivationBoundsTpl<Scalar> Bounds; \ | ||
| 64 | typedef typename Bounds::VectorXs VectorXs; \ | ||
| 65 | bp::register_ptr_to_python<std::shared_ptr<Bounds>>(); \ | ||
| 66 | bp::class_<Bounds>( \ | ||
| 67 | "ActivationBounds", \ | ||
| 68 | "Activation bounds.\n\n" \ | ||
| 69 | "The activation bounds describe the lower and upper vector plus it " \ | ||
| 70 | "activation range (between 0 and 1), its default value is 1. Note that " \ | ||
| 71 | "a full activation is defined by 1 and the activation range is equally " \ | ||
| 72 | "distributed between the lower and upper bounds.", \ | ||
| 73 | bp::init<VectorXs, VectorXs, bp::optional<Scalar>>( \ | ||
| 74 | bp::args("self", "lb", "ub", "beta"), \ | ||
| 75 | "Initialize the activation bounds.\n\n" \ | ||
| 76 | ":param lb: lower bounds\n" \ | ||
| 77 | ":param ub: upper bounds\n" \ | ||
| 78 | ":param beta: range of activation (between 0 to 1, default 1)")) \ | ||
| 79 | .def(ActivationBoundsVisitor<Bounds>()) \ | ||
| 80 | .def(CastVisitor<Bounds>()) \ | ||
| 81 | .def(PrintableVisitor<Bounds>()) \ | ||
| 82 | .def(CopyableVisitor<Bounds>()); | ||
| 83 | |||
| 84 | #define CROCODDYL_ACTIVATION_MODEL_QUADRATIC_BARRIER_PYTHON_BINDINGS(Scalar) \ | ||
| 85 | typedef ActivationModelQuadraticBarrierTpl<Scalar> Model; \ | ||
| 86 | typedef ActivationModelAbstractTpl<Scalar> ModelBase; \ | ||
| 87 | typedef ActivationBoundsTpl<Scalar> Bounds; \ | ||
| 88 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 89 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
| 90 | "ActivationModelQuadraticBarrier", \ | ||
| 91 | "Inequality activation model.\n\n" \ | ||
| 92 | "The activation is zero when r is between the lower (lb) and upper " \ | ||
| 93 | "(ub) bounds, beta determines how much of the total range is not " \ | ||
| 94 | "activated. This is the activation equations:\n" \ | ||
| 95 | "a(r) = 0.5 * ||r||^2 for lb > r > ub\n" \ | ||
| 96 | "a(r) = 0. for lb <= r <= ub.", \ | ||
| 97 | bp::init<Bounds>(bp::args("self", "bounds"), \ | ||
| 98 | "Initialize the activation model.\n\n" \ | ||
| 99 | ":param bounds: activation bounds")) \ | ||
| 100 | .def(ActivationModelQuadraticVisitor<Model>()) \ | ||
| 101 | .def(CastVisitor<Model>()) \ | ||
| 102 | .def(PrintableVisitor<Model>()) \ | ||
| 103 | .def(CopyableVisitor<Model>()); | ||
| 104 | |||
| 105 | 10 | void exposeActivationQuadraticBarrier() { | |
| 106 |
17/34✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 10 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 30 taken 10 times.
✗ Branch 31 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
✓ Branch 47 taken 10 times.
✗ Branch 48 not taken.
✓ Branch 50 taken 10 times.
✗ Branch 51 not taken.
✓ Branch 53 taken 10 times.
✗ Branch 54 not taken.
✓ Branch 56 taken 10 times.
✗ Branch 57 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS(CROCODDYL_ACTIVATION_BOUNDS_PYTHON_BINDINGS) |
| 107 |
17/34✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 10 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 30 taken 10 times.
✗ Branch 31 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
✓ Branch 47 taken 10 times.
✗ Branch 48 not taken.
✓ Branch 50 taken 10 times.
✗ Branch 51 not taken.
✓ Branch 53 taken 10 times.
✗ Branch 54 not taken.
✓ Branch 56 taken 10 times.
✗ Branch 57 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS( |
| 108 | CROCODDYL_ACTIVATION_MODEL_QUADRATIC_BARRIER_PYTHON_BINDINGS) | ||
| 109 | 10 | } | |
| 110 | |||
| 111 | } // namespace python | ||
| 112 | } // namespace crocoddyl | ||
| 113 |