GCC Code Coverage Report


Directory: ./
File: bindings/python/crocoddyl/core/constraints/residual.cpp
Date: 2025-03-26 19:23:43
Exec Total Coverage
Lines: 13 13 100.0%
Branches: 42 84 50.0%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2021-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 "crocoddyl/core/constraints/residual.hpp"
10
11 #include "python/crocoddyl/core/core.hpp"
12
13 namespace crocoddyl {
14 namespace python {
15
16 template <typename Model>
17 struct ConstraintModelResidualVisitor
18 : public bp::def_visitor<ConstraintModelResidualVisitor<Model>> {
19 typedef typename Model::ConstraintDataAbstract Data;
20 typedef typename Model::Base ModelBase;
21 typedef typename Model::StateAbstract State;
22 typedef typename Model::ResidualModelAbstract ResidualModel;
23 typedef typename Model::VectorXs VectorXs;
24 template <class PyClass>
25 40 void visit(PyClass& cl) const {
26
2/4
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
40 cl.def(bp::init<std::shared_ptr<State>, std::shared_ptr<ResidualModel>,
27 bp::optional<bool>>(
28 bp::args("self", "state", "residual", "T_act"),
29 "Initialize the residual constraint model as an equality "
30 "constraint.\n\n"
31 ":param state: state description\n"
32 ":param residual: residual model\n"
33 ":param T_act: false if we want to deactivate the residual at "
34 "the terminal node (default true)"))
35
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
80 .def(
36 "calc",
37 static_cast<void (Model::*)(
38 const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&,
39 const Eigen::Ref<const VectorXs>&)>(&Model::calc),
40 bp::args("self", "data", "x", "u"),
41 "Compute the residual constraint.\n\n"
42 ":param data: constraint data\n"
43 ":param x: state point (dim. state.nx)\n"
44 ":param u: control input (dim. nu)")
45
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
80 .def("calc",
46 static_cast<void (Model::*)(const std::shared_ptr<Data>&,
47 const Eigen::Ref<const VectorXs>&)>(
48 &Model::calc),
49 bp::args("self", "data", "x"),
50 "Compute the residual constraint based on state only.\n\n"
51 "It updates the constraint based on the state only. This function "
52 "is commonly used in the terminal nodes of an optimal control "
53 "problem.\n"
54 ":param data: constraint data\n"
55 ":param x: state point (dim. state.nx)")
56
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
80 .def(
57 "calcDiff",
58 static_cast<void (Model::*)(
59 const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&,
60 const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff),
61 bp::args("self", "data", "x", "u"),
62 "Compute the derivatives of the residual constraint.\n\n"
63 "It assumes that calc has been run first.\n"
64 ":param data: constraint data\n"
65 ":param x: state point (dim. state.nx)\n"
66 ":param u: control input (dim. nu)\n")
67
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
80 .def("calcDiff",
68 static_cast<void (Model::*)(const std::shared_ptr<Data>&,
69 const Eigen::Ref<const VectorXs>&)>(
70 &Model::calcDiff),
71 bp::args("self", "data", "x"),
72 "Compute the derivatives of the residual constraint with respect "
73 "to the state only.\n\n"
74 "It updates the Jacobian of the constraint function based on the "
75 "state only. This function is commonly used in the terminal nodes "
76 "of an optimal control problem.\n"
77 ":param data: constraint data\n"
78 ":param x: state point (dim. state.nx)")
79
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("createData", &Model::createData,
80 40 bp::with_custodian_and_ward_postcall<0, 2>(),
81 bp::args("self", "data"),
82 "Create the residual constraint data.\n\n"
83 "Each constraint model has its own data that needs to be "
84 "allocated. This function returns the allocated data for a "
85 "predefined constraint.\n"
86 ":param data: shared data\n"
87 ":return constraint data.");
88 40 }
89 };
90
91 #define CROCODDYL_CONSTRAINT_MODEL_RESIDUAL_PYTHON_BINDINGS(Scalar) \
92 typedef ConstraintModelResidualTpl<Scalar> Model; \
93 typedef ConstraintModelAbstractTpl<Scalar> ModelBase; \
94 typedef typename ModelBase::StateAbstract State; \
95 typedef typename ModelBase::ResidualModelAbstract ResidualModel; \
96 typedef typename ModelBase::VectorXs VectorXs; \
97 bp::register_ptr_to_python<std::shared_ptr<Model>>(); \
98 bp::class_<Model, bp::bases<ModelBase>>( \
99 "ConstraintModelResidual", \
100 "This defines equality / inequality constraints based on a residual " \
101 "vector and its bounds.", \
102 bp::init<std::shared_ptr<State>, std::shared_ptr<ResidualModel>, \
103 VectorXs, VectorXs, bp::optional<bool>>( \
104 bp::args("self", "state", "residual", "lower", "upper", "T_act"), \
105 "Initialize the residual constraint model as an inequality " \
106 "constraint.\n\n" \
107 ":param state: state description\n" \
108 ":param residual: residual model\n" \
109 ":param lower: lower bound\n" \
110 ":param upper: upper bound\n" \
111 ":param T_act: false if we want to deactivate the residual at the " \
112 "terminal node (default true)")) \
113 .def(ConstraintModelResidualVisitor<Model>()) \
114 .def(CastVisitor<Model>()) \
115 .def(PrintableVisitor<Model>()) \
116 .def(CopyableVisitor<Model>());
117
118 #define CROCODDYL_CONSTRAINT_DATA_RESIDUAL_PYTHON_BINDINGS(Scalar) \
119 typedef ConstraintDataResidualTpl<Scalar> Data; \
120 typedef ConstraintDataAbstractTpl<Scalar> DataBase; \
121 typedef ConstraintModelResidualTpl<Scalar> Model; \
122 typedef Model::DataCollectorAbstract DataCollector; \
123 bp::register_ptr_to_python<std::shared_ptr<Data>>(); \
124 bp::class_<Data, bp::bases<DataBase>>( \
125 "ConstraintDataResidual", "Data for residual constraint.\n\n", \
126 bp::init<Model*, DataCollector*>( \
127 bp::args("self", "model", "data"), \
128 "Create residual constraint data.\n\n" \
129 ":param model: residual constraint model\n" \
130 ":param data: shared data")[bp::with_custodian_and_ward< \
131 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \
132 .def(CopyableVisitor<Data>());
133
134 10 void exposeConstraintResidual() {
135
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_CONSTRAINT_MODEL_RESIDUAL_PYTHON_BINDINGS)
136
13/26
✓ 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 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 29 taken 10 times.
✗ Branch 30 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.
20 CROCODDYL_PYTHON_SCALARS(CROCODDYL_CONSTRAINT_DATA_RESIDUAL_PYTHON_BINDINGS)
137 10 }
138
139 } // namespace python
140 } // namespace crocoddyl
141