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;
263 template <
class Scalar>
268 boost::shared_ptr<StateAbstract> state_;
269 ConstraintModelContainer constraints_;
277 std::set<std::string> active_set_;
278 std::set<std::string>
282 template <
typename _Scalar>
284 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
286 typedef _Scalar Scalar;
290 typedef typename MathBase::VectorXs VectorXs;
291 typedef typename MathBase::MatrixXs MatrixXs;
293 template <
template <
typename Scalar>
class Model>
296 : g_internal(model->get_ng()),
297 Gx_internal(model->get_ng(), model->get_state()->get_ndx()),
298 Gu_internal(model->get_ng(), model->get_nu()),
299 h_internal(model->get_nh()),
300 Hx_internal(model->get_nh(), model->get_state()->get_ndx()),
301 Hu_internal(model->get_nh(), model->get_nu()),
303 g(g_internal.data(), model->get_ng()),
304 Gx(Gx_internal.data(), model->get_ng(), model->get_state()->get_ndx()),
305 Gu(Gu_internal.data(), model->get_ng(), model->get_nu()),
306 h(h_internal.data(), model->get_nh()),
307 Hx(Hx_internal.data(), model->get_nh(), model->get_state()->get_ndx()),
308 Hu(Hu_internal.data(), model->get_nh(), model->get_nu()) {
316 Scalar>::ConstraintModelContainer::const_iterator it =
318 it != model->get_constraints().end(); ++it) {
319 const boost::shared_ptr<ConstraintItem>& item = it->second;
321 std::make_pair(item->name, item->constraint->createData(data)));
325 template <
class ActionData>
326 void shareMemory(ActionData*
const data) {
328 g_internal.resize(0);
329 Gx_internal.resize(0, 0);
330 Gu_internal.resize(0, 0);
331 h_internal.resize(0);
332 Hx_internal.resize(0, 0);
333 Hu_internal.resize(0, 0);
335 new (&g) Eigen::Map<VectorXs>(data->g.data(), data->g.size());
337 Eigen::Map<MatrixXs>(data->Gx.data(), data->Gx.rows(), data->Gx.cols());
339 Eigen::Map<MatrixXs>(data->Gu.data(), data->Gu.rows(), data->Gu.cols());
340 new (&h) Eigen::Map<VectorXs>(data->h.data(), data->h.size());
342 Eigen::Map<MatrixXs>(data->Hx.data(), data->Hx.rows(), data->Hx.cols());
344 Eigen::Map<MatrixXs>(data->Hu.data(), data->Hu.rows(), data->Hu.cols());
347 template <
class Model>
348 void resize(Model*
const model,
const bool running_node =
true) {
349 const std::size_t ndx = model->get_state()->get_ndx();
350 const std::size_t nu = model->get_nu();
351 const std::size_t ng = running_node ? model->get_ng() : model->get_ng_T();
352 const std::size_t nh = running_node ? model->get_nh() : model->get_nh_T();
353 new (&g) Eigen::Map<VectorXs>(g_internal.data(), ng);
354 new (&Gx) Eigen::Map<MatrixXs>(Gx_internal.data(), ng, ndx);
355 new (&Gu) Eigen::Map<MatrixXs>(Gu_internal.data(), ng, nu);
356 new (&h) Eigen::Map<VectorXs>(h_internal.data(), nh);
357 new (&Hx) Eigen::Map<MatrixXs>(Hx_internal.data(), nh, ndx);
358 new (&Hu) Eigen::Map<MatrixXs>(Hu_internal.data(), nh, nu);
361 template <
class ActionModel,
class ActionData>
362 void resize(ActionModel*
const model, ActionData*
const data,
363 const bool running_node =
true) {
364 const std::size_t ndx = model->get_state()->get_ndx();
365 const std::size_t nu = model->get_nu();
366 const std::size_t ng = running_node ? model->get_ng() : model->get_ng_T();
367 const std::size_t nh = running_node ? model->get_nh() : model->get_nh_T();
368 data->g.conservativeResize(ng);
369 data->Gx.conservativeResize(ng, ndx);
370 data->Gu.conservativeResize(ng, nu);
371 data->h.conservativeResize(nh);
372 data->Hx.conservativeResize(nh, ndx);
373 data->Hu.conservativeResize(nh, nu);
374 new (&g) Eigen::Map<VectorXs>(data->g.data(), ng);
375 new (&Gx) Eigen::Map<MatrixXs>(data->Gx.data(), ng, ndx);
376 new (&Gu) Eigen::Map<MatrixXs>(data->Gu.data(), ng, nu);
377 new (&h) Eigen::Map<VectorXs>(data->h.data(), nh);
378 new (&Hx) Eigen::Map<MatrixXs>(data->Hx.data(), nh, ndx);
379 new (&Hu) Eigen::Map<MatrixXs>(data->Hu.data(), nh, nu);
382 VectorXs get_g()
const {
return g; }
383 MatrixXs get_Gx()
const {
return Gx; }
384 MatrixXs get_Gu()
const {
return Gu; }
385 VectorXs get_h()
const {
return h; }
386 MatrixXs get_Hx()
const {
return Hx; }
387 MatrixXs get_Hu()
const {
return Hu; }
389 void set_g(
const VectorXs& _g) {
390 if (g.size() != _g.size()) {
392 "Invalid argument: " <<
"g has wrong dimension (it should be " +
393 std::to_string(g.size()) +
")");
397 void set_Gx(
const MatrixXs& _Gx) {
398 if (Gx.rows() != _Gx.rows() || Gx.cols() != _Gx.cols()) {
400 "Invalid argument: " <<
"Gx has wrong dimension (it should be " +
401 std::to_string(Gx.rows()) +
", " +
402 std::to_string(Gx.cols()) +
")");
406 void set_Gu(
const MatrixXs& _Gu) {
407 if (Gu.rows() != _Gu.rows() || Gu.cols() != _Gu.cols()) {
409 "Invalid argument: " <<
"Gu has wrong dimension (it should be " +
410 std::to_string(Gu.rows()) +
", " +
411 std::to_string(Gu.cols()) +
")");
415 void set_h(
const VectorXs& _h) {
416 if (h.size() != _h.size()) {
418 "Invalid argument: " <<
"h has wrong dimension (it should be " +
419 std::to_string(h.size()) +
")");
423 void set_Hx(
const MatrixXs& _Hx) {
424 if (Hx.rows() != _Hx.rows() || Hx.cols() != _Hx.cols()) {
426 "Invalid argument: " <<
"Hx has wrong dimension (it should be " +
427 std::to_string(Hx.rows()) +
", " +
428 std::to_string(Hx.cols()) +
")");
432 void set_Hu(
const MatrixXs& _Hu) {
433 if (Hu.rows() != _Hu.rows() || Hu.cols() != _Hu.cols()) {
435 "Invalid argument: " <<
"Hu has wrong dimension (it should be " +
436 std::to_string(Hu.rows()) +
", " +
437 std::to_string(Hu.cols()) +
")");
444 MatrixXs Gx_internal;
445 MatrixXs Gu_internal;
447 MatrixXs Hx_internal;
448 MatrixXs Hu_internal;
453 Eigen::Map<VectorXs> g;
454 Eigen::Map<MatrixXs> Gx;
455 Eigen::Map<MatrixXs> Gu;
456 Eigen::Map<VectorXs> h;
457 Eigen::Map<MatrixXs> Hx;
458 Eigen::Map<MatrixXs> Hu;
466 #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.
std::size_t get_nh_T() const
Return the number of active equality terminal constraints.
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_T() const
Return the number of active inequality terminal constraints.
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.