crocoddyl  1.8.1
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
cost-sum.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2021, LAAS-CNRS, University of Edinburgh
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_COSTS_COST_SUM_HPP_
10 #define CROCODDYL_CORE_COSTS_COST_SUM_HPP_
11 
12 #include <string>
13 #include <map>
14 #include <utility>
15 
16 #include "crocoddyl/core/fwd.hpp"
17 #include "crocoddyl/core/cost-base.hpp"
18 #include "crocoddyl/core/utils/exception.hpp"
19 
20 namespace crocoddyl {
21 
22 template <typename _Scalar>
23 struct CostItemTpl {
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25 
26  typedef _Scalar Scalar;
28 
29  CostItemTpl() {}
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) {}
33 
37  friend std::ostream& operator<<(std::ostream& os, const CostItemTpl<Scalar>& model) {
38  os << "{w=" << model.weight << ", " << *model.cost << "}";
39  return os;
40  }
41 
42  std::string name;
43  boost::shared_ptr<CostModelAbstract> cost;
44  Scalar weight;
45  bool active;
46 };
47 
64 template <typename _Scalar>
66  public:
67  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
68 
69  typedef _Scalar Scalar;
77  typedef typename MathBase::VectorXs VectorXs;
78  typedef typename MathBase::MatrixXs MatrixXs;
79 
80  typedef std::map<std::string, boost::shared_ptr<CostItem> > CostModelContainer;
81  typedef std::map<std::string, boost::shared_ptr<CostDataAbstract> > CostDataContainer;
82 
89  CostModelSumTpl(boost::shared_ptr<StateAbstract> state, const std::size_t nu);
90 
98  explicit CostModelSumTpl(boost::shared_ptr<StateAbstract> state);
99  ~CostModelSumTpl();
100 
109  void addCost(const std::string& name, boost::shared_ptr<CostModelAbstract> cost, const Scalar weight,
110  const bool active = true);
111 
117  void removeCost(const std::string& name);
118 
125  void changeCostStatus(const std::string& name, const bool active);
126 
134  void calc(const boost::shared_ptr<CostDataSum>& data, const Eigen::Ref<const VectorXs>& x,
135  const Eigen::Ref<const VectorXs>& u);
136 
144  void calcDiff(const boost::shared_ptr<CostDataSum>& data, const Eigen::Ref<const VectorXs>& x,
145  const Eigen::Ref<const VectorXs>& u);
146 
157  boost::shared_ptr<CostDataSum> createData(DataCollectorAbstract* const data);
158 
165  void calc(const boost::shared_ptr<CostDataSum>& data, const Eigen::Ref<const VectorXs>& x);
166 
173  void calcDiff(const boost::shared_ptr<CostDataSum>& data, const Eigen::Ref<const VectorXs>& x);
174 
178  const boost::shared_ptr<StateAbstract>& get_state() const;
179 
183  const CostModelContainer& get_costs() const;
184 
188  std::size_t get_nu() const;
189 
193  std::size_t get_nr() const;
194 
198  std::size_t get_nr_total() const;
199 
203  const std::vector<std::string>& get_active() const;
204 
208  const std::vector<std::string>& get_inactive() const;
209 
215  bool getCostStatus(const std::string& name) const;
216 
220  template <class Scalar>
221  friend std::ostream& operator<<(std::ostream& os, const CostModelSumTpl<Scalar>& model);
222 
223  private:
224  boost::shared_ptr<StateAbstract> state_;
225  CostModelContainer costs_;
226  std::size_t nu_;
227  std::size_t nr_;
228  std::size_t nr_total_;
229  std::vector<std::string> active_;
230  std::vector<std::string> inactive_;
231  VectorXs unone_;
232 };
233 
234 template <typename _Scalar>
236  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
237 
238  typedef _Scalar Scalar;
242  typedef typename MathBase::VectorXs VectorXs;
243  typedef typename MathBase::MatrixXs MatrixXs;
244 
245  template <template <typename Scalar> class Model>
246  CostDataSumTpl(Model<Scalar>* const model, DataCollectorAbstract* const data)
247  : Lx_internal(model->get_state()->get_ndx()),
248  Lu_internal(model->get_nu()),
249  Lxx_internal(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
250  Lxu_internal(model->get_state()->get_ndx(), model->get_nu()),
251  Luu_internal(model->get_nu(), model->get_nu()),
252  shared(data),
253  cost(Scalar(0.)),
254  Lx(Lx_internal.data(), model->get_state()->get_ndx()),
255  Lu(Lu_internal.data(), model->get_nu()),
256  Lxx(Lxx_internal.data(), model->get_state()->get_ndx(), model->get_state()->get_ndx()),
257  Lxu(Lxu_internal.data(), model->get_state()->get_ndx(), model->get_nu()),
258  Luu(Luu_internal.data(), model->get_nu(), model->get_nu()) {
259  Lx.setZero();
260  Lu.setZero();
261  Lxx.setZero();
262  Lxu.setZero();
263  Luu.setZero();
265  it != model->get_costs().end(); ++it) {
266  const boost::shared_ptr<CostItem>& item = it->second;
267  costs.insert(std::make_pair(item->name, item->cost->createData(data)));
268  }
269  }
270 
271  template <class ActionData>
272  void shareMemory(ActionData* const data) {
273  // Share memory with the differential action data
274  new (&Lx) Eigen::Map<VectorXs>(data->Lx.data(), data->Lx.size());
275  new (&Lu) Eigen::Map<VectorXs>(data->Lu.data(), data->Lu.size());
276  new (&Lxx) Eigen::Map<MatrixXs>(data->Lxx.data(), data->Lxx.rows(), data->Lxx.cols());
277  new (&Lxu) Eigen::Map<MatrixXs>(data->Lxu.data(), data->Lxu.rows(), data->Lxu.cols());
278  new (&Luu) Eigen::Map<MatrixXs>(data->Luu.data(), data->Luu.rows(), data->Luu.cols());
279  }
280 
281  VectorXs get_Lx() const { return Lx; }
282  VectorXs get_Lu() const { return Lu; }
283  MatrixXs get_Lxx() const { return Lxx; }
284  MatrixXs get_Lxu() const { return Lxu; }
285  MatrixXs get_Luu() const { return Luu; }
286 
287  void set_Lx(const VectorXs& _Lx) {
288  if (Lx.size() != _Lx.size()) {
289  throw_pretty("Invalid argument: "
290  << "Lx has wrong dimension (it should be " + std::to_string(Lx.size()) + ")");
291  }
292  Lx = _Lx;
293  }
294  void set_Lu(const VectorXs& _Lu) {
295  if (Lu.size() != _Lu.size()) {
296  throw_pretty("Invalid argument: "
297  << "Lu has wrong dimension (it should be " + std::to_string(Lu.size()) + ")");
298  }
299  Lu = _Lu;
300  }
301  void set_Lxx(const MatrixXs& _Lxx) {
302  if (Lxx.rows() != _Lxx.rows() || Lxx.cols() != _Lxx.cols()) {
303  throw_pretty("Invalid argument: "
304  << "Lxx has wrong dimension (it should be " + std::to_string(Lxx.rows()) + ", " +
305  std::to_string(Lxx.cols()) + ")");
306  }
307  Lxx = _Lxx;
308  }
309  void set_Lxu(const MatrixXs& _Lxu) {
310  if (Lxu.rows() != _Lxu.rows() || Lxu.cols() != _Lxu.cols()) {
311  throw_pretty("Invalid argument: "
312  << "Lxu has wrong dimension (it should be " + std::to_string(Lxu.rows()) + ", " +
313  std::to_string(Lxu.cols()) + ")");
314  }
315  Lxu = _Lxu;
316  }
317  void set_Luu(const MatrixXs& _Luu) {
318  if (Luu.rows() != _Luu.rows() || Luu.cols() != _Luu.cols()) {
319  throw_pretty("Invalid argument: "
320  << "Luu has wrong dimension (it should be " + std::to_string(Luu.rows()) + ", " +
321  std::to_string(Luu.cols()) + ")");
322  }
323  Luu = _Luu;
324  }
325 
326  // Creates internal data in case we don't share it externally
327  VectorXs Lx_internal;
328  VectorXs Lu_internal;
329  MatrixXs Lxx_internal;
330  MatrixXs Lxu_internal;
331  MatrixXs Luu_internal;
332 
334  DataCollectorAbstract* shared;
335  Scalar cost;
336  Eigen::Map<VectorXs> Lx;
337  Eigen::Map<VectorXs> Lu;
338  Eigen::Map<MatrixXs> Lxx;
339  Eigen::Map<MatrixXs> Lxu;
340  Eigen::Map<MatrixXs> Luu;
341 };
342 
343 } // namespace crocoddyl
344 
345 /* --- Details -------------------------------------------------------------- */
346 /* --- Details -------------------------------------------------------------- */
347 /* --- Details -------------------------------------------------------------- */
348 #include "crocoddyl/core/costs/cost-sum.hxx"
349 
350 #endif // CROCODDYL_CORE_COSTS_COST_SUM_HPP_
Abstract class for cost models.
Definition: cost-base.hpp:49
Abstract class for the state representation.
Definition: fwd.hpp:112
const CostModelContainer & get_costs() const
Return the stack of cost models.
Summation of individual cost models.
Definition: cost-sum.hpp:65