GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/activations/weighted-quadratic.hpp
Date: 2025-01-30 11:01:55
Exec Total Coverage
Lines: 28 39 71.8%
Branches: 17 94 18.1%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019, LAAS-CNRS
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 #include "crocoddyl/core/utils/exception.hpp"
17
18 namespace crocoddyl {
19
20 template <typename _Scalar>
21 class ActivationModelWeightedQuadTpl
22 : public ActivationModelAbstractTpl<_Scalar> {
23 public:
24 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
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 224 explicit ActivationModelWeightedQuadTpl(const VectorXs& weights)
35
2/4
✓ Branch 3 taken 224 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 224 times.
✗ Branch 7 not taken.
224 : Base(weights.size()), weights_(weights), new_weights_(false) {};
36 452 virtual ~ActivationModelWeightedQuadTpl() {};
37
38 9675 virtual void calc(const std::shared_ptr<ActivationDataAbstract>& data,
39 const Eigen::Ref<const VectorXs>& r) {
40
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9675 times.
9675 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 9675 std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data);
46
47
2/4
✓ Branch 1 taken 9675 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9675 times.
✗ Branch 6 not taken.
9675 d->Wr = weights_.cwiseProduct(r);
48
1/2
✓ Branch 2 taken 9675 times.
✗ Branch 3 not taken.
9675 data->a_value = Scalar(0.5) * r.dot(d->Wr);
49 9675 };
50
51 304 virtual void calcDiff(const std::shared_ptr<ActivationDataAbstract>& data,
52 const Eigen::Ref<const VectorXs>& r) {
53
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 304 times.
304 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 304 std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data);
60
1/2
✓ Branch 3 taken 304 times.
✗ Branch 4 not taken.
304 data->Ar = d->Wr;
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
304 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 304 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 304 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 304 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.
304 assert_pretty(MatrixXs(data->Arr).isApprox(Arr_), "Arr has wrong value");
68 #endif
69 304 };
70
71 7839 virtual std::shared_ptr<ActivationDataAbstract> createData() {
72
1/2
✓ Branch 1 taken 7839 times.
✗ Branch 2 not taken.
7839 std::shared_ptr<Data> data =
73 7839 std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
74
1/2
✓ Branch 3 taken 7839 times.
✗ Branch 4 not taken.
7839 data->Arr.diagonal() = weights_;
75
76 #ifndef NDEBUG
77
1/2
✓ Branch 2 taken 7839 times.
✗ Branch 3 not taken.
7839 Arr_ = data->Arr;
78 #endif
79
80 15678 return data;
81 7839 };
82
83 const VectorXs& get_weights() const { return weights_; };
84 void set_weights(const VectorXs& weights) {
85 if (weights.size() != weights_.size()) {
86 throw_pretty("Invalid argument: "
87 << "weight vector has wrong dimension (it should be " +
88 std::to_string(weights_.size()) + ")");
89 }
90
91 weights_ = weights;
92 new_weights_ = true;
93 };
94
95 /**
96 * @brief Print relevant information of the quadratic-weighted model
97 *
98 * @param[out] os Output stream object
99 */
100 37 virtual void print(std::ostream& os) const {
101 37 os << "ActivationModelQuad {nr=" << nr_ << "}";
102 37 }
103
104 protected:
105 using Base::nr_;
106
107 private:
108 VectorXs weights_;
109 bool new_weights_;
110
111 #ifndef NDEBUG
112 MatrixXs Arr_;
113 #endif
114 };
115
116 template <typename _Scalar>
117 struct ActivationDataWeightedQuadTpl
118 : public ActivationDataAbstractTpl<_Scalar> {
119 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
120
121 typedef _Scalar Scalar;
122 typedef MathBaseTpl<Scalar> MathBase;
123 typedef typename MathBase::VectorXs VectorXs;
124 typedef ActivationDataAbstractTpl<Scalar> Base;
125
126 template <typename Activation>
127 7839 explicit ActivationDataWeightedQuadTpl(Activation* const activation)
128
2/4
✓ Branch 3 taken 7839 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 7839 times.
✗ Branch 7 not taken.
7839 : Base(activation), Wr(VectorXs::Zero(activation->get_nr())) {}
129
130 VectorXs Wr;
131 };
132
133 } // namespace crocoddyl
134
135 #endif // CROCODDYL_CORE_ACTIVATIONS_WEIGHTED_QUADRATIC_HPP_
136