Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/multibody/residuals/state.cpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 15 | 16 | 93.8% |
Branches: | 37 | 74 | 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/multibody/residuals/state.hpp" | ||
10 | |||
11 | #include "python/crocoddyl/multibody/multibody.hpp" | ||
12 | |||
13 | namespace crocoddyl { | ||
14 | namespace python { | ||
15 | |||
16 | template <typename Model> | ||
17 | struct ResidualModelStateVisitor | ||
18 | : public bp::def_visitor<ResidualModelStateVisitor<Model>> { | ||
19 | typedef typename Model::ResidualDataAbstract Data; | ||
20 | typedef typename Model::Base ModelBase; | ||
21 | typedef typename Model::StateAbstract State; | ||
22 | typedef typename Model::VectorXs VectorXs; | ||
23 | template <class PyClass> | ||
24 | 40 | void visit(PyClass& cl) const { | |
25 |
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>, VectorXs>( |
26 | bp::args("self", "state", "xref"), | ||
27 | "Initialize the state cost model.\n\n" | ||
28 | "The default nu value is obtained from state.nv.\n" | ||
29 | ":param state: state description\n" | ||
30 | ":param xref: reference state")) | ||
31 |
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>( |
32 | bp::args("self", "state", "nu"), | ||
33 | "Initialize the state cost model.\n\n" | ||
34 | "The default reference state is obtained from state.zero().\n" | ||
35 | ":param state: state description\n" | ||
36 | ":param nu: dimension of control vector")) | ||
37 |
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>>( |
38 | bp::args("self", "state"), | ||
39 | "Initialize the state cost model.\n\n" | ||
40 | "The default reference state is obtained from state.zero(), and nu " | ||
41 | "from state.nv.\n" | ||
42 | ":param state: state description")) | ||
43 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def( |
44 | "calc", | ||
45 | static_cast<void (Model::*)( | ||
46 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
47 | const Eigen::Ref<const VectorXs>&)>(&Model::calc), | ||
48 | bp::args("self", "data", "x", "u"), | ||
49 | "Compute the state cost.\n\n" | ||
50 | ":param data: cost data\n" | ||
51 | ":param x: state point (dim. state.nx)\n" | ||
52 | ":param u: control input (dim. nu)") | ||
53 |
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( |
54 | "calc", | ||
55 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
56 | const Eigen::Ref<const VectorXs>&)>( | ||
57 | &ModelBase::calc), | ||
58 | bp::args("self", "data", "x")) | ||
59 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def( |
60 | "calcDiff", | ||
61 | static_cast<void (Model::*)( | ||
62 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
63 | const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff), | ||
64 | bp::args("self", "data", "x", "u"), | ||
65 | "Compute the derivatives of the state cost.\n\n" | ||
66 | "It assumes that calc has been run first.\n" | ||
67 | ":param data: action data\n" | ||
68 | ":param x: state point (dim. state.nx)\n" | ||
69 | ":param u: control input (dim. nu)") | ||
70 |
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( |
71 | "calcDiff", | ||
72 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
73 | const Eigen::Ref<const VectorXs>&)>( | ||
74 | &ModelBase::calcDiff), | ||
75 | bp::args("self", "data", "x")) | ||
76 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("createData", &Model::createData, |
77 | ✗ | bp::with_custodian_and_ward_postcall<0, 2>(), | |
78 | bp::args("self", "data"), | ||
79 | "Create the stae residual data.\n\n" | ||
80 | "Each residual model has its own data that needs to be allocated. " | ||
81 | "This function\n" | ||
82 | "returns the allocated data for the control residual.\n" | ||
83 | ":param data: shared data\n" | ||
84 | ":return residual data.") | ||
85 |
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("reference", |
86 | bp::make_function(&Model::get_reference, | ||
87 | 40 | bp::return_internal_reference<>()), | |
88 | &Model::set_reference, "reference state"); | ||
89 | 40 | } | |
90 | }; | ||
91 | |||
92 | #define CROCODDYL_RESIDUAL_MODEL_STATE_PYTHON_BINDINGS(Scalar) \ | ||
93 | typedef ResidualModelStateTpl<Scalar> Model; \ | ||
94 | typedef ResidualModelAbstractTpl<Scalar> ModelBase; \ | ||
95 | typedef typename ModelBase::StateAbstract State; \ | ||
96 | typedef typename ModelBase::VectorXs VectorXs; \ | ||
97 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
98 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
99 | "ResidualModelState", \ | ||
100 | "This cost function defines a residual vector as r = x - xref, with x " \ | ||
101 | "and xref as the current and reference state, respectively.", \ | ||
102 | bp::init<std::shared_ptr<State>, VectorXs, std::size_t>( \ | ||
103 | bp::args("self", "state", "xref", "nu"), \ | ||
104 | "Initialize the state cost model.\n\n" \ | ||
105 | ":param state: state description\n" \ | ||
106 | ":param xref: reference state (default state.zero())\n" \ | ||
107 | ":param nu: dimension of control vector")) \ | ||
108 | .def(ResidualModelStateVisitor<Model>()) \ | ||
109 | .def(CastVisitor<Model>()) \ | ||
110 | .def(PrintableVisitor<Model>()) \ | ||
111 | .def(CopyableVisitor<Model>()); | ||
112 | |||
113 | 10 | void exposeResidualState() { | |
114 |
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_RESIDUAL_MODEL_STATE_PYTHON_BINDINGS) |
115 | 10 | } | |
116 | |||
117 | } // namespace python | ||
118 | } // namespace crocoddyl | ||
119 |