Crocoddyl
 
Loading...
Searching...
No Matches
constraint-base.hpp
1
2// BSD 3-Clause License
3//
4// Copyright (C) 2020-2025, 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 "crocoddyl/core/fwd.hpp"
13//
14#include "crocoddyl/core/data-collector-base.hpp"
15#include "crocoddyl/core/residual-base.hpp"
16#include "crocoddyl/core/state-base.hpp"
17
18namespace crocoddyl {
19
20enum ConstraintType { Inequality = 0, Equality, Both };
21
23 public:
24 virtual ~ConstraintModelBase() = default;
25
27};
28
53template <typename _Scalar>
55 public:
56 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
57
58 typedef _Scalar Scalar;
64 typedef typename MathBase::VectorXs VectorXs;
65
74 ConstraintModelAbstractTpl(std::shared_ptr<StateAbstract> state,
75 std::shared_ptr<ResidualModelAbstract> residual,
76 const std::size_t ng, const std::size_t nh);
77
89 ConstraintModelAbstractTpl(std::shared_ptr<StateAbstract> state,
90 const std::size_t nu, const std::size_t ng,
91 const std::size_t nh, const bool T_const = true);
92
105 ConstraintModelAbstractTpl(std::shared_ptr<StateAbstract> state,
106 const std::size_t ng, const std::size_t nh,
107 const bool T_const = true);
108 virtual ~ConstraintModelAbstractTpl() = default;
109
117 virtual void calc(const std::shared_ptr<ConstraintDataAbstract>& data,
118 const Eigen::Ref<const VectorXs>& x,
119 const Eigen::Ref<const VectorXs>& u) = 0;
120
131 virtual void calc(const std::shared_ptr<ConstraintDataAbstract>& data,
132 const Eigen::Ref<const VectorXs>& x);
133
144 virtual void calcDiff(const std::shared_ptr<ConstraintDataAbstract>& data,
145 const Eigen::Ref<const VectorXs>& x,
146 const Eigen::Ref<const VectorXs>& u) = 0;
147
159 virtual void calcDiff(const std::shared_ptr<ConstraintDataAbstract>& data,
160 const Eigen::Ref<const VectorXs>& x);
161
173 virtual std::shared_ptr<ConstraintDataAbstract> createData(
174 DataCollectorAbstract* const data);
175
179 void update_bounds(const VectorXs& lower, const VectorXs& upper);
180
185
189 const std::shared_ptr<StateAbstract>& get_state() const;
190
194 const std::shared_ptr<ResidualModelAbstract>& get_residual() const;
195
199 ConstraintType get_type() const;
200
204 const VectorXs& get_lb() const;
205
209 const VectorXs& get_ub() const;
210
214 std::size_t get_nu() const;
215
219 std::size_t get_ng() const;
220
224 std::size_t get_nh() const;
225
229 bool get_T_constraint() const;
230
234 template <class Scalar>
235 friend std::ostream& operator<<(std::ostream& os,
236 const CostModelAbstractTpl<Scalar>& model);
237
243 virtual void print(std::ostream& os) const;
244
245 private:
246 std::size_t ng_internal_;
248 std::size_t nh_internal_;
250
251 protected:
252 std::shared_ptr<StateAbstract> state_;
253 std::shared_ptr<ResidualModelAbstract> residual_;
254 ConstraintType
256 VectorXs lb_;
257 VectorXs ub_;
258 std::size_t nu_;
259 std::size_t ng_;
260 std::size_t nh_;
263 VectorXs unone_;
265 : state_(nullptr), residual_(nullptr), nu_(0), ng_(0), nh_(0) {}
266};
267
268template <typename _Scalar>
270 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
271
272 typedef _Scalar Scalar;
276 typedef typename MathBase::VectorXs VectorXs;
277 typedef typename MathBase::MatrixXs MatrixXs;
278
279 template <template <typename Scalar> class Model>
280 ConstraintDataAbstractTpl(Model<Scalar>* const model,
281 DataCollectorAbstract* const data)
282 : shared(data),
283 residual(model->get_residual()->createData(data)),
284 g(model->get_ng()),
285 Gx(model->get_ng(), model->get_state()->get_ndx()),
286 Gu(model->get_ng(), model->get_nu()),
287 h(model->get_nh()),
288 Hx(model->get_nh(), model->get_state()->get_ndx()),
289 Hu(model->get_nh(), model->get_nu()) {
290 if (model->get_ng() == 0 && model->get_nh() == 0) {
291 throw_pretty("Invalid argument: " << "ng and nh cannot be equals to 0");
292 }
293 g.setZero();
294 Gx.setZero();
295 Gu.setZero();
296 h.setZero();
297 Hx.setZero();
298 Hu.setZero();
299 }
300 virtual ~ConstraintDataAbstractTpl() = default;
301
303 std::shared_ptr<ResidualDataAbstract> residual;
304 VectorXs g;
305 MatrixXs Gx;
306 MatrixXs Gu;
307 VectorXs h;
308 MatrixXs Hx;
309 MatrixXs Hu;
310};
311
312} // namespace crocoddyl
313
314/* --- Details -------------------------------------------------------------- */
315/* --- Details -------------------------------------------------------------- */
316/* --- Details -------------------------------------------------------------- */
317#include "crocoddyl/core/constraint-base.hxx"
318
319CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::ConstraintModelAbstractTpl)
320CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(crocoddyl::ConstraintDataAbstractTpl)
321
322#endif // CROCODDYL_CORE_CONSTRAINT_BASE_HPP_
Abstract class for constraint models.
void remove_bounds()
Remove the bounds of the constraint.
const VectorXs & get_ub() const
Return the upper bound of the constraint.
virtual void calcDiff(const std::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the Jacobian of the constraint.
std::shared_ptr< StateAbstract > state_
State description.
std::size_t get_ng() const
Return the number of inequality constraints.
ConstraintModelAbstractTpl(std::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
virtual void calc(const std::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the constraint value for nodes that depends only on the state.
virtual void print(std::ostream &os) const
Print relevant information of the constraint model.
virtual void calcDiff(const std::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the Jacobian of the constraint with respect to the state only.
ConstraintModelAbstractTpl(std::shared_ptr< StateAbstract > state, std::shared_ptr< ResidualModelAbstract > residual, const std::size_t ng, const std::size_t nh)
Initialize the constraint model.
const VectorXs & get_lb() const
Return the lower bound of the constraint.
std::size_t get_nh() const
Return the number of equality constraints.
friend std::ostream & operator<<(std::ostream &os, const CostModelAbstractTpl< Scalar > &model)
Print information on the constraint model.
const std::shared_ptr< ResidualModelAbstract > & get_residual() const
Return the residual model.
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.
std::size_t nu_
Control dimension.
ConstraintModelAbstractTpl(std::shared_ptr< StateAbstract > state, const std::size_t ng, const std::size_t nh, const bool T_const=true)
const std::shared_ptr< StateAbstract > & get_state() const
Return the state.
std::size_t nh_
Number of equality constraints.
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.
std::shared_ptr< ResidualModelAbstract > residual_
Residual model.
virtual std::shared_ptr< ConstraintDataAbstract > createData(DataCollectorAbstract *const data)
Create the constraint data.
virtual void calc(const std::shared_ptr< ConstraintDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the constraint value.
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.
Abstract class for cost models.
Definition cost-base.hpp:64
Abstract class for residual models.
Abstract class for the state representation.
MatrixXs Gx
Jacobian of the inequality constraint.
VectorXs h
Equality constraint values.
VectorXs g
Inequality constraint values.
MatrixXs Hx
Jacobian of the equality constraint.
std::shared_ptr< ResidualDataAbstract > residual
Residual data.
MatrixXs Hu
Jacobian of the equality constraint.
DataCollectorAbstract * shared
Shared data.
MatrixXs Gu
Jacobian of the inequality constraint.