| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/core/constraint-base.cpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 52 | 54 | 96.3% |
| Branches: | 96 | 192 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2020-2025, University of Edinburgh, Heriot-Watt University | ||
| 5 | // Copyright note valid unless otherwise stated in individual files. | ||
| 6 | // All rights reserved. | ||
| 7 | /////////////////////////////////////////////////////////////////////////////// | ||
| 8 | |||
| 9 | #include "python/crocoddyl/core/constraint-base.hpp" | ||
| 10 | |||
| 11 | namespace crocoddyl { | ||
| 12 | namespace python { | ||
| 13 | |||
| 14 | template <typename Model> | ||
| 15 | struct ConstraintModelAbstractVisitor | ||
| 16 | : public bp::def_visitor<ConstraintModelAbstractVisitor<Model>> { | ||
| 17 | typedef typename Model::ConstraintModel ConstraintModel; | ||
| 18 | typedef typename Model::ConstraintData ConstraintData; | ||
| 19 | typedef typename Model::State State; | ||
| 20 | typedef typename Model::VectorXs VectorXs; | ||
| 21 | template <class PyClass> | ||
| 22 | 40 | void visit(PyClass& cl) const { | |
| 23 | cl | ||
| 24 |
2/4✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
|
40 | .def(bp::init<std::shared_ptr<State>, std::size_t, std::size_t, |
| 25 | std::size_t, bp::optional<bool>>( | ||
| 26 | bp::args("self", "state", "nu", "ng", "nh", "T_const"), | ||
| 27 | "Initialize the constraint model.\n\n" | ||
| 28 | ":param state: state description\n" | ||
| 29 | ":param nu: dimension of control vector (default state.nv)\n" | ||
| 30 | ":param ng: number of inequality constraints\n" | ||
| 31 | ":param nh: number of equality constraints\n" | ||
| 32 | ":param T_const: True if this is a constraint in both running and " | ||
| 33 | "terminal nodes.\n" | ||
| 34 | " False if it is a constraint on running nodes only " | ||
| 35 | "(default true)")) | ||
| 36 |
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 | .def(bp::init<std::shared_ptr<State>, std::size_t, std::size_t, |
| 37 | bp::optional<bool>>( | ||
| 38 | bp::args("self", "state", "ng", "nh", "T_const"), | ||
| 39 | "Initialize the constraint model.\n\n" | ||
| 40 | ":param state: state description\n" | ||
| 41 | ":param ng: number of inequality constraints\n" | ||
| 42 | ":param nh: number of equality constraints\n" | ||
| 43 | ":param T_const: True if this is a constraint in both running and " | ||
| 44 | "terminal nodes.\n" | ||
| 45 | " False if it is a constraint on running nodes only " | ||
| 46 | "(default true)")) | ||
| 47 |
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 | .def("calc", pure_virtual(&Model::calc), |
| 48 | bp::args("self", "data", "x", "u"), | ||
| 49 | "Compute the constraint value.\n\n" | ||
| 50 | ":param data: constraint data\n" | ||
| 51 | ":param x: state point (dim. state.nx)\n" | ||
| 52 | ":param u: control input (dim. nu)") | ||
| 53 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("calc", |
| 54 | static_cast<void (ConstraintModel::*)( | ||
| 55 | const std::shared_ptr<ConstraintData>&, | ||
| 56 | const Eigen::Ref<const VectorXs>&)>(&ConstraintModel::calc), | ||
| 57 | bp::args("self", "data", "x"), | ||
| 58 | "Compute the constraint value for nodes that depends only on the " | ||
| 59 | "state.\n\n" | ||
| 60 | "It updates the constraint based on the state only.\n" | ||
| 61 | "This function is commonly used in the terminal nodes of an " | ||
| 62 | "optimal " | ||
| 63 | "control problem.\n" | ||
| 64 | ":param data: constraint data\n" | ||
| 65 | ":param x: state point (dim. state.nx)") | ||
| 66 |
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 | .def("calcDiff", pure_virtual(&Model::calcDiff), |
| 67 | bp::args("self", "data", "x", "u"), | ||
| 68 | "Compute the Jacobians of the constraint function.\n\n" | ||
| 69 | "It computes the Jacobians of the constraint function.\n" | ||
| 70 | "It assumes that calc has been run first.\n" | ||
| 71 | ":param data: constraint data\n" | ||
| 72 | ":param x: state point (dim. state.nx)\n" | ||
| 73 | ":param u: control input (dim. nu)\n") | ||
| 74 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def( |
| 75 | "calcDiff", | ||
| 76 | static_cast<void (ConstraintModel::*)( | ||
| 77 | const std::shared_ptr<ConstraintData>&, | ||
| 78 | const Eigen::Ref<const VectorXs>&)>(&ConstraintModel::calcDiff), | ||
| 79 | bp::args("self", "data", "x"), | ||
| 80 | "Compute the Jacobian of the constraint with respect to the state " | ||
| 81 | "only.\n\n" | ||
| 82 | "It computes the Jacobian of the constraint function based on the " | ||
| 83 | "state only.\n" | ||
| 84 | "This function is commonly used in the terminal nodes of an " | ||
| 85 | "optimal " | ||
| 86 | "control problem.\n" | ||
| 87 | ":param data: constraint data\n" | ||
| 88 | ":param x: state point (dim. state.nx)") | ||
| 89 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("createData", &Model::createData, |
| 90 | ✗ | bp::with_custodian_and_ward_postcall<0, 2>(), | |
| 91 | bp::args("self", "data"), | ||
| 92 | "Create the constraint data.\n\n" | ||
| 93 | "Each constraint model has its own data that needs to be " | ||
| 94 | "allocated. " | ||
| 95 | "This function\n" | ||
| 96 | "returns the allocated data for a predefined constraint.\n" | ||
| 97 | ":param data: shared data\n" | ||
| 98 | ":return constraint data.") | ||
| 99 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
40 | .def("createData", &Model::default_createData, |
| 100 | ✗ | bp::with_custodian_and_ward_postcall<0, 2>()) | |
| 101 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("updateBounds", &Model::update_bounds, |
| 102 | bp::args("self", "lower", "upper"), | ||
| 103 | "Update the lower and upper bounds.\n\n" | ||
| 104 | ":param lower: lower bound\n" | ||
| 105 | ":param upper: upper bound") | ||
| 106 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("removeBounds", &Model::remove_bounds, bp::args("self"), |
| 107 | "Remove the bounds.") | ||
| 108 |
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.
|
40 | .add_property( |
| 109 | "state", | ||
| 110 | bp::make_function(&Model::get_state, | ||
| 111 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 112 | "state description") | ||
| 113 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
| 114 | "residual", | ||
| 115 | bp::make_function(&Model::get_residual, | ||
| 116 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 117 | "residual model") | ||
| 118 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .add_property("type", bp::make_function(&Model::get_type), |
| 119 | "type of constraint") | ||
| 120 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property("lb", |
| 121 | bp::make_function(&Model::get_lb, | ||
| 122 | 40 | bp::return_internal_reference<>()), | |
| 123 | "lower bound of constraint") | ||
| 124 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property("ub", |
| 125 | bp::make_function(&Model::get_ub, | ||
| 126 | 40 | bp::return_internal_reference<>()), | |
| 127 | "upper bound of constraint") | ||
| 128 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .add_property("nu", bp::make_function(&Model::get_nu), |
| 129 | "dimension of control vector") | ||
| 130 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .add_property("ng", bp::make_function(&Model::get_ng), |
| 131 | "number of inequality constraints") | ||
| 132 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .add_property("nh", bp::make_function(&Model::get_nh), |
| 133 | "number of equality constraints") | ||
| 134 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
| 135 | "T_constraint", bp::make_function(&Model::get_T_constraint), | ||
| 136 | "True if the constraint is imposed in terminal nodes as well"); | ||
| 137 | 40 | } | |
| 138 | }; | ||
| 139 | |||
| 140 | template <typename Data> | ||
| 141 | struct ConstraintDataAbstractVisitor | ||
| 142 | : public bp::def_visitor<ConstraintDataAbstractVisitor<Data>> { | ||
| 143 | template <class PyClass> | ||
| 144 | 40 | void visit(PyClass& cl) const { | |
| 145 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | cl.add_property( |
| 146 | "shared", | ||
| 147 | 40 | bp::make_getter(&Data::shared, bp::return_internal_reference<>()), | |
| 148 | "shared data") | ||
| 149 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
| 150 | "residual", | ||
| 151 | bp::make_getter(&Data::residual, | ||
| 152 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 153 | "residual data") | ||
| 154 |
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( |
| 155 | 40 | "g", bp::make_getter(&Data::g, bp::return_internal_reference<>()), | |
| 156 | bp::make_setter(&Data::g), "inequality constraint residual") | ||
| 157 |
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( |
| 158 | 40 | "Gx", bp::make_getter(&Data::Gx, bp::return_internal_reference<>()), | |
| 159 | bp::make_setter(&Data::Gx), "Jacobian of the inequality constraint") | ||
| 160 |
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( |
| 161 | 40 | "Gu", bp::make_getter(&Data::Gu, bp::return_internal_reference<>()), | |
| 162 | bp::make_setter(&Data::Gu), "Jacobian of the inequality constraint") | ||
| 163 |
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( |
| 164 | 40 | "h", bp::make_getter(&Data::h, bp::return_internal_reference<>()), | |
| 165 | bp::make_setter(&Data::h), "equality constraint residual") | ||
| 166 |
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( |
| 167 | 40 | "Hx", bp::make_getter(&Data::Hx, bp::return_internal_reference<>()), | |
| 168 | bp::make_setter(&Data::Hx), "Jacobian of the equality constraint") | ||
| 169 |
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( |
| 170 | 40 | "Hu", bp::make_getter(&Data::Hu, bp::return_internal_reference<>()), | |
| 171 | bp::make_setter(&Data::Hu), "Jacobian of the equality constraint"); | ||
| 172 | 40 | } | |
| 173 | }; | ||
| 174 | |||
| 175 | #define CROCODDYL_CONSTRAINT_MODEL_ABSTRACT_PYTHON_BINDINGS(Scalar) \ | ||
| 176 | typedef ConstraintModelAbstractTpl<Scalar> Model; \ | ||
| 177 | typedef ConstraintModelAbstractTpl_wrap<Scalar> Model_wrap; \ | ||
| 178 | typedef StateAbstractTpl<Scalar> State; \ | ||
| 179 | typedef Model::ResidualModelAbstract ResidualModel; \ | ||
| 180 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 181 | bp::class_<Model_wrap, boost::noncopyable>( \ | ||
| 182 | "ConstraintModelAbstract", \ | ||
| 183 | "Abstract multibody constraint models.\n\n" \ | ||
| 184 | "A constraint model defines both: inequality g(x,u) and equality h(x, " \ | ||
| 185 | "u) constraints. The constraint function depends on the state point x, " \ | ||
| 186 | "which lies in the state manifold described with a nx-tuple, its " \ | ||
| 187 | "velocity xd that belongs to the tangent space with ndx dimension, and " \ | ||
| 188 | "the control input u.", \ | ||
| 189 | bp::init<std::shared_ptr<State>, std::shared_ptr<ResidualModel>, \ | ||
| 190 | std::size_t, std::size_t>( \ | ||
| 191 | bp::args("self", "state", "residual", "ng", "nh"), \ | ||
| 192 | "Initialize the constraint model.\n\n" \ | ||
| 193 | ":param state: state description\n" \ | ||
| 194 | ":param residual: residual model\n" \ | ||
| 195 | ":param ng: number of inequality constraints\n" \ | ||
| 196 | ":param nh: number of equality constraints")) \ | ||
| 197 | .def(ConstraintModelAbstractVisitor<Model_wrap>()) \ | ||
| 198 | .def(PrintableVisitor<Model_wrap>()) \ | ||
| 199 | .def(CopyableVisitor<Model_wrap>()); | ||
| 200 | |||
| 201 | #define CROCODDYL_CONSTRAINT_DATA_ABSTRACT_PYTHON_BINDINGS(Scalar) \ | ||
| 202 | typedef ConstraintDataAbstractTpl<Scalar> Data; \ | ||
| 203 | typedef ConstraintModelAbstractTpl<Scalar> Model; \ | ||
| 204 | typedef Model::DataCollectorAbstract DataCollector; \ | ||
| 205 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 206 | bp::class_<Data, boost::noncopyable>( \ | ||
| 207 | "ConstraintDataAbstract", "Abstract class for constraint data.\n\n", \ | ||
| 208 | bp::init<Model*, DataCollector*>( \ | ||
| 209 | bp::args("self", "model", "data"), \ | ||
| 210 | "Create common data shared between constraint models.\n\n" \ | ||
| 211 | ":param model: constraint model\n" \ | ||
| 212 | ":param data: shared data")[bp::with_custodian_and_ward< \ | ||
| 213 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
| 214 | .def(ConstraintDataAbstractVisitor<Data>()) \ | ||
| 215 | .def(CopyableVisitor<Data>()); | ||
| 216 | |||
| 217 | 10 | void exposeConstraintAbstract() { | |
| 218 | 20 | bp::enum_<ConstraintType>("ConstraintType") | |
| 219 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .value("Inequality", Inequality) |
| 220 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .value("Equality", Equality) |
| 221 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .value("Both", Both) |
| 222 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .export_values(); |
| 223 | |||
| 224 |
15/30✓ 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 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 32 taken 10 times.
✗ Branch 33 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.
|
20 | CROCODDYL_PYTHON_SCALARS(CROCODDYL_CONSTRAINT_MODEL_ABSTRACT_PYTHON_BINDINGS) |
| 225 |
15/30✓ 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 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 32 taken 10 times.
✗ Branch 33 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.
|
20 | CROCODDYL_PYTHON_SCALARS(CROCODDYL_CONSTRAINT_DATA_ABSTRACT_PYTHON_BINDINGS) |
| 226 | 10 | } | |
| 227 | |||
| 228 | } // namespace python | ||
| 229 | } // namespace crocoddyl | ||
| 230 |