1 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// BSD 3-Clause License |
3 |
|
|
// |
4 |
|
|
// Copyright (C) 2019-2023, 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 |
|
|
#ifndef CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_HPP_ |
11 |
|
|
#define CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_HPP_ |
12 |
|
|
|
13 |
|
|
#include <stdexcept> |
14 |
|
|
|
15 |
|
|
#include "crocoddyl/core/activation-base.hpp" |
16 |
|
|
#include "crocoddyl/core/fwd.hpp" |
17 |
|
|
#include "crocoddyl/core/utils/exception.hpp" |
18 |
|
|
|
19 |
|
|
namespace crocoddyl { |
20 |
|
|
|
21 |
|
|
template <typename _Scalar> |
22 |
|
|
class ActivationModelQuadTpl : 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 typename MathBase::VectorXs VectorXs; |
31 |
|
|
typedef typename MathBase::MatrixXs MatrixXs; |
32 |
|
|
|
33 |
|
4468 |
explicit ActivationModelQuadTpl(const std::size_t nr) : Base(nr){}; |
34 |
|
8940 |
virtual ~ActivationModelQuadTpl(){}; |
35 |
|
|
|
36 |
|
295719 |
virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data, |
37 |
|
|
const Eigen::Ref<const VectorXs>& r) { |
38 |
✗✓ |
295719 |
if (static_cast<std::size_t>(r.size()) != nr_) { |
39 |
|
|
throw_pretty("Invalid argument: " |
40 |
|
|
<< "r has wrong dimension (it should be " + |
41 |
|
|
std::to_string(nr_) + ")"); |
42 |
|
|
} |
43 |
|
295719 |
data->a_value = Scalar(0.5) * r.dot(r); |
44 |
|
295719 |
}; |
45 |
|
|
|
46 |
|
50359 |
virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data, |
47 |
|
|
const Eigen::Ref<const VectorXs>& r) { |
48 |
✗✓ |
50359 |
if (static_cast<std::size_t>(r.size()) != nr_) { |
49 |
|
|
throw_pretty("Invalid argument: " |
50 |
|
|
<< "r has wrong dimension (it should be " + |
51 |
|
|
std::to_string(nr_) + ")"); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
50359 |
data->Ar = r; |
55 |
|
|
// The Hessian has constant values which were set in createData. |
56 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✗✗ |
50359 |
assert_pretty(MatrixXs(data->Arr).isApprox(MatrixXs::Identity(nr_, nr_)), |
57 |
|
|
"Arr has wrong value"); |
58 |
|
50359 |
}; |
59 |
|
|
|
60 |
|
341482 |
virtual boost::shared_ptr<ActivationDataAbstract> createData() { |
61 |
✓✗ |
341482 |
boost::shared_ptr<ActivationDataAbstract> data = |
62 |
|
|
boost::allocate_shared<ActivationDataAbstract>( |
63 |
|
|
Eigen::aligned_allocator<ActivationDataAbstract>(), this); |
64 |
✓✗ |
341482 |
data->Arr.diagonal().setOnes(); |
65 |
|
341482 |
return data; |
66 |
|
|
}; |
67 |
|
|
|
68 |
|
|
/** |
69 |
|
|
* @brief Print relevant information of the quadratic model |
70 |
|
|
* |
71 |
|
|
* @param[out] os Output stream object |
72 |
|
|
*/ |
73 |
|
39 |
virtual void print(std::ostream& os) const { |
74 |
|
39 |
os << "ActivationModelQuad {nr=" << nr_ << "}"; |
75 |
|
39 |
} |
76 |
|
|
|
77 |
|
|
protected: |
78 |
|
|
using Base::nr_; |
79 |
|
|
}; |
80 |
|
|
|
81 |
|
|
} // namespace crocoddyl |
82 |
|
|
|
83 |
|
|
#endif // CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_HPP_ |