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