Crocoddyl
weighted-quadratic.hpp
1 // 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.
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>
22  : public ActivationModelAbstractTpl<_Scalar> {
23  public:
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25 
26  typedef _Scalar Scalar;
31  typedef typename MathBase::VectorXs VectorXs;
32  typedef typename MathBase::MatrixXs MatrixXs;
33 
34  explicit ActivationModelWeightedQuadTpl(const VectorXs& weights)
35  : Base(weights.size()), weights_(weights), new_weights_(false){};
36  virtual ~ActivationModelWeightedQuadTpl(){};
37 
38  virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data,
39  const Eigen::Ref<const VectorXs>& r) {
40  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  boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
46 
47  d->Wr = weights_.cwiseProduct(r);
48  data->a_value = Scalar(0.5) * r.dot(d->Wr);
49  };
50 
51  virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data,
52  const Eigen::Ref<const VectorXs>& r) {
53  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  boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
60  data->Ar = d->Wr;
61  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  assert_pretty(MatrixXs(data->Arr).isApprox(Arr_), "Arr has wrong value");
68 #endif
69  };
70 
71  virtual boost::shared_ptr<ActivationDataAbstract> createData() {
72  boost::shared_ptr<Data> data =
73  boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
74  data->Arr.diagonal() = weights_;
75 
76 #ifndef NDEBUG
77  Arr_ = data->Arr;
78 #endif
79 
80  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 
100  virtual void print(std::ostream& os) const {
101  os << "ActivationModelQuad {nr=" << nr_ << "}";
102  }
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>
118  : public ActivationDataAbstractTpl<_Scalar> {
119  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
120 
121  typedef _Scalar Scalar;
123  typedef typename MathBase::VectorXs VectorXs;
125 
126  template <typename Activation>
127  explicit ActivationDataWeightedQuadTpl(Activation* const activation)
128  : 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_
virtual void print(std::ostream &os) const
Print relevant information of the quadratic-weighted model.