Crocoddyl
diff-action.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2024, LAAS-CNRS, University of Edinburgh,
5 // New York University, Max Planck Gesellschaft
6 // Heriot-Watt University
7 // Copyright note valid unless otherwise stated in individual files.
8 // All rights reserved.
10 
11 #ifndef CROCODDYL_CORE_NUMDIFF_DIFF_ACTION_HPP_
12 #define CROCODDYL_CORE_NUMDIFF_DIFF_ACTION_HPP_
13 
14 #include <iostream>
15 #include <vector>
16 
17 #include "crocoddyl/core/diff-action-base.hpp"
18 
19 namespace crocoddyl {
20 
46 template <typename _Scalar>
48  : public DifferentialActionModelAbstractTpl<_Scalar> {
49  public:
50  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
51 
52  typedef _Scalar Scalar;
58  typedef typename MathBase::VectorXs VectorXs;
59  typedef typename MathBase::MatrixXs MatrixXs;
60 
70  std::shared_ptr<Base> model, const bool with_gauss_approx = false);
72 
76  virtual void calc(const std::shared_ptr<DifferentialActionDataAbstract>& data,
77  const Eigen::Ref<const VectorXs>& x,
78  const Eigen::Ref<const VectorXs>& u);
79 
85  virtual void calc(const std::shared_ptr<DifferentialActionDataAbstract>& data,
86  const Eigen::Ref<const VectorXs>& x);
87 
91  virtual void calcDiff(
92  const std::shared_ptr<DifferentialActionDataAbstract>& data,
93  const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u);
94 
100  virtual void calcDiff(
101  const std::shared_ptr<DifferentialActionDataAbstract>& data,
102  const Eigen::Ref<const VectorXs>& x);
103 
107  virtual std::shared_ptr<DifferentialActionDataAbstract> createData();
108 
112  virtual void quasiStatic(
113  const std::shared_ptr<DifferentialActionDataAbstract>& data,
114  Eigen::Ref<VectorXs> u, const Eigen::Ref<const VectorXs>& x,
115  const std::size_t maxiter = 100, const Scalar tol = Scalar(1e-9));
116 
121  const std::shared_ptr<Base>& get_model() const;
122 
127  const Scalar get_disturbance() const;
128 
133  void set_disturbance(const Scalar disturbance);
134 
139 
145  virtual void print(std::ostream& os) const;
146 
147  protected:
150  using Base::nr_;
151  using Base::nu_;
152  using Base::state_;
153  using Base::u_lb_;
154  using Base::u_ub_;
155 
156  private:
157  void assertStableStateFD(const Eigen::Ref<const VectorXs>& x);
158  std::shared_ptr<Base> model_;
159  bool with_gauss_approx_;
160  Scalar e_jac_;
162  Scalar e_hess_;
164 };
165 
166 template <typename _Scalar>
168  : public DifferentialActionDataAbstractTpl<_Scalar> {
169  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
170 
171  typedef _Scalar Scalar;
174  typedef typename MathBase::VectorXs VectorXs;
175  typedef typename MathBase::MatrixXs MatrixXs;
176 
183  template <template <typename Scalar> class Model>
184  explicit DifferentialActionDataNumDiffTpl(Model<Scalar>* const model)
185  : Base(model),
186  Rx(model->get_model()->get_nr(),
187  model->get_model()->get_state()->get_ndx()),
188  Ru(model->get_model()->get_nr(), model->get_model()->get_nu()),
189  dx(model->get_model()->get_state()->get_ndx()),
190  du(model->get_model()->get_nu()),
191  xp(model->get_model()->get_state()->get_nx()) {
192  Rx.setZero();
193  Ru.setZero();
194  dx.setZero();
195  du.setZero();
196  xp.setZero();
197 
198  const std::size_t ndx = model->get_model()->get_state()->get_ndx();
199  const std::size_t nu = model->get_model()->get_nu();
200  data_0 = model->get_model()->createData();
201  for (std::size_t i = 0; i < ndx; ++i) {
202  data_x.push_back(model->get_model()->createData());
203  }
204  for (std::size_t i = 0; i < nu; ++i) {
205  data_u.push_back(model->get_model()->createData());
206  }
207  }
208 
209  Scalar x_norm;
210  Scalar
212  Scalar
214  Scalar xh_hess;
216  Scalar uh_hess;
218  Scalar xh_hess_pow2;
219  Scalar uh_hess_pow2;
220  Scalar xuh_hess_pow2;
221  MatrixXs Rx;
222  MatrixXs Ru;
223  VectorXs dx;
224  VectorXs du;
225  VectorXs xp;
226  std::shared_ptr<Base> data_0;
227  std::vector<std::shared_ptr<Base> > data_x;
228  std::vector<std::shared_ptr<Base> > data_u;
229 
230  using Base::cost;
231  using Base::Fu;
232  using Base::Fx;
233  using Base::g;
234  using Base::Gu;
235  using Base::Gx;
236  using Base::h;
237  using Base::Hu;
238  using Base::Hx;
239  using Base::Lu;
240  using Base::Luu;
241  using Base::Lx;
242  using Base::Lxu;
243  using Base::Lxx;
244  using Base::r;
245  using Base::xout;
246 };
247 
248 } // namespace crocoddyl
249 
250 /* --- Details -------------------------------------------------------------- */
251 /* --- Details -------------------------------------------------------------- */
252 /* --- Details -------------------------------------------------------------- */
253 #include "crocoddyl/core/numdiff/diff-action.hxx"
254 
255 #endif // CROCODDYL_CORE_NUMDIFF_DIFF_ACTION_HPP_
Abstract class for differential action model.
std::shared_ptr< StateAbstract > state_
Model of the state.
std::size_t nr_
Dimension of the cost residual.
This class computes the numerical differentiation of a differential action model.
Definition: diff-action.hpp:48
const std::shared_ptr< Base > & get_model() const
Return the differential acton model that we use to numerical differentiate.
const Scalar get_disturbance() const
Return the disturbance constant used in the numerical differentiation routine.
bool get_with_gauss_approx()
Identify if the Gauss approximation is going to be used or not.
virtual std::shared_ptr< DifferentialActionDataAbstract > createData()
Create the differential action data.
void set_disturbance(const Scalar disturbance)
Modify the disturbance constant used in the numerical differentiation routine.
virtual void print(std::ostream &os) const
Print relevant information of the action numdiff model.
DifferentialActionModelNumDiffTpl(std::shared_ptr< Base > model, const bool with_gauss_approx=false)
Initialize the numdiff differential action model.
virtual void calcDiff(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the derivatives of the dynamics and cost functions.
virtual void calc(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the system acceleration and cost value.
virtual void quasiStatic(const std::shared_ptr< DifferentialActionDataAbstract > &data, Eigen::Ref< VectorXs > u, const Eigen::Ref< const VectorXs > &x, const std::size_t maxiter=100, const Scalar tol=Scalar(1e-9))
Computes the quasic static commands.
virtual void calcDiff(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
virtual void calc(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
MatrixXs Fx
Jacobian of the dynamics w.r.t. the state .
MatrixXs Fu
Jacobian of the dynamics w.r.t. the control .
VectorXs h
Equality constraint values.
MatrixXs Luu
Hessian of the cost w.r.t. the control .
VectorXs g
Inequality constraint values.
VectorXs Lx
Jacobian of the cost w.r.t. the state .
MatrixXs Lxx
Hessian of the cost w.r.t. the state .
VectorXs Lu
Jacobian of the cost w.r.t. the control .
DifferentialActionDataNumDiffTpl(Model< Scalar > *const model)
Construct a new ActionDataNumDiff object.
Scalar x_norm
Norm of the state vector.
Scalar uh_jac
Disturbance value used for computing .
Scalar xh_jac
Disturbance value used for computing .