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