GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/crocoddyl/core/integrator/euler.hpp Lines: 11 13 84.6 %
Date: 2024-02-13 11:12:33 Branches: 11 22 50.0 %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2019-2022, LAAS-CNRS, University of Edinburgh,
5
//                          University of Oxford, Heriot-Watt University
6
// Copyright note valid unless otherwise stated in individual files.
7
// All rights reserved.
8
///////////////////////////////////////////////////////////////////////////////
9
10
#ifndef CROCODDYL_CORE_INTEGRATOR_EULER_HPP_
11
#define CROCODDYL_CORE_INTEGRATOR_EULER_HPP_
12
13
#include "crocoddyl/core/fwd.hpp"
14
#include "crocoddyl/core/integ-action-base.hpp"
15
16
namespace crocoddyl {
17
18
/**
19
 * @brief Symplectic Euler integrator
20
 *
21
 * It applies a symplectic Euler integration scheme to a differential (i.e.,
22
 * continuous time) action model.
23
 *
24
 * This symplectic Euler scheme introduces also the possibility to parametrize
25
 * the control trajectory inside an integration step, for instance using
26
 * polynomials. This requires introducing some notation to clarify the
27
 * difference between the control inputs of the differential model and the
28
 * control inputs to the integrated model. We have decided to use
29
 * \f$\mathbf{w}\f$ to refer to the control inputs of the differential model and
30
 * \f$\mathbf{u}\f$ for the control inputs of the integrated action model. Note
31
 * that the zero-order (e.g., `ControlParametrizationModelPolyZeroTpl`) are the
32
 * only ones that make sense to use within this integrator.
33
 *
34
 * \sa `calc()`, `calcDiff()`, `createData()`
35
 */
36
template <typename _Scalar>
37
class IntegratedActionModelEulerTpl
38
    : public IntegratedActionModelAbstractTpl<_Scalar> {
39
 public:
40
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
41
42
  typedef _Scalar Scalar;
43
  typedef MathBaseTpl<Scalar> MathBase;
44
  typedef IntegratedActionModelAbstractTpl<Scalar> Base;
45
  typedef IntegratedActionDataEulerTpl<Scalar> Data;
46
  typedef ActionDataAbstractTpl<Scalar> ActionDataAbstract;
47
  typedef DifferentialActionModelAbstractTpl<Scalar>
48
      DifferentialActionModelAbstract;
49
  typedef ControlParametrizationModelAbstractTpl<Scalar>
50
      ControlParametrizationModelAbstract;
51
  typedef ControlParametrizationDataAbstractTpl<Scalar>
52
      ControlParametrizationDataAbstract;
53
  typedef typename MathBase::VectorXs VectorXs;
54
  typedef typename MathBase::MatrixXs MatrixXs;
55
56
  /**
57
   * @brief Initialize the symplectic Euler integrator
58
   *
59
   * @param[in] model               Differential action model
60
   * @param[in] control             Control parametrization
61
   * @param[in] time_step           Step time (default 1e-3)
62
   * @param[in] with_cost_residual  Compute cost residual (default true)
63
   */
64
  IntegratedActionModelEulerTpl(
65
      boost::shared_ptr<DifferentialActionModelAbstract> model,
66
      boost::shared_ptr<ControlParametrizationModelAbstract> control,
67
      const Scalar time_step = Scalar(1e-3),
68
      const bool with_cost_residual = true);
69
70
  /**
71
   * @brief Initialize the symplectic Euler integrator
72
   *
73
   * This initialization uses `ControlParametrizationPolyZeroTpl` for the
74
   * control parametrization.
75
   *
76
   * @param[in] model               Differential action model
77
   * @param[in] time_step           Step time (default 1e-3)
78
   * @param[in] with_cost_residual  Compute cost residual (default true)
79
   */
80
  IntegratedActionModelEulerTpl(
81
      boost::shared_ptr<DifferentialActionModelAbstract> model,
82
      const Scalar time_step = Scalar(1e-3),
83
      const bool with_cost_residual = true);
84
  virtual ~IntegratedActionModelEulerTpl();
85
86
  /**
87
   * @brief Integrate the differential action model using symplectic Euler
88
   * scheme
89
   *
90
   * @param[in] data  Symplectic Euler data
91
   * @param[in] x     State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
92
   * @param[in] u     Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
93
   */
94
  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
95
                    const Eigen::Ref<const VectorXs>& x,
96
                    const Eigen::Ref<const VectorXs>& u);
97
98
  /**
99
   * @brief Integrate the total cost value for nodes that depends only on the
100
   * state using symplectic Euler scheme
101
   *
102
   * It computes the total cost and defines the next state as the current one.
103
   * This function is used in the terminal nodes of an optimal control problem.
104
   *
105
   * @param[in] data  Symplectic Euler data
106
   * @param[in] x     State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
107
   */
108
  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
109
                    const Eigen::Ref<const VectorXs>& x);
110
111
  /**
112
   * @brief Compute the partial derivatives of the symplectic Euler integrator
113
   *
114
   * @param[in] data  Symplectic Euler data
115
   * @param[in] x     State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
116
   * @param[in] u     Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
117
   */
118
  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
119
                        const Eigen::Ref<const VectorXs>& x,
120
                        const Eigen::Ref<const VectorXs>& u);
121
122
  /**
123
   * @brief Compute the partial derivatives of the cost
124
   *
125
   * It updates the derivatives of the cost function with respect to the state
126
   * only. This function is used in the terminal nodes of an optimal control
127
   * problem.
128
   *
129
   * @param[in] data  Symplectic Euler data
130
   * @param[in] x     State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
131
   */
132
  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
133
                        const Eigen::Ref<const VectorXs>& x);
134
135
  /**
136
   * @brief Create the symplectic Euler data
137
   *
138
   * @return the symplectic Euler data
139
   */
140
  virtual boost::shared_ptr<ActionDataAbstract> createData();
141
142
  /**
143
   * @brief Checks that a specific data belongs to this model
144
   */
145
  virtual bool checkData(const boost::shared_ptr<ActionDataAbstract>& data);
146
147
  /**
148
   * @brief Computes the quasic static commands
149
   *
150
   * The quasic static commands are the ones produced for a the reference
151
   * posture as an equilibrium point, i.e. for
152
   * \f$\mathbf{f^q_x}\delta\mathbf{q}+\mathbf{f_u}\delta\mathbf{u}=\mathbf{0}\f$
153
   *
154
   * @param[in] data    Symplectic Euler data
155
   * @param[out] u      Quasic static commands
156
   * @param[in] x       State point (velocity has to be zero)
157
   * @param[in] maxiter Maximum allowed number of iterations
158
   * @param[in] tol     Tolerance
159
   */
160
  virtual void quasiStatic(const boost::shared_ptr<ActionDataAbstract>& data,
161
                           Eigen::Ref<VectorXs> u,
162
                           const Eigen::Ref<const VectorXs>& x,
163
                           const std::size_t maxiter = 100,
164
                           const Scalar tol = Scalar(1e-9));
165
166
  /**
167
   * @brief Print relevant information of the Euler integrator model
168
   *
169
   * @param[out] os  Output stream object
170
   */
171
  virtual void print(std::ostream& os) const;
172
173
 protected:
174
  using Base::control_;       //!< Control parametrization
175
  using Base::differential_;  //!< Differential action model
176
  using Base::ng_;            //!< Number of inequality constraints
177
  using Base::nh_;            //!< Number of equality constraints
178
  using Base::nu_;            //!< Dimension of the control
179
  using Base::state_;         //!< Model of the state
180
  using Base::time_step2_;    //!< Square of the time step used for integration
181
  using Base::time_step_;     //!< Time step used for integration
182
  using Base::with_cost_residual_;  //!< Flag indicating whether a cost residual
183
                                    //!< is used
184
};
185
186
template <typename _Scalar>
187
struct IntegratedActionDataEulerTpl
188
    : public IntegratedActionDataAbstractTpl<_Scalar> {
189
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
190
191
  typedef _Scalar Scalar;
192
  typedef MathBaseTpl<Scalar> MathBase;
193
  typedef IntegratedActionDataAbstractTpl<Scalar> Base;
194
  typedef DifferentialActionDataAbstractTpl<Scalar>
195
      DifferentialActionDataAbstract;
196
  typedef ControlParametrizationDataAbstractTpl<Scalar>
197
      ControlParametrizationDataAbstract;
198
  typedef typename MathBase::VectorXs VectorXs;
199
  typedef typename MathBase::MatrixXs MatrixXs;
200
201
  template <template <typename Scalar> class Model>
202
7410
  explicit IntegratedActionDataEulerTpl(Model<Scalar>* const model)
203

7410
      : Base(model) {
204
7410
    differential = model->get_differential()->createData();
205
7410
    control = model->get_control()->createData();
206
7410
    const std::size_t ndx = model->get_state()->get_ndx();
207
7410
    const std::size_t nv = model->get_state()->get_nv();
208

7410
    dx = VectorXs::Zero(ndx);
209

7410
    da_du = MatrixXs::Zero(nv, model->get_nu());
210

7410
    Lwu = MatrixXs::Zero(model->get_control()->get_nw(), model->get_nu());
211
7410
  }
212
14824
  virtual ~IntegratedActionDataEulerTpl() {}
213
214
  boost::shared_ptr<DifferentialActionDataAbstract>
215
      differential;  //!< Differential model data
216
  boost::shared_ptr<ControlParametrizationDataAbstract>
217
      control;  //!< Control parametrization data
218
  VectorXs dx;
219
  MatrixXs da_du;
220
  MatrixXs Lwu;  //!< Hessian of the cost function with respect to the control
221
                 //!< input (w) and control parameters (u)
222
223
  using Base::cost;
224
  using Base::Fu;
225
  using Base::Fx;
226
  using Base::Lu;
227
  using Base::Luu;
228
  using Base::Lx;
229
  using Base::Lxu;
230
  using Base::Lxx;
231
  using Base::r;
232
  using Base::xnext;
233
};
234
235
}  // namespace crocoddyl
236
237
/* --- Details -------------------------------------------------------------- */
238
/* --- Details -------------------------------------------------------------- */
239
/* --- Details -------------------------------------------------------------- */
240
#include "crocoddyl/core/integrator/euler.hxx"
241
242
#endif  // CROCODDYL_CORE_INTEGRATOR_EULER_HPP_