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 |
✓✗✓✗
|
224 |
: Base(weights.size()), weights_(weights), new_weights_(false){}; |
36 |
|
452 |
virtual ~ActivationModelWeightedQuadTpl(){}; |
37 |
|
|
|
38 |
|
9675 |
virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data, |
39 |
|
|
const Eigen::Ref<const VectorXs>& r) { |
40 |
✓✗✗✓
|
9675 |
if (static_cast<std::size_t>(r.size()) != nr_) { |
41 |
|
|
throw_pretty("Invalid argument: " |
42 |
|
|
<< "r has wrong dimension (it should be " + |
43 |
|
|
std::to_string(nr_) + ")"); |
44 |
|
|
} |
45 |
|
9675 |
boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data); |
46 |
|
|
|
47 |
✓✗✓✗
|
9675 |
d->Wr = weights_.cwiseProduct(r); |
48 |
✓✗ |
9675 |
data->a_value = Scalar(0.5) * r.dot(d->Wr); |
49 |
|
9675 |
}; |
50 |
|
|
|
51 |
|
304 |
virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data, |
52 |
|
|
const Eigen::Ref<const VectorXs>& r) { |
53 |
✓✗✗✓
|
304 |
if (static_cast<std::size_t>(r.size()) != nr_) { |
54 |
|
|
throw_pretty("Invalid argument: " |
55 |
|
|
<< "r has wrong dimension (it should be " + |
56 |
|
|
std::to_string(nr_) + ")"); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
608 |
boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data); |
60 |
✓✗ |
304 |
data->Ar = d->Wr; |
61 |
✗✓ |
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 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✗✗ |
304 |
assert_pretty(MatrixXs(data->Arr).isApprox(Arr_), "Arr has wrong value"); |
68 |
|
|
#endif |
69 |
|
304 |
}; |
70 |
|
|
|
71 |
|
7839 |
virtual boost::shared_ptr<ActivationDataAbstract> createData() { |
72 |
✓✗ |
7839 |
boost::shared_ptr<Data> data = |
73 |
|
|
boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this); |
74 |
✓✗ |
7839 |
data->Arr.diagonal() = weights_; |
75 |
|
|
|
76 |
|
|
#ifndef NDEBUG |
77 |
✓✗ |
7839 |
Arr_ = data->Arr; |
78 |
|
|
#endif |
79 |
|
|
|
80 |
|
15678 |
return data; |
81 |
|
|
}; |
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 |
✓✗✓✗
|
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_ |