9 #ifndef CROCODDYL_CORE_COSTS_COST_SUM_HPP_
10 #define CROCODDYL_CORE_COSTS_COST_SUM_HPP_
16 #include "crocoddyl/core/fwd.hpp"
17 #include "crocoddyl/core/cost-base.hpp"
18 #include "crocoddyl/core/utils/exception.hpp"
22 template <
typename _Scalar>
24 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26 typedef _Scalar Scalar;
30 CostItemTpl(
const std::string& name, boost::shared_ptr<CostModelAbstract> cost,
const Scalar weight,
31 const bool active =
true)
32 : name(name), cost(cost), weight(weight), active(active) {}
38 os <<
"{w=" << model.weight <<
", " << *model.cost <<
"}";
43 boost::shared_ptr<CostModelAbstract> cost;
64 template <
typename _Scalar>
67 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
69 typedef _Scalar Scalar;
77 typedef typename MathBase::VectorXs VectorXs;
78 typedef typename MathBase::MatrixXs MatrixXs;
80 typedef std::map<std::string, boost::shared_ptr<CostItem> > CostModelContainer;
81 typedef std::map<std::string, boost::shared_ptr<CostDataAbstract> > CostDataContainer;
89 CostModelSumTpl(boost::shared_ptr<StateAbstract> state,
const std::size_t nu);
109 void addCost(
const std::string& name, boost::shared_ptr<CostModelAbstract> cost,
const Scalar weight,
110 const bool active =
true);
134 void calc(
const boost::shared_ptr<CostDataSum>& data,
const Eigen::Ref<const VectorXs>& x,
135 const Eigen::Ref<const VectorXs>& u);
146 void calc(
const boost::shared_ptr<CostDataSum>& data,
const Eigen::Ref<const VectorXs>& x);
155 void calcDiff(
const boost::shared_ptr<CostDataSum>& data,
const Eigen::Ref<const VectorXs>& x,
156 const Eigen::Ref<const VectorXs>& u);
168 void calcDiff(
const boost::shared_ptr<CostDataSum>& data,
const Eigen::Ref<const VectorXs>& x);
185 const boost::shared_ptr<StateAbstract>&
get_state()
const;
190 const CostModelContainer&
get_costs()
const;
195 std::size_t
get_nu()
const;
200 std::size_t
get_nr()
const;
210 const std::vector<std::string>&
get_active()
const;
227 template <
class Scalar>
231 boost::shared_ptr<StateAbstract> state_;
232 CostModelContainer costs_;
235 std::size_t nr_total_;
236 std::vector<std::string> active_;
237 std::vector<std::string> inactive_;
240 template <
typename _Scalar>
242 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
244 typedef _Scalar Scalar;
248 typedef typename MathBase::VectorXs VectorXs;
249 typedef typename MathBase::MatrixXs MatrixXs;
251 template <
template <
typename Scalar>
class Model>
253 : Lx_internal(model->get_state()->get_ndx()),
254 Lu_internal(model->get_nu()),
255 Lxx_internal(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
256 Lxu_internal(model->get_state()->get_ndx(), model->get_nu()),
257 Luu_internal(model->get_nu(), model->get_nu()),
260 Lx(Lx_internal.data(), model->get_state()->get_ndx()),
261 Lu(Lu_internal.data(), model->get_nu()),
262 Lxx(Lxx_internal.data(), model->get_state()->get_ndx(), model->get_state()->get_ndx()),
263 Lxu(Lxu_internal.data(), model->get_state()->get_ndx(), model->get_nu()),
264 Luu(Luu_internal.data(), model->get_nu(), model->get_nu()) {
271 it != model->get_costs().end(); ++it) {
272 const boost::shared_ptr<CostItem>& item = it->second;
273 costs.insert(std::make_pair(item->name, item->cost->createData(data)));
277 template <
class ActionData>
278 void shareMemory(ActionData*
const data) {
280 new (&Lx) Eigen::Map<VectorXs>(data->Lx.data(), data->Lx.size());
281 new (&Lu) Eigen::Map<VectorXs>(data->Lu.data(), data->Lu.size());
282 new (&Lxx) Eigen::Map<MatrixXs>(data->Lxx.data(), data->Lxx.rows(), data->Lxx.cols());
283 new (&Lxu) Eigen::Map<MatrixXs>(data->Lxu.data(), data->Lxu.rows(), data->Lxu.cols());
284 new (&Luu) Eigen::Map<MatrixXs>(data->Luu.data(), data->Luu.rows(), data->Luu.cols());
287 VectorXs get_Lx()
const {
return Lx; }
288 VectorXs get_Lu()
const {
return Lu; }
289 MatrixXs get_Lxx()
const {
return Lxx; }
290 MatrixXs get_Lxu()
const {
return Lxu; }
291 MatrixXs get_Luu()
const {
return Luu; }
293 void set_Lx(
const VectorXs& _Lx) {
294 if (Lx.size() != _Lx.size()) {
295 throw_pretty(
"Invalid argument: "
296 <<
"Lx has wrong dimension (it should be " + std::to_string(Lx.size()) +
")");
300 void set_Lu(
const VectorXs& _Lu) {
301 if (Lu.size() != _Lu.size()) {
302 throw_pretty(
"Invalid argument: "
303 <<
"Lu has wrong dimension (it should be " + std::to_string(Lu.size()) +
")");
307 void set_Lxx(
const MatrixXs& _Lxx) {
308 if (Lxx.rows() != _Lxx.rows() || Lxx.cols() != _Lxx.cols()) {
309 throw_pretty(
"Invalid argument: "
310 <<
"Lxx has wrong dimension (it should be " + std::to_string(Lxx.rows()) +
", " +
311 std::to_string(Lxx.cols()) +
")");
315 void set_Lxu(
const MatrixXs& _Lxu) {
316 if (Lxu.rows() != _Lxu.rows() || Lxu.cols() != _Lxu.cols()) {
317 throw_pretty(
"Invalid argument: "
318 <<
"Lxu has wrong dimension (it should be " + std::to_string(Lxu.rows()) +
", " +
319 std::to_string(Lxu.cols()) +
")");
323 void set_Luu(
const MatrixXs& _Luu) {
324 if (Luu.rows() != _Luu.rows() || Luu.cols() != _Luu.cols()) {
325 throw_pretty(
"Invalid argument: "
326 <<
"Luu has wrong dimension (it should be " + std::to_string(Luu.rows()) +
", " +
327 std::to_string(Luu.cols()) +
")");
333 VectorXs Lx_internal;
334 VectorXs Lu_internal;
335 MatrixXs Lxx_internal;
336 MatrixXs Lxu_internal;
337 MatrixXs Luu_internal;
342 Eigen::Map<VectorXs> Lx;
343 Eigen::Map<VectorXs> Lu;
344 Eigen::Map<MatrixXs> Lxx;
345 Eigen::Map<MatrixXs> Lxu;
346 Eigen::Map<MatrixXs> Luu;
354 #include "crocoddyl/core/costs/cost-sum.hxx"
356 #endif // CROCODDYL_CORE_COSTS_COST_SUM_HPP_