Crocoddyl
quadratic.hpp
1 // 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.
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>
23  public:
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25 
26  typedef _Scalar Scalar;
30  typedef typename MathBase::VectorXs VectorXs;
31  typedef typename MathBase::MatrixXs MatrixXs;
32 
33  explicit ActivationModelQuadTpl(const std::size_t nr) : Base(nr) {};
34  virtual ~ActivationModelQuadTpl() {};
35 
36  virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data,
37  const Eigen::Ref<const VectorXs>& r) {
38  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  data->a_value = Scalar(0.5) * r.dot(r);
44  };
45 
46  virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data,
47  const Eigen::Ref<const VectorXs>& r) {
48  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  data->Ar = r;
55  // The Hessian has constant values which were set in createData.
56  assert_pretty(MatrixXs(data->Arr).isApprox(MatrixXs::Identity(nr_, nr_)),
57  "Arr has wrong value");
58  };
59 
60  virtual boost::shared_ptr<ActivationDataAbstract> createData() {
61  boost::shared_ptr<ActivationDataAbstract> data =
62  boost::allocate_shared<ActivationDataAbstract>(
63  Eigen::aligned_allocator<ActivationDataAbstract>(), this);
64  data->Arr.diagonal().setOnes();
65  return data;
66  };
67 
73  virtual void print(std::ostream& os) const {
74  os << "ActivationModelQuad {nr=" << nr_ << "}";
75  }
76 
77  protected:
78  using Base::nr_;
79 };
80 
81 } // namespace crocoddyl
82 
83 #endif // CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_HPP_
virtual void print(std::ostream &os) const
Print relevant information of the quadratic model.
Definition: quadratic.hpp:73