9 #ifndef CROCODDYL_CORE_CONSTRAINTS_CONSTRAINT_MANAGER_HPP_
10 #define CROCODDYL_CORE_CONSTRAINTS_CONSTRAINT_MANAGER_HPP_
17 #include "crocoddyl/core/constraint-base.hpp"
18 #include "crocoddyl/core/utils/exception.hpp"
22 template <
typename _Scalar>
24 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26 typedef _Scalar Scalar;
31 boost::shared_ptr<ConstraintModelAbstract> constraint,
33 : name(name), constraint(constraint), active(active) {}
40 os <<
"{" << *model.constraint <<
"}";
45 boost::shared_ptr<ConstraintModelAbstract> constraint;
67 template <
typename _Scalar>
70 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
72 typedef _Scalar Scalar;
80 typedef typename MathBase::VectorXs VectorXs;
81 typedef typename MathBase::MatrixXs MatrixXs;
83 typedef std::map<std::string, boost::shared_ptr<ConstraintItem> >
84 ConstraintModelContainer;
85 typedef std::map<std::string, boost::shared_ptr<ConstraintDataAbstract> >
86 ConstraintDataContainer;
95 const std::size_t nu);
116 boost::shared_ptr<ConstraintModelAbstract> constraint,
117 const bool active =
true);
142 void calc(
const boost::shared_ptr<ConstraintDataManager>& data,
143 const Eigen::Ref<const VectorXs>& x,
144 const Eigen::Ref<const VectorXs>& u);
156 void calc(
const boost::shared_ptr<ConstraintDataManager>& data,
157 const Eigen::Ref<const VectorXs>& x);
166 void calcDiff(
const boost::shared_ptr<ConstraintDataManager>& data,
167 const Eigen::Ref<const VectorXs>& x,
168 const Eigen::Ref<const VectorXs>& u);
181 void calcDiff(
const boost::shared_ptr<ConstraintDataManager>& data,
182 const Eigen::Ref<const VectorXs>& x);
201 const boost::shared_ptr<StateAbstract>&
get_state()
const;
253 template <
class Scalar>
258 boost::shared_ptr<StateAbstract> state_;
259 ConstraintModelContainer constraints_;
265 std::set<std::string> active_set_;
266 std::set<std::string>
270 template <
typename _Scalar>
272 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
274 typedef _Scalar Scalar;
278 typedef typename MathBase::VectorXs VectorXs;
279 typedef typename MathBase::MatrixXs MatrixXs;
281 template <
template <
typename Scalar>
class Model>
284 : g_internal(model->get_ng()),
285 Gx_internal(model->get_ng(), model->get_state()->get_ndx()),
286 Gu_internal(model->get_ng(), model->get_nu()),
287 h_internal(model->get_nh()),
288 Hx_internal(model->get_nh(), model->get_state()->get_ndx()),
289 Hu_internal(model->get_nh(), model->get_nu()),
291 g(g_internal.data(), model->get_ng()),
292 Gx(Gx_internal.data(), model->get_ng(), model->get_state()->get_ndx()),
293 Gu(Gu_internal.data(), model->get_ng(), model->get_nu()),
294 h(h_internal.data(), model->get_nh()),
295 Hx(Hx_internal.data(), model->get_nh(), model->get_state()->get_ndx()),
296 Hu(Hu_internal.data(), model->get_nh(), model->get_nu()) {
304 Scalar>::ConstraintModelContainer::const_iterator it =
306 it != model->get_constraints().end(); ++it) {
307 const boost::shared_ptr<ConstraintItem>& item = it->second;
309 std::make_pair(item->name, item->constraint->createData(data)));
313 template <
class ActionData>
314 void shareMemory(ActionData*
const data) {
316 g_internal.resize(0);
317 Gx_internal.resize(0, 0);
318 Gu_internal.resize(0, 0);
319 h_internal.resize(0);
320 Hx_internal.resize(0, 0);
321 Hu_internal.resize(0, 0);
323 new (&g) Eigen::Map<VectorXs>(data->g.data(), data->g.size());
325 Eigen::Map<MatrixXs>(data->Gx.data(), data->Gx.rows(), data->Gx.cols());
327 Eigen::Map<MatrixXs>(data->Gu.data(), data->Gu.rows(), data->Gu.cols());
328 new (&h) Eigen::Map<VectorXs>(data->h.data(), data->h.size());
330 Eigen::Map<MatrixXs>(data->Hx.data(), data->Hx.rows(), data->Hx.cols());
332 Eigen::Map<MatrixXs>(data->Hu.data(), data->Hu.rows(), data->Hu.cols());
335 template <
class ActionModel,
class ActionData>
336 void resize(ActionModel*
const model, ActionData*
const data) {
337 const std::size_t ndx = model->get_state()->get_ndx();
338 const std::size_t nu = model->get_nu();
339 const std::size_t ng = model->get_ng();
340 const std::size_t nh = model->get_nh();
341 data->g.conservativeResize(ng);
342 data->Gx.conservativeResize(ng, ndx);
343 data->Gu.conservativeResize(ng, nu);
344 data->h.conservativeResize(nh);
345 data->Hx.conservativeResize(nh, ndx);
346 data->Hu.conservativeResize(nh, nu);
347 new (&g) Eigen::Map<VectorXs>(data->g.data(), ng);
348 new (&Gx) Eigen::Map<MatrixXs>(data->Gx.data(), ng, ndx);
349 new (&Gu) Eigen::Map<MatrixXs>(data->Gu.data(), ng, nu);
350 new (&h) Eigen::Map<VectorXs>(data->h.data(), nh);
351 new (&Hx) Eigen::Map<MatrixXs>(data->Hx.data(), nh, ndx);
352 new (&Hu) Eigen::Map<MatrixXs>(data->Hu.data(), nh, nu);
355 VectorXs get_g()
const {
return g; }
356 MatrixXs get_Gx()
const {
return Gx; }
357 MatrixXs get_Gu()
const {
return Gu; }
358 VectorXs get_h()
const {
return h; }
359 MatrixXs get_Hx()
const {
return Hx; }
360 MatrixXs get_Hu()
const {
return Hu; }
362 void set_g(
const VectorXs& _g) {
363 if (g.size() != _g.size()) {
364 throw_pretty(
"Invalid argument: "
365 <<
"g has wrong dimension (it should be " +
366 std::to_string(g.size()) +
")");
370 void set_Gx(
const MatrixXs& _Gx) {
371 if (Gx.rows() != _Gx.rows() || Gx.cols() != _Gx.cols()) {
372 throw_pretty(
"Invalid argument: "
373 <<
"Gx has wrong dimension (it should be " +
374 std::to_string(Gx.rows()) +
", " +
375 std::to_string(Gx.cols()) +
")");
379 void set_Gu(
const MatrixXs& _Gu) {
380 if (Gu.rows() != _Gu.rows() || Gu.cols() != _Gu.cols()) {
381 throw_pretty(
"Invalid argument: "
382 <<
"Gu has wrong dimension (it should be " +
383 std::to_string(Gu.rows()) +
", " +
384 std::to_string(Gu.cols()) +
")");
388 void set_h(
const VectorXs& _h) {
389 if (h.size() != _h.size()) {
390 throw_pretty(
"Invalid argument: "
391 <<
"h has wrong dimension (it should be " +
392 std::to_string(h.size()) +
")");
396 void set_Hx(
const MatrixXs& _Hx) {
397 if (Hx.rows() != _Hx.rows() || Hx.cols() != _Hx.cols()) {
398 throw_pretty(
"Invalid argument: "
399 <<
"Hx has wrong dimension (it should be " +
400 std::to_string(Hx.rows()) +
", " +
401 std::to_string(Hx.cols()) +
")");
405 void set_Hu(
const MatrixXs& _Hu) {
406 if (Hu.rows() != _Hu.rows() || Hu.cols() != _Hu.cols()) {
407 throw_pretty(
"Invalid argument: "
408 <<
"Hu has wrong dimension (it should be " +
409 std::to_string(Hu.rows()) +
", " +
410 std::to_string(Hu.cols()) +
")");
417 MatrixXs Gx_internal;
418 MatrixXs Gu_internal;
420 MatrixXs Hx_internal;
421 MatrixXs Hu_internal;
426 Eigen::Map<VectorXs> g;
427 Eigen::Map<MatrixXs> Gx;
428 Eigen::Map<MatrixXs> Gu;
429 Eigen::Map<VectorXs> h;
430 Eigen::Map<MatrixXs> Hx;
431 Eigen::Map<MatrixXs> Hu;
439 #include "crocoddyl/core/constraints/constraint-manager.hxx"
Abstract class for constraint models.
Manage the individual constraint models.
friend std::ostream & operator<<(std::ostream &os, const ConstraintModelManagerTpl< Scalar > &model)
Print information on the stack of constraints.
void removeConstraint(const std::string &name)
Remove a constraint item.
void changeConstraintStatus(const std::string &name, bool active)
Change the constraint status.
void calc(const boost::shared_ptr< ConstraintDataManager > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the total constraint value.
std::size_t get_ng() const
Return the number of active inequality constraints.
ConstraintModelManagerTpl(boost::shared_ptr< StateAbstract > state, const std::size_t nu)
Initialize the constraint-manager model.
const boost::shared_ptr< StateAbstract > & get_state() const
Return the state.
void calc(const boost::shared_ptr< ConstraintDataManager > &data, const Eigen::Ref< const VectorXs > &x)
Compute the total constraint value for nodes that depends only on the state.
const VectorXs & get_lb() const
Return the state lower bound.
void calcDiff(const boost::shared_ptr< ConstraintDataManager > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the Jacobian of the total constraint.
const VectorXs & get_ub() const
Return the state upper bound.
boost::shared_ptr< ConstraintDataManager > createData(DataCollectorAbstract *const data)
Create the constraint data.
std::size_t get_nh() const
Return the number of active equality constraints.
const std::set< std::string > & get_inactive_set() const
Return the names of the set of inactive constraints.
void addConstraint(const std::string &name, boost::shared_ptr< ConstraintModelAbstract > constraint, const bool active=true)
Add a constraint item.
const std::set< std::string > & get_active_set() const
Return the names of the set of active constraints.
const ConstraintModelContainer & get_constraints() const
Return the stack of constraint models.
ConstraintModelManagerTpl(boost::shared_ptr< StateAbstract > state)
Initialize the constraint-manager model.
bool getConstraintStatus(const std::string &name) const
Return the status of a given constraint name.
std::size_t get_nu() const
Return the dimension of the control input.
void calcDiff(const boost::shared_ptr< ConstraintDataManager > &data, const Eigen::Ref< const VectorXs > &x)
Compute the Jacobian of the total constraint with respect to the state only.
Abstract class for the state representation.
friend std::ostream & operator<<(std::ostream &os, const ConstraintItemTpl< Scalar > &model)
Print information on the constraint item.