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  boost::shared_ptr<Base> model, const bool with_gauss_approx = false);
72 
76  virtual void calc(
77  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
78  const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u);
79 
85  virtual void calc(
86  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
87  const Eigen::Ref<const VectorXs>& x);
88 
92  virtual void calcDiff(
93  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
94  const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u);
95 
101  virtual void calcDiff(
102  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
103  const Eigen::Ref<const VectorXs>& x);
104 
108  virtual boost::shared_ptr<DifferentialActionDataAbstract> createData();
109 
113  virtual void quasiStatic(
114  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
115  Eigen::Ref<VectorXs> u, const Eigen::Ref<const VectorXs>& x,
116  const std::size_t maxiter = 100, const Scalar tol = Scalar(1e-9));
117 
122  const boost::shared_ptr<Base>& get_model() const;
123 
128  const Scalar get_disturbance() const;
129 
134  void set_disturbance(const Scalar disturbance);
135 
140 
146  virtual void print(std::ostream& os) const;
147 
148  protected:
151  using Base::nr_;
152  using Base::nu_;
153  using Base::state_;
154  using Base::u_lb_;
155  using Base::u_ub_;
156 
157  private:
158  void assertStableStateFD(const Eigen::Ref<const VectorXs>& x);
159  boost::shared_ptr<Base> model_;
160  bool with_gauss_approx_;
161  Scalar e_jac_;
163  Scalar e_hess_;
165 };
166 
167 template <typename _Scalar>
169  : public DifferentialActionDataAbstractTpl<_Scalar> {
170  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
171 
172  typedef _Scalar Scalar;
175  typedef typename MathBase::VectorXs VectorXs;
176  typedef typename MathBase::MatrixXs MatrixXs;
177 
184  template <template <typename Scalar> class Model>
185  explicit DifferentialActionDataNumDiffTpl(Model<Scalar>* const model)
186  : Base(model),
187  Rx(model->get_model()->get_nr(),
188  model->get_model()->get_state()->get_ndx()),
189  Ru(model->get_model()->get_nr(), model->get_model()->get_nu()),
190  dx(model->get_model()->get_state()->get_ndx()),
191  du(model->get_model()->get_nu()),
192  xp(model->get_model()->get_state()->get_nx()) {
193  Rx.setZero();
194  Ru.setZero();
195  dx.setZero();
196  du.setZero();
197  xp.setZero();
198 
199  const std::size_t ndx = model->get_model()->get_state()->get_ndx();
200  const std::size_t nu = model->get_model()->get_nu();
201  data_0 = model->get_model()->createData();
202  for (std::size_t i = 0; i < ndx; ++i) {
203  data_x.push_back(model->get_model()->createData());
204  }
205  for (std::size_t i = 0; i < nu; ++i) {
206  data_u.push_back(model->get_model()->createData());
207  }
208  }
209 
210  Scalar x_norm;
211  Scalar
213  Scalar
215  Scalar xh_hess;
217  Scalar uh_hess;
219  Scalar xh_hess_pow2;
220  Scalar uh_hess_pow2;
221  Scalar xuh_hess_pow2;
222  MatrixXs Rx;
223  MatrixXs Ru;
224  VectorXs dx;
225  VectorXs du;
226  VectorXs xp;
227  boost::shared_ptr<Base> data_0;
228  std::vector<boost::shared_ptr<Base> > data_x;
229  std::vector<boost::shared_ptr<Base> > data_u;
230 
231  using Base::cost;
232  using Base::Fu;
233  using Base::Fx;
234  using Base::g;
235  using Base::Gu;
236  using Base::Gx;
237  using Base::h;
238  using Base::Hu;
239  using Base::Hx;
240  using Base::Lu;
241  using Base::Luu;
242  using Base::Lx;
243  using Base::Lxu;
244  using Base::Lxx;
245  using Base::r;
246  using Base::xout;
247 };
248 
249 } // namespace crocoddyl
250 
251 /* --- Details -------------------------------------------------------------- */
252 /* --- Details -------------------------------------------------------------- */
253 /* --- Details -------------------------------------------------------------- */
254 #include "crocoddyl/core/numdiff/diff-action.hxx"
255 
256 #endif // CROCODDYL_CORE_NUMDIFF_DIFF_ACTION_HPP_
Abstract class for differential action model.
boost::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
virtual void calc(const boost::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
virtual void calcDiff(const boost::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.
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.
void set_disturbance(const Scalar disturbance)
Modify the disturbance constant used in the numerical differentiation routine.
virtual boost::shared_ptr< DifferentialActionDataAbstract > createData()
Create the differential action data.
virtual void print(std::ostream &os) const
Print relevant information of the action numdiff model.
const boost::shared_ptr< Base > & get_model() const
Return the differential acton model that we use to numerical differentiate.
virtual void calc(const boost::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 boost::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.
DifferentialActionModelNumDiffTpl(boost::shared_ptr< Base > model, const bool with_gauss_approx=false)
Initialize the numdiff differential action model.
virtual void calcDiff(const boost::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 .