Directory: | ./ |
---|---|
File: | include/crocoddyl/core/activations/weighted-quadratic.hpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 33 | 44 | 75.0% |
Branches: | 21 | 102 | 20.6% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2025, LAAS-CNRS, Heriot-Watt University | ||
5 | // Copyright note valid unless otherwise stated in individual files. | ||
6 | // All rights reserved. | ||
7 | /////////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | #ifndef CROCODDYL_CORE_ACTIVATIONS_WEIGHTED_QUADRATIC_HPP_ | ||
10 | #define CROCODDYL_CORE_ACTIVATIONS_WEIGHTED_QUADRATIC_HPP_ | ||
11 | |||
12 | #include <stdexcept> | ||
13 | |||
14 | #include "crocoddyl/core/activation-base.hpp" | ||
15 | #include "crocoddyl/core/fwd.hpp" | ||
16 | |||
17 | namespace crocoddyl { | ||
18 | |||
19 | template <typename _Scalar> | ||
20 | class ActivationModelWeightedQuadTpl | ||
21 | : public ActivationModelAbstractTpl<_Scalar> { | ||
22 | public: | ||
23 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
24 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
16 | CROCODDYL_DERIVED_CAST(ActivationModelBase, ActivationModelWeightedQuadTpl) |
25 | |||
26 | typedef _Scalar Scalar; | ||
27 | typedef MathBaseTpl<Scalar> MathBase; | ||
28 | typedef ActivationModelAbstractTpl<Scalar> Base; | ||
29 | typedef ActivationDataAbstractTpl<Scalar> ActivationDataAbstract; | ||
30 | typedef ActivationDataWeightedQuadTpl<Scalar> Data; | ||
31 | typedef typename MathBase::VectorXs VectorXs; | ||
32 | typedef typename MathBase::MatrixXs MatrixXs; | ||
33 | |||
34 | 228 | explicit ActivationModelWeightedQuadTpl(const VectorXs& weights) | |
35 |
2/4✓ Branch 3 taken 228 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 228 times.
✗ Branch 7 not taken.
|
228 | : Base(weights.size()), weights_(weights), new_weights_(false) {}; |
36 | 468 | virtual ~ActivationModelWeightedQuadTpl() = default; | |
37 | |||
38 | 9677 | virtual void calc(const std::shared_ptr<ActivationDataAbstract>& data, | |
39 | const Eigen::Ref<const VectorXs>& r) override { | ||
40 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9677 times.
|
9677 | if (static_cast<std::size_t>(r.size()) != nr_) { |
41 | ✗ | throw_pretty( | |
42 | "Invalid argument: " << "r has wrong dimension (it should be " + | ||
43 | std::to_string(nr_) + ")"); | ||
44 | } | ||
45 | 9677 | std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data); | |
46 | |||
47 |
2/4✓ Branch 1 taken 9677 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9677 times.
✗ Branch 6 not taken.
|
9677 | d->Wr = weights_.cwiseProduct(r); |
48 |
1/2✓ Branch 2 taken 9677 times.
✗ Branch 3 not taken.
|
9677 | data->a_value = Scalar(0.5) * r.dot(d->Wr); |
49 | 9677 | }; | |
50 | |||
51 | 305 | virtual void calcDiff(const std::shared_ptr<ActivationDataAbstract>& data, | |
52 | const Eigen::Ref<const VectorXs>& r) override { | ||
53 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 305 times.
|
305 | if (static_cast<std::size_t>(r.size()) != nr_) { |
54 | ✗ | throw_pretty( | |
55 | "Invalid argument: " << "r has wrong dimension (it should be " + | ||
56 | std::to_string(nr_) + ")"); | ||
57 | } | ||
58 | |||
59 | 305 | std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data); | |
60 |
1/2✓ Branch 3 taken 305 times.
✗ Branch 4 not taken.
|
305 | data->Ar = d->Wr; |
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
|
305 | if (new_weights_) { |
62 | ✗ | data->Arr.diagonal() = weights_; | |
63 | ✗ | new_weights_ = false; | |
64 | } | ||
65 | // The Hessian has constant values which were set in createData. | ||
66 | #ifndef NDEBUG | ||
67 |
3/14✓ Branch 2 taken 305 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 305 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 305 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
305 | assert_pretty(MatrixXs(data->Arr).isApprox(Arr_), "Arr has wrong value"); |
68 | #endif | ||
69 | 305 | }; | |
70 | |||
71 | 7842 | virtual std::shared_ptr<ActivationDataAbstract> createData() override { | |
72 |
1/2✓ Branch 1 taken 7842 times.
✗ Branch 2 not taken.
|
7842 | std::shared_ptr<Data> data = |
73 | 7842 | std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this); | |
74 |
1/2✓ Branch 3 taken 7842 times.
✗ Branch 4 not taken.
|
7842 | data->Arr.diagonal() = weights_; |
75 | |||
76 | #ifndef NDEBUG | ||
77 |
1/2✓ Branch 2 taken 7842 times.
✗ Branch 3 not taken.
|
7842 | Arr_ = data->Arr; |
78 | #endif | ||
79 | |||
80 | 15684 | return data; | |
81 | 7842 | }; | |
82 | |||
83 | template <typename NewScalar> | ||
84 | 4 | ActivationModelWeightedQuadTpl<NewScalar> cast() const { | |
85 | typedef ActivationModelWeightedQuadTpl<NewScalar> ReturnType; | ||
86 |
2/4✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
|
4 | ReturnType res(weights_.template cast<NewScalar>()); |
87 | 4 | return res; | |
88 | } | ||
89 | |||
90 | ✗ | const VectorXs& get_weights() const { return weights_; }; | |
91 | ✗ | void set_weights(const VectorXs& weights) { | |
92 | ✗ | if (weights.size() != weights_.size()) { | |
93 | ✗ | throw_pretty("Invalid argument: " | |
94 | << "weight vector has wrong dimension (it should be " + | ||
95 | std::to_string(weights_.size()) + ")"); | ||
96 | } | ||
97 | |||
98 | ✗ | weights_ = weights; | |
99 | ✗ | new_weights_ = true; | |
100 | ✗ | }; | |
101 | |||
102 | /** | ||
103 | * @brief Print relevant information of the quadratic-weighted model | ||
104 | * | ||
105 | * @param[out] os Output stream object | ||
106 | */ | ||
107 | 37 | virtual void print(std::ostream& os) const override { | |
108 | 37 | os << "ActivationModelQuad {nr=" << nr_ << "}"; | |
109 | 37 | } | |
110 | |||
111 | protected: | ||
112 | using Base::nr_; | ||
113 | |||
114 | private: | ||
115 | VectorXs weights_; | ||
116 | bool new_weights_; | ||
117 | |||
118 | #ifndef NDEBUG | ||
119 | MatrixXs Arr_; | ||
120 | #endif | ||
121 | }; | ||
122 | |||
123 | template <typename _Scalar> | ||
124 | struct ActivationDataWeightedQuadTpl | ||
125 | : public ActivationDataAbstractTpl<_Scalar> { | ||
126 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
127 | |||
128 | typedef _Scalar Scalar; | ||
129 | typedef MathBaseTpl<Scalar> MathBase; | ||
130 | typedef typename MathBase::VectorXs VectorXs; | ||
131 | typedef typename MathBase::DiagonalMatrixXs DiagonalMatrixXs; | ||
132 | typedef ActivationDataAbstractTpl<Scalar> Base; | ||
133 | |||
134 | template <typename Activation> | ||
135 | 7842 | explicit ActivationDataWeightedQuadTpl(Activation* const activation) | |
136 |
3/6✓ Branch 2 taken 7842 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7842 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 7842 times.
✗ Branch 9 not taken.
|
7842 | : Base(activation), Wr(VectorXs::Zero(activation->get_nr())) {} |
137 | 15684 | virtual ~ActivationDataWeightedQuadTpl() = default; | |
138 | |||
139 | VectorXs Wr; | ||
140 | |||
141 | using Base::a_value; | ||
142 | using Base::Ar; | ||
143 | using Base::Arr; | ||
144 | }; | ||
145 | |||
146 | } // namespace crocoddyl | ||
147 | |||
148 | CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS( | ||
149 | crocoddyl::ActivationModelWeightedQuadTpl) | ||
150 | CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT( | ||
151 | crocoddyl::ActivationDataWeightedQuadTpl) | ||
152 | |||
153 | #endif // CROCODDYL_CORE_ACTIVATIONS_WEIGHTED_QUADRATIC_HPP_ | ||
154 |