Crocoddyl
constraint-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2020-2024, 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_CONSTRAINT_BASE_HPP_
10 #define CROCODDYL_CORE_CONSTRAINT_BASE_HPP_
11 
12 #include <boost/make_shared.hpp>
13 #include <boost/shared_ptr.hpp>
14 
15 #include "crocoddyl/core/fwd.hpp"
16 //
17 #include "crocoddyl/core/data-collector-base.hpp"
18 #include "crocoddyl/core/residual-base.hpp"
19 #include "crocoddyl/core/state-base.hpp"
20 
21 namespace crocoddyl {
22 
23 enum ConstraintType { Inequality = 0, Equality, Both };
24 
49 template <typename _Scalar>
51  public:
52  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
53 
54  typedef _Scalar Scalar;
60  typedef typename MathBase::VectorXs VectorXs;
61 
70  ConstraintModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
71  boost::shared_ptr<ResidualModelAbstract> residual,
72  const std::size_t ng, const std::size_t nh);
73 
85  ConstraintModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
86  const std::size_t nu, const std::size_t ng,
87  const std::size_t nh, const bool T_const = true);
88 
101  ConstraintModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
102  const std::size_t ng, const std::size_t nh,
103  const bool T_const = true);
104  virtual ~ConstraintModelAbstractTpl();
105 
113  virtual void calc(const boost::shared_ptr<ConstraintDataAbstract>& data,
114  const Eigen::Ref<const VectorXs>& x,
115  const Eigen::Ref<const VectorXs>& u) = 0;
116 
127  virtual void calc(const boost::shared_ptr<ConstraintDataAbstract>& data,
128  const Eigen::Ref<const VectorXs>& x);
129 
140  virtual void calcDiff(const boost::shared_ptr<ConstraintDataAbstract>& data,
141  const Eigen::Ref<const VectorXs>& x,
142  const Eigen::Ref<const VectorXs>& u) = 0;
143 
155  virtual void calcDiff(const boost::shared_ptr<ConstraintDataAbstract>& data,
156  const Eigen::Ref<const VectorXs>& x);
157 
169  virtual boost::shared_ptr<ConstraintDataAbstract> createData(
170  DataCollectorAbstract* const data);
171 
175  void update_bounds(const VectorXs& lower, const VectorXs& upper);
176 
181 
185  const boost::shared_ptr<StateAbstract>& get_state() const;
186 
190  const boost::shared_ptr<ResidualModelAbstract>& get_residual() const;
191 
195  ConstraintType get_type() const;
196 
200  const VectorXs& get_lb() const;
201 
205  const VectorXs& get_ub() const;
206 
210  std::size_t get_nu() const;
211 
215  std::size_t get_ng() const;
216 
220  std::size_t get_nh() const;
221 
225  bool get_T_constraint() const;
226 
230  template <class Scalar>
231  friend std::ostream& operator<<(std::ostream& os,
232  const CostModelAbstractTpl<Scalar>& model);
233 
239  virtual void print(std::ostream& os) const;
240 
241  private:
242  std::size_t ng_internal_;
244  std::size_t nh_internal_;
246 
247  protected:
248  boost::shared_ptr<StateAbstract> state_;
249  boost::shared_ptr<ResidualModelAbstract> residual_;
250  ConstraintType
252  VectorXs lb_;
253  VectorXs ub_;
254  std::size_t nu_;
255  std::size_t ng_;
256  std::size_t nh_;
259  VectorXs unone_;
260 };
261 
262 template <typename _Scalar>
264  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
265 
266  typedef _Scalar Scalar;
270  typedef typename MathBase::VectorXs VectorXs;
271  typedef typename MathBase::MatrixXs MatrixXs;
272 
273  template <template <typename Scalar> class Model>
274  ConstraintDataAbstractTpl(Model<Scalar>* const model,
275  DataCollectorAbstract* const data)
276  : shared(data),
277  residual(model->get_residual()->createData(data)),
278  g(model->get_ng()),
279  Gx(model->get_ng(), model->get_state()->get_ndx()),
280  Gu(model->get_ng(), model->get_nu()),
281  h(model->get_nh()),
282  Hx(model->get_nh(), model->get_state()->get_ndx()),
283  Hu(model->get_nh(), model->get_nu()) {
284  if (model->get_ng() == 0 && model->get_nh() == 0) {
285  throw_pretty("Invalid argument: " << "ng and nh cannot be equals to 0");
286  }
287  g.setZero();
288  Gx.setZero();
289  Gu.setZero();
290  h.setZero();
291  Hx.setZero();
292  Hu.setZero();
293  }
294  virtual ~ConstraintDataAbstractTpl() {}
295 
297  boost::shared_ptr<ResidualDataAbstract> residual;
298  VectorXs g;
299  MatrixXs Gx;
300  MatrixXs Gu;
301  VectorXs h;
302  MatrixXs Hx;
303  MatrixXs Hu;
304 };
305 
306 } // namespace crocoddyl
307 
308 /* --- Details -------------------------------------------------------------- */
309 /* --- Details -------------------------------------------------------------- */
310 /* --- Details -------------------------------------------------------------- */
311 #include "crocoddyl/core/constraint-base.hxx"
312 
313 #endif // CROCODDYL_CORE_CONSTRAINT_BASE_HPP_
Abstract class for constraint models.
void remove_bounds()
Remove the bounds of the constraint.
virtual boost::shared_ptr< ConstraintDataAbstract > createData(DataCollectorAbstract *const data)
Create the constraint data.
std::size_t get_ng() const
Return the number of inequality constraints.
const boost::shared_ptr< StateAbstract > & get_state() const
Return the state.
virtual void calcDiff(const boost::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the Jacobian of the constraint with respect to the state only.
virtual void print(std::ostream &os) const
Print relevant information of the constraint model.
friend std::ostream & operator<<(std::ostream &os, const CostModelAbstractTpl< Scalar > &model)
Print information on the constraint model.
const VectorXs & get_lb() const
Return the lower bound of the constraint.
virtual void calc(const boost::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the constraint value for nodes that depends only on the state.
ConstraintModelAbstractTpl(boost::shared_ptr< StateAbstract > state, const std::size_t ng, const std::size_t nh, const bool T_const=true)
Initialize the constraint model.
const VectorXs & get_ub() const
Return the upper bound of the constraint.
std::size_t get_nh() const
Return the number of equality constraints.
ConstraintType get_type() const
Return the type of constraint.
bool get_T_constraint() const
Return true if the constraint is imposed in terminal nodes as well.
virtual void calcDiff(const boost::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the Jacobian of the constraint.
boost::shared_ptr< StateAbstract > state_
State description.
std::size_t nu_
Control dimension.
std::size_t nh_
Number of equality constraints.
ConstraintModelAbstractTpl(boost::shared_ptr< StateAbstract > state, const std::size_t nu, const std::size_t ng, const std::size_t nh, const bool T_const=true)
the constraint model
boost::shared_ptr< ResidualModelAbstract > residual_
Residual model.
VectorXs lb_
Lower bound of the constraint.
ConstraintType type_
Type of constraint: inequality=0, equality=1, both=2.
void update_bounds(const VectorXs &lower, const VectorXs &upper)
Update the lower and upper bounds the upper bound of constraint.
VectorXs ub_
Upper bound of the constraint.
std::size_t ng_
Number of inequality constraints.
std::size_t get_nu() const
Return the dimension of the control input.
ConstraintModelAbstractTpl(boost::shared_ptr< StateAbstract > state, boost::shared_ptr< ResidualModelAbstract > residual, const std::size_t ng, const std::size_t nh)
Initialize the constraint model.
virtual void calc(const boost::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the constraint value.
const boost::shared_ptr< ResidualModelAbstract > & get_residual() const
Return the residual model.
Abstract class for cost models.
Definition: cost-base.hpp:59
Abstract class for residual models.
Abstract class for the state representation.
Definition: state-base.hpp:46
MatrixXs Gx
Jacobian of the inequality constraint.
VectorXs h
Equality constraint values.
boost::shared_ptr< ResidualDataAbstract > residual
Residual data.
VectorXs g
Inequality constraint values.
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.