crocoddyl 1.9.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
 
Loading...
Searching...
No Matches
weighted-quadratic.hpp
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.
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/fwd.hpp"
15#include "crocoddyl/core/utils/exception.hpp"
16#include "crocoddyl/core/activation-base.hpp"
17
18namespace crocoddyl {
19
20template <typename _Scalar>
22 public:
23 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
24
25 typedef _Scalar Scalar;
30 typedef typename MathBase::VectorXs VectorXs;
31 typedef typename MathBase::MatrixXs MatrixXs;
32
33 explicit ActivationModelWeightedQuadTpl(const VectorXs& weights)
34 : Base(weights.size()), weights_(weights), new_weights_(false){};
36
37 virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data, 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 " + std::to_string(nr_) + ")");
41 }
42 boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
43
44 d->Wr = weights_.cwiseProduct(r);
45 data->a_value = Scalar(0.5) * r.dot(d->Wr);
46 };
47
48 virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data, const Eigen::Ref<const VectorXs>& r) {
49 if (static_cast<std::size_t>(r.size()) != nr_) {
50 throw_pretty("Invalid argument: "
51 << "r has wrong dimension (it should be " + std::to_string(nr_) + ")");
52 }
53
54 boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
55 data->Ar = d->Wr;
56 if (new_weights_) {
57 data->Arr.diagonal() = weights_;
58 new_weights_ = false;
59 }
60 // The Hessian has constant values which were set in createData.
61#ifndef NDEBUG
62 assert_pretty(MatrixXs(data->Arr).isApprox(Arr_), "Arr has wrong value");
63#endif
64 };
65
66 virtual boost::shared_ptr<ActivationDataAbstract> createData() {
67 boost::shared_ptr<Data> data = boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
68 data->Arr.diagonal() = weights_;
69
70#ifndef NDEBUG
71 Arr_ = data->Arr;
72#endif
73
74 return data;
75 };
76
77 const VectorXs& get_weights() const { return weights_; };
78 void set_weights(const VectorXs& weights) {
79 if (weights.size() != weights_.size()) {
80 throw_pretty("Invalid argument: "
81 << "weight vector has wrong dimension (it should be " + std::to_string(weights_.size()) + ")");
82 }
83
84 weights_ = weights;
85 new_weights_ = true;
86 };
87
93 virtual void print(std::ostream& os) const { os << "ActivationModelQuad {nr=" << nr_ << "}"; }
94
95 protected:
96 using Base::nr_;
97
98 private:
99 VectorXs weights_;
100 bool new_weights_;
101
102#ifndef NDEBUG
103 MatrixXs Arr_;
104#endif
105};
106
107template <typename _Scalar>
109 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
110
111 typedef _Scalar Scalar;
113 typedef typename MathBase::VectorXs VectorXs;
115
116 template <typename Activation>
117 explicit ActivationDataWeightedQuadTpl(Activation* const activation)
118 : Base(activation), Wr(VectorXs::Zero(activation->get_nr())) {}
119
120 VectorXs Wr;
121};
122
123} // namespace crocoddyl
124
125#endif // CROCODDYL_CORE_ACTIVATIONS_WEIGHTED_QUADRATIC_HPP_
virtual void print(std::ostream &os) const
Print relevant information of the quadratic-weighted model.