Crocoddyl
constraint.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2020-2023, University of Edinburgh, Heriot-Watt University
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_NUMDIFF_CONSTRAINT_HPP_
10 #define CROCODDYL_CORE_NUMDIFF_CONSTRAINT_HPP_
11 
12 #include <boost/function.hpp>
13 
14 #include "crocoddyl/core/constraint-base.hpp"
15 
16 namespace crocoddyl {
17 
29 template <typename _Scalar>
31  public:
32  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
33 
34  typedef _Scalar Scalar;
40  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
41  typedef boost::function<void(const VectorXs&, const VectorXs&)>
42  ReevaluationFunction;
43 
49  explicit ConstraintModelNumDiffTpl(const boost::shared_ptr<Base>& model);
50 
55 
59  virtual void calc(const boost::shared_ptr<ConstraintDataAbstract>& data,
60  const Eigen::Ref<const VectorXs>& x,
61  const Eigen::Ref<const VectorXs>& u);
62 
68  virtual void calc(const boost::shared_ptr<ConstraintDataAbstract>& data,
69  const Eigen::Ref<const VectorXs>& x);
70 
74  virtual void calcDiff(const boost::shared_ptr<ConstraintDataAbstract>& data,
75  const Eigen::Ref<const VectorXs>& x,
76  const Eigen::Ref<const VectorXs>& u);
77 
83  virtual void calcDiff(const boost::shared_ptr<ConstraintDataAbstract>& data,
84  const Eigen::Ref<const VectorXs>& x);
85 
89  virtual boost::shared_ptr<ConstraintDataAbstract> createData(
90  DataCollectorAbstract* const data);
91 
95  const boost::shared_ptr<Base>& get_model() const;
96 
101  const Scalar get_disturbance() const;
102 
107  void set_disturbance(const Scalar disturbance);
108 
116  void set_reevals(const std::vector<ReevaluationFunction>& reevals);
117 
118  protected:
119  using Base::nu_;
120  using Base::state_;
121  using Base::unone_;
122 
123  private:
132  void assertStableStateFD(const Eigen::Ref<const VectorXs>& /*x*/);
133 
134  boost::shared_ptr<Base> model_;
136  Scalar e_jac_;
138  std::vector<ReevaluationFunction>
139  reevals_;
140 };
141 
142 template <typename _Scalar>
144  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
145 
146  typedef _Scalar Scalar;
151  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
152 
153  template <template <typename Scalar> class Model>
154  explicit ConstraintDataNumDiffTpl(Model<Scalar>* const model,
155  DataCollectorAbstract* const shared_data)
156  : Base(model, shared_data),
157  dx(model->get_state()->get_ndx()),
158  xp(model->get_state()->get_nx()),
159  du(model->get_nu()),
160  up(model->get_nu()) {
161  dx.setZero();
162  xp.setZero();
163  du.setZero();
164  up.setZero();
165 
166  const std::size_t& ndx = model->get_model()->get_state()->get_ndx();
167  const std::size_t& nu = model->get_model()->get_nu();
168  data_0 = model->get_model()->createData(shared_data);
169  for (std::size_t i = 0; i < ndx; ++i) {
170  data_x.push_back(model->get_model()->createData(shared_data));
171  }
172  for (std::size_t i = 0; i < nu; ++i) {
173  data_u.push_back(model->get_model()->createData(shared_data));
174  }
175  }
176 
177  virtual ~ConstraintDataNumDiffTpl() {}
178 
179  using Base::Gu;
180  using Base::Gx;
181  using Base::Hu;
182  using Base::Hx;
183  using Base::shared;
184 
185  Scalar x_norm;
186  Scalar
188  Scalar
190  VectorXs dx;
191  VectorXs xp;
193  VectorXs du;
194  VectorXs up;
196  boost::shared_ptr<Base> data_0;
197  std::vector<boost::shared_ptr<Base> >
199  std::vector<boost::shared_ptr<Base> >
201 };
202 
203 } // namespace crocoddyl
204 
205 /* --- Details -------------------------------------------------------------- */
206 /* --- Details -------------------------------------------------------------- */
207 /* --- Details -------------------------------------------------------------- */
208 #include "crocoddyl/core/numdiff/constraint.hxx"
209 
210 #endif // CROCODDYL_CORE_NUMDIFF_CONSTRAINT_HPP_
Abstract class for constraint models.
boost::shared_ptr< StateAbstract > state_
State description.
std::size_t nu_
Control dimension.
This class computes the numerical differentiation of a constraint model.
Definition: constraint.hpp:30
virtual boost::shared_ptr< ConstraintDataAbstract > createData(DataCollectorAbstract *const data)
Create the constraint data.
virtual ~ConstraintModelNumDiffTpl()
Initialize the numdiff constraint model.
virtual void calcDiff(const boost::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
const Scalar get_disturbance() const
Return the disturbance constant used by the numerical differentiation routine.
void set_reevals(const std::vector< ReevaluationFunction > &reevals)
Register functions that updates the shared data computed for a system rollout The updated data is use...
void set_disturbance(const Scalar disturbance)
Modify the disturbance constant used by the numerical differentiation routine.
virtual void calc(const boost::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
virtual void calc(const boost::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the constraint value.
virtual void calcDiff(const boost::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the Jacobian of the constraint.
const boost::shared_ptr< Base > & get_model() const
Return the original constraint model.
ConstraintModelNumDiffTpl(const boost::shared_ptr< Base > &model)
Initialize the numdiff constraint model.
MatrixXs Gx
Jacobian of the inequality constraint.
MatrixXs Hx
Jacobian of the equality constraint.
MatrixXs Hu
Jacobian of the equality constraint.
DataCollectorAbstract * shared
Shared data.
MatrixXs Gu
Jacobian of the inequality constraint.
std::vector< boost::shared_ptr< Base > > data_u
The temporary data associated with the control variation.
Definition: constraint.hpp:200
Scalar x_norm
Norm of the state vector.
Definition: constraint.hpp:185
boost::shared_ptr< Base > data_0
The data at the approximation point.
Definition: constraint.hpp:196
Scalar uh_jac
Disturbance value used for computing .
Definition: constraint.hpp:189
Scalar xh_jac
Disturbance value used for computing .
Definition: constraint.hpp:187
VectorXs du
Control disturbance.
Definition: constraint.hpp:193
std::vector< boost::shared_ptr< Base > > data_x
The temporary data associated with the state variation.
Definition: constraint.hpp:198
VectorXs dx
State disturbance.
Definition: constraint.hpp:190