GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/crocoddyl/core/numdiff/control.hpp Lines: 9 11 81.8 %
Date: 2024-02-13 11:12:33 Branches: 7 12 58.3 %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2021-2023, LAAS-CNRS, New York University, Max Planck
5
// Gesellschaft,
6
//                          University of Edinburgh, University of Trento,
7
//                          Heriot-Watt University
8
// Copyright note valid unless otherwise stated in individual files.
9
// All rights reserved.
10
///////////////////////////////////////////////////////////////////////////////
11
12
#ifndef CROCODDYL_CORE_NUMDIFF_CONTROL_HPP_
13
#define CROCODDYL_CORE_NUMDIFF_CONTROL_HPP_
14
15
#include <boost/make_shared.hpp>
16
#include <boost/shared_ptr.hpp>
17
#include <vector>
18
19
#include "crocoddyl/core/control-base.hpp"
20
#include "crocoddyl/core/fwd.hpp"
21
22
namespace crocoddyl {
23
24
template <typename _Scalar>
25
class ControlParametrizationModelNumDiffTpl
26
    : public ControlParametrizationModelAbstractTpl<_Scalar> {
27
 public:
28
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
29
30
  typedef _Scalar Scalar;
31
  typedef MathBaseTpl<Scalar> MathBase;
32
  typedef ControlParametrizationModelAbstractTpl<_Scalar> Base;
33
  typedef ControlParametrizationDataNumDiffTpl<_Scalar> Data;
34
  typedef ControlParametrizationDataAbstractTpl<_Scalar>
35
      ControlParametrizationDataAbstract;
36
  typedef typename MathBase::VectorXs VectorXs;
37
  typedef typename MathBase::MatrixXs MatrixXs;
38
39
  /**
40
   * @brief Construct a new ControlParametrizationModelNumDiff object
41
   *
42
   * @param model
43
   */
44
  explicit ControlParametrizationModelNumDiffTpl(boost::shared_ptr<Base> model);
45
  virtual ~ControlParametrizationModelNumDiffTpl();
46
47
  /**
48
   * @brief Get the value of the control at the specified time
49
   *
50
   * @param[in]  data   Control-parametrization numdiff data
51
   * @param[in]  t      Time in [0,1]
52
   * @param[in]  u      Control parameters
53
   */
54
  void calc(const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
55
            const Scalar t, const Eigen::Ref<const VectorXs>& u) const;
56
57
  /**
58
   * @brief Get the value of the Jacobian of the control with respect to the
59
   * parameters
60
   *
61
   * @param[in]  data   Control-parametrization numdiff data
62
   * @param[in]  t      Time in [0,1]
63
   * @param[in]  u      Control parameters
64
   */
65
  void calcDiff(
66
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
67
      const Scalar t, const Eigen::Ref<const VectorXs>& u) const;
68
69
  /**
70
   * @brief Create the control-parametrization data
71
   *
72
   * @return the control-parametrization data
73
   */
74
  virtual boost::shared_ptr<ControlParametrizationDataAbstract> createData();
75
76
  /**
77
   * @brief Get a value of the control parameters such that the control at the
78
   * specified time t is equal to the specified value u
79
   *
80
   * @param[in]  data   Control-parametrization numdiff data
81
   * @param[in]  t      Time in [0,1]
82
   * @param[in]  w      Control values
83
   */
84
  void params(const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
85
              const Scalar t, const Eigen::Ref<const VectorXs>& w) const;
86
87
  /**
88
   * @brief Convert the bounds on the control to bounds on the control
89
   * parameters
90
   *
91
   * @param[in]  w_lb   Control lower bound
92
   * @param[in]  w_ub   Control upper bound
93
   * @param[in]  u_lb   Control parameter lower bound
94
   * @param[in]  u_ub   Control parameter upper bound
95
   */
96
  void convertBounds(const Eigen::Ref<const VectorXs>& w_lb,
97
                     const Eigen::Ref<const VectorXs>& w_ub,
98
                     Eigen::Ref<VectorXs> u_lb,
99
                     Eigen::Ref<VectorXs> u_ub) const;
100
101
  /**
102
   * @brief Compute the product between a specified matrix and the Jacobian of
103
   * the control (with respect to the parameters)
104
   *
105
   * @param[in]  data   Control-parametrization numdiff data
106
   * @param[in]  A      A matrix to multiply times the Jacobian
107
   * @param[out] out    Product between the matrix A and the Jacobian of the
108
   * control with respect to the parameters
109
   * @param[in] op      Assignment operator which sets, adds, or removes the
110
   * given results
111
   */
112
  void multiplyByJacobian(
113
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
114
      const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
115
      const AssignmentOp = setto) const;
116
117
  /**
118
   * @brief Compute the product between the transposed Jacobian of the control
119
   * (with respect to the parameters) and a specified matrix
120
   *
121
   * @param[in]  data   Control-parametrization numdiff data
122
   * @param[in]  A      A matrix to multiply times the Jacobian
123
   * @param[out] out    Product between the transposed Jacobian of the control
124
   * with respect to the parameters and the matrix A
125
   * @param[in] op      Assignment operator which sets, adds, or removes the
126
   * given results
127
   */
128
  void multiplyJacobianTransposeBy(
129
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
130
      const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
131
      const AssignmentOp = setto) const;
132
133
  /**
134
   * @brief Get the model_ object
135
   *
136
   * @return Base&
137
   */
138
  const boost::shared_ptr<Base>& get_model() const;
139
140
  /**
141
   * @brief Return the disturbance constant used in the numerical
142
   * differentiation routine
143
   */
144
  const Scalar get_disturbance() const;
145
146
  /**
147
   * @brief Modify the disturbance constant used in the numerical
148
   * differentiation routine
149
   */
150
  void set_disturbance(const Scalar disturbance);
151
152
 private:
153
  boost::shared_ptr<Base>
154
      model_;     //!< model we need to compute the numerical differentiation
155
  Scalar e_jac_;  //!< Constant used for computing disturbances in Jacobian
156
                  //!< calculation
157
158
 protected:
159
  using Base::nu_;
160
  using Base::nw_;
161
};
162
163
template <typename _Scalar>
164
struct ControlParametrizationDataNumDiffTpl
165
    : public ControlParametrizationDataAbstractTpl<_Scalar> {
166
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
167
168
  typedef _Scalar Scalar;
169
  typedef MathBaseTpl<Scalar> MathBase;
170
  typedef typename MathBase::VectorXs VectorXs;
171
  typedef typename MathBase::MatrixXs MatrixXs;
172
  typedef ControlParametrizationDataAbstractTpl<Scalar> Base;
173
174
  template <template <typename Scalar> class Model>
175
12
  explicit ControlParametrizationDataNumDiffTpl(Model<Scalar>* const model)
176
12
      : Base(model), du(model->get_nu()) {
177
12
    du.setZero();
178
179
12
    const std::size_t nu = model->get_model()->get_nu();
180
12
    data_0 = model->get_model()->createData();
181
282
    for (std::size_t i = 0; i < nu; ++i) {
182

270
      data_u.push_back(model->get_model()->createData());
183
    }
184
12
  }
185
186
24
  virtual ~ControlParametrizationDataNumDiffTpl() {}
187
188
  VectorXs du;  //!< temporary variable used for finite differencing
189
  boost::shared_ptr<Base> data_0;  //!< The data that contains the final results
190
  std::vector<boost::shared_ptr<Base> >
191
      data_u;  //!< The temporary data associated with the control variation
192
};
193
194
}  // namespace crocoddyl
195
196
/* --- Details -------------------------------------------------------------- */
197
/* --- Details -------------------------------------------------------------- */
198
/* --- Details -------------------------------------------------------------- */
199
#include "crocoddyl/core/numdiff/control.hxx"
200
201
#endif  // CROCODDYL_CORE_NUMDIFF_CONTROL_HPP_