10 #ifndef CROCODDYL_CORE_COSTS_COST_SUM_HPP_
11 #define CROCODDYL_CORE_COSTS_COST_SUM_HPP_
13 #include "crocoddyl/core/cost-base.hpp"
14 #include "crocoddyl/core/fwd.hpp"
18 template <
typename _Scalar>
20 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
22 typedef _Scalar Scalar;
26 CostItemTpl(
const std::string& name, std::shared_ptr<CostModelAbstract> cost,
27 const Scalar weight,
const bool active =
true)
28 : name(name), cost(cost), weight(weight), active(active) {}
30 template <
typename NewScalar>
33 ReturnType ret(name, cost->template cast<NewScalar>(),
34 scalar_cast<NewScalar>(weight), active);
43 os <<
"{w=" << model.weight <<
", " << *model.cost <<
"}";
48 std::shared_ptr<CostModelAbstract> cost;
74 template <
typename _Scalar>
77 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
79 typedef _Scalar Scalar;
87 typedef typename MathBase::VectorXs VectorXs;
88 typedef typename MathBase::MatrixXs MatrixXs;
90 typedef std::map<std::string, std::shared_ptr<CostItem> > CostModelContainer;
91 typedef std::map<std::string, std::shared_ptr<CostDataAbstract> >
120 void addCost(
const std::string& name, std::shared_ptr<CostModelAbstract> cost,
121 const Scalar weight,
const bool active =
true);
145 void calc(
const std::shared_ptr<CostDataSum>& data,
146 const Eigen::Ref<const VectorXs>& x,
147 const Eigen::Ref<const VectorXs>& u);
159 void calc(
const std::shared_ptr<CostDataSum>& data,
160 const Eigen::Ref<const VectorXs>& x);
169 void calcDiff(
const std::shared_ptr<CostDataSum>& data,
170 const Eigen::Ref<const VectorXs>& x,
171 const Eigen::Ref<const VectorXs>& u);
185 void calcDiff(
const std::shared_ptr<CostDataSum>& data,
186 const Eigen::Ref<const VectorXs>& x);
210 template <
typename NewScalar>
216 const std::shared_ptr<StateAbstract>&
get_state()
const;
249 "get_active() is deprecated and will be replaced with get_active_set()",
250 const std::vector<std::string>& get_active() {
252 active_.reserve(active_set_.size());
253 for (
const auto& contact : active_set_) {
254 active_.push_back(contact);
260 "get_inactive() is deprecated and will be replaced with "
261 "get_inactive_set()",
262 const std::vector<std::string>& get_inactive() {
264 inactive_.reserve(inactive_set_.size());
265 for (
const auto& contact : inactive_set_) {
266 inactive_.push_back(contact);
281 template <
class Scalar>
286 std::shared_ptr<StateAbstract> state_;
287 CostModelContainer costs_;
290 std::size_t nr_total_;
291 std::set<std::string> active_set_;
292 std::set<std::string>
298 std::vector<std::string> active_;
299 std::vector<std::string> inactive_;
302 template <
typename _Scalar>
304 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
306 typedef _Scalar Scalar;
310 typedef typename MathBase::VectorXs VectorXs;
311 typedef typename MathBase::MatrixXs MatrixXs;
313 template <
template <
typename Scalar>
class Model>
315 : Lx_internal(model->get_state()->get_ndx()),
316 Lu_internal(model->get_nu()),
317 Lxx_internal(model->get_state()->get_ndx(),
318 model->get_state()->get_ndx()),
319 Lxu_internal(model->get_state()->get_ndx(), model->get_nu()),
320 Luu_internal(model->get_nu(), model->get_nu()),
323 Lx(Lx_internal.data(), model->get_state()->get_ndx()),
324 Lu(Lu_internal.data(), model->get_nu()),
325 Lxx(Lxx_internal.data(), model->get_state()->get_ndx(),
326 model->get_state()->get_ndx()),
327 Lxu(Lxu_internal.data(), model->get_state()->get_ndx(),
329 Luu(Luu_internal.data(), model->get_nu(), model->get_nu()) {
337 it != model->get_costs().end(); ++it) {
338 const std::shared_ptr<CostItem>& item = it->second;
339 costs.insert(std::make_pair(item->name, item->cost->createData(data)));
343 template <
class ActionData>
344 void shareMemory(ActionData*
const data) {
346 Lx_internal.resize(0);
347 Lu_internal.resize(0);
348 Lxx_internal.resize(0, 0);
349 Lxu_internal.resize(0, 0);
350 Luu_internal.resize(0, 0);
352 new (&Lx) Eigen::Map<VectorXs>(data->Lx.data(), data->Lx.size());
353 new (&Lu) Eigen::Map<VectorXs>(data->Lu.data(), data->Lu.size());
354 new (&Lxx) Eigen::Map<MatrixXs>(data->Lxx.data(), data->Lxx.rows(),
356 new (&Lxu) Eigen::Map<MatrixXs>(data->Lxu.data(), data->Lxu.rows(),
358 new (&Luu) Eigen::Map<MatrixXs>(data->Luu.data(), data->Luu.rows(),
362 VectorXs get_Lx()
const {
return Lx; }
363 VectorXs get_Lu()
const {
return Lu; }
364 MatrixXs get_Lxx()
const {
return Lxx; }
365 MatrixXs get_Lxu()
const {
return Lxu; }
366 MatrixXs get_Luu()
const {
return Luu; }
368 void set_Lx(
const VectorXs& _Lx) {
369 if (Lx.size() != _Lx.size()) {
371 "Invalid argument: " <<
"Lx has wrong dimension (it should be " +
372 std::to_string(Lx.size()) +
")");
376 void set_Lu(
const VectorXs& _Lu) {
377 if (Lu.size() != _Lu.size()) {
379 "Invalid argument: " <<
"Lu has wrong dimension (it should be " +
380 std::to_string(Lu.size()) +
")");
384 void set_Lxx(
const MatrixXs& _Lxx) {
385 if (Lxx.rows() != _Lxx.rows() || Lxx.cols() != _Lxx.cols()) {
387 "Invalid argument: " <<
"Lxx has wrong dimension (it should be " +
388 std::to_string(Lxx.rows()) +
", " +
389 std::to_string(Lxx.cols()) +
")");
393 void set_Lxu(
const MatrixXs& _Lxu) {
394 if (Lxu.rows() != _Lxu.rows() || Lxu.cols() != _Lxu.cols()) {
396 "Invalid argument: " <<
"Lxu has wrong dimension (it should be " +
397 std::to_string(Lxu.rows()) +
", " +
398 std::to_string(Lxu.cols()) +
")");
402 void set_Luu(
const MatrixXs& _Luu) {
403 if (Luu.rows() != _Luu.rows() || Luu.cols() != _Luu.cols()) {
405 "Invalid argument: " <<
"Luu has wrong dimension (it should be " +
406 std::to_string(Luu.rows()) +
", " +
407 std::to_string(Luu.cols()) +
")");
413 VectorXs Lx_internal;
414 VectorXs Lu_internal;
415 MatrixXs Lxx_internal;
416 MatrixXs Lxu_internal;
417 MatrixXs Luu_internal;
422 Eigen::Map<VectorXs> Lx;
423 Eigen::Map<VectorXs> Lu;
424 Eigen::Map<MatrixXs> Lxx;
425 Eigen::Map<MatrixXs> Lxu;
426 Eigen::Map<MatrixXs> Luu;
434 #include "crocoddyl/core/costs/cost-sum.hxx"
Abstract class for cost models.
Summation of individual cost models.
void calcDiff(const std::shared_ptr< CostDataSum > &data, const Eigen::Ref< const VectorXs > &x)
Compute the Jacobian and Hessian of the total cost for nodes that depends on the state only.
void removeCost(const std::string &name)
Remove a cost item.
void addCost(const std::string &name, std::shared_ptr< CostModelAbstract > cost, const Scalar weight, const bool active=true)
Add a cost item.
std::shared_ptr< CostDataSum > createData(DataCollectorAbstract *const data)
Create the cost data.
void calc(const std::shared_ptr< CostDataSum > &data, const Eigen::Ref< const VectorXs > &x)
Compute the total cost value for nodes that depends only on the state.
void calc(const std::shared_ptr< CostDataSum > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the total cost value.
void changeCostStatus(const std::string &name, const bool active)
Change the cost status.
CostModelSumTpl(std::shared_ptr< StateAbstract > state)
Initialize the cost-sum model.
CostModelSumTpl(std::shared_ptr< StateAbstract > state, const std::size_t nu)
Initialize the cost-sum model.
void calcDiff(const std::shared_ptr< CostDataSum > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the Jacobian and Hessian of the total cost.
friend std::ostream & operator<<(std::ostream &os, const CostModelSumTpl< Scalar > &model)
Print information on the stack of costs.
std::size_t get_nr_total() const
Return the dimension of the total residual vector.
CostModelSumTpl< NewScalar > cast() const
Cast the cost-sum model to a different scalar type.
const std::set< std::string > & get_inactive_set() const
Return the names of the set of inactive costs.
const std::shared_ptr< StateAbstract > & get_state() const
Return the state.
const std::set< std::string > & get_active_set() const
Return the names of the set of active costs.
std::size_t get_nr() const
Return the dimension of the active residual vector.
const CostModelContainer & get_costs() const
Return the stack of cost models.
bool getCostStatus(const std::string &name) const
Return the status of a given cost name.
std::size_t get_nu() const
Return the dimension of the control input.
Abstract class for the state representation.
friend std::ostream & operator<<(std::ostream &os, const CostItemTpl< Scalar > &model)
Print information on the cost item.