Directory: | ./ |
---|---|
File: | include/crocoddyl/core/residuals/control.hxx |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 40 | 48 | 83.3% |
Branches: | 20 | 110 | 18.2% |
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/residuals/control.hpp" | ||
11 | |||
12 | namespace crocoddyl { | ||
13 | |||
14 | template <typename Scalar> | ||
15 | 1405 | ResidualModelControlTpl<Scalar>::ResidualModelControlTpl( | |
16 | std::shared_ptr<typename Base::StateAbstract> state, const VectorXs& uref) | ||
17 | 1405 | : Base(state, static_cast<std::size_t>(uref.size()), | |
18 | 1405 | static_cast<std::size_t>(uref.size()), false, false, true), | |
19 |
2/4✓ Branch 4 taken 1405 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1405 times.
✗ Branch 9 not taken.
|
1405 | uref_(uref) { |
20 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1405 times.
|
1405 | if (nu_ == 0) { |
21 | ✗ | throw_pretty("Invalid argument: " | |
22 | << "it seems to be an autonomous system, if so, don't add " | ||
23 | "this residual function"); | ||
24 | } | ||
25 | 1405 | } | |
26 | |||
27 | template <typename Scalar> | ||
28 | 4 | ResidualModelControlTpl<Scalar>::ResidualModelControlTpl( | |
29 | std::shared_ptr<typename Base::StateAbstract> state, const std::size_t nu) | ||
30 |
3/6✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
|
4 | : Base(state, nu, nu, false, false, true), uref_(VectorXs::Zero(nu)) { |
31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (nu_ == 0) { |
32 | ✗ | throw_pretty("Invalid argument: " | |
33 | << "it seems to be an autonomous system, if so, don't add " | ||
34 | "this residual function"); | ||
35 | } | ||
36 | 4 | } | |
37 | |||
38 | template <typename Scalar> | ||
39 | 12 | ResidualModelControlTpl<Scalar>::ResidualModelControlTpl( | |
40 | std::shared_ptr<typename Base::StateAbstract> state) | ||
41 | : Base(state, state->get_nv(), state->get_nv(), false, false, true), | ||
42 |
4/8✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 11 taken 12 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 12 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 12 times.
✗ Branch 18 not taken.
|
12 | uref_(VectorXs::Zero(state->get_nv())) {} |
43 | |||
44 | template <typename Scalar> | ||
45 | 74411 | void ResidualModelControlTpl<Scalar>::calc( | |
46 | const std::shared_ptr<ResidualDataAbstract>& data, | ||
47 | const Eigen::Ref<const VectorXs>&, const Eigen::Ref<const VectorXs>& u) { | ||
48 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 74411 times.
|
74411 | if (static_cast<std::size_t>(u.size()) != nu_) { |
49 | ✗ | throw_pretty( | |
50 | "Invalid argument: " << "u has wrong dimension (it should be " + | ||
51 | std::to_string(nu_) + ")"); | ||
52 | } | ||
53 | |||
54 |
1/2✓ Branch 3 taken 74411 times.
✗ Branch 4 not taken.
|
74411 | data->r = u - uref_; |
55 | 74411 | } | |
56 | |||
57 | template <typename Scalar> | ||
58 | 1252 | void ResidualModelControlTpl<Scalar>::calc( | |
59 | const std::shared_ptr<ResidualDataAbstract>& data, | ||
60 | const Eigen::Ref<const VectorXs>&) { | ||
61 | 1252 | data->r.setZero(); | |
62 | 1252 | } | |
63 | |||
64 | template <typename Scalar> | ||
65 | #ifndef NDEBUG | ||
66 | 13497 | void ResidualModelControlTpl<Scalar>::calcDiff( | |
67 | const std::shared_ptr<ResidualDataAbstract>& data, | ||
68 | const Eigen::Ref<const VectorXs>&, const Eigen::Ref<const VectorXs>&) { | ||
69 | #else | ||
70 | void ResidualModelControlTpl<Scalar>::calcDiff( | ||
71 | const std::shared_ptr<ResidualDataAbstract>&, | ||
72 | const Eigen::Ref<const VectorXs>&, const Eigen::Ref<const VectorXs>&) { | ||
73 | #endif | ||
74 | // The Jacobian has constant values which were set in createData. | ||
75 |
3/14✓ Branch 4 taken 13497 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13497 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 13497 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
|
13497 | assert_pretty(MatrixXs(data->Ru).isApprox(MatrixXs::Identity(nu_, nu_)), |
76 | "Ru has wrong value"); | ||
77 | 13497 | } | |
78 | |||
79 | template <typename Scalar> | ||
80 | std::shared_ptr<ResidualDataAbstractTpl<Scalar> > | ||
81 | 96599 | ResidualModelControlTpl<Scalar>::createData( | |
82 | DataCollectorAbstract* const _data) { | ||
83 |
1/2✓ Branch 1 taken 96599 times.
✗ Branch 2 not taken.
|
96599 | std::shared_ptr<ResidualDataAbstract> data = |
84 | std::allocate_shared<ResidualDataAbstract>( | ||
85 | 96599 | Eigen::aligned_allocator<ResidualDataAbstract>(), this, _data); | |
86 |
2/4✓ Branch 2 taken 96599 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 96599 times.
✗ Branch 6 not taken.
|
96599 | data->Ru.diagonal().fill((Scalar)1.); |
87 | 96599 | return data; | |
88 | } | ||
89 | |||
90 | template <typename Scalar> | ||
91 | 13417 | void ResidualModelControlTpl<Scalar>::calcCostDiff( | |
92 | const std::shared_ptr<CostDataAbstract>& cdata, | ||
93 | const std::shared_ptr<ResidualDataAbstract>&, | ||
94 | const std::shared_ptr<ActivationDataAbstract>& adata, const bool) { | ||
95 | 13417 | cdata->Lu = adata->Ar; | |
96 | 13417 | cdata->Luu = adata->Arr; | |
97 | 13417 | } | |
98 | |||
99 | template <typename Scalar> | ||
100 | template <typename NewScalar> | ||
101 | ✗ | ResidualModelControlTpl<NewScalar> ResidualModelControlTpl<Scalar>::cast() | |
102 | const { | ||
103 | typedef ResidualModelControlTpl<NewScalar> ReturnType; | ||
104 | ✗ | ReturnType ret(state_->template cast<NewScalar>(), | |
105 | ✗ | uref_.template cast<NewScalar>()); | |
106 | ✗ | return ret; | |
107 | } | ||
108 | |||
109 | template <typename Scalar> | ||
110 | 63 | void ResidualModelControlTpl<Scalar>::print(std::ostream& os) const { | |
111 | 63 | os << "ResidualModelControl"; | |
112 | 63 | } | |
113 | |||
114 | template <typename Scalar> | ||
115 | const typename MathBaseTpl<Scalar>::VectorXs& | ||
116 | 1 | ResidualModelControlTpl<Scalar>::get_reference() const { | |
117 | 1 | return uref_; | |
118 | } | ||
119 | |||
120 | template <typename Scalar> | ||
121 | 1 | void ResidualModelControlTpl<Scalar>::set_reference(const VectorXs& reference) { | |
122 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (static_cast<std::size_t>(reference.size()) != nu_) { |
123 | ✗ | throw_pretty("Invalid argument: " | |
124 | << "the control reference has wrong dimension (" | ||
125 | << reference.size() | ||
126 | << " provided - it should be " + std::to_string(nu_) + ")") | ||
127 | } | ||
128 | 1 | uref_ = reference; | |
129 | 1 | } | |
130 | |||
131 | } // namespace crocoddyl | ||
132 |