aligator  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
cost-wrap.hpp
Go to the documentation of this file.
1
3#pragma once
4
7#include <crocoddyl/core/cost-base.hpp>
8#include <crocoddyl/core/action-base.hpp>
9
10namespace aligator::compat::croc {
11
12template <typename _Scalar>
13struct CrocCostModelWrapperTpl : CostAbstractTpl<_Scalar> {
14 using Scalar = _Scalar;
16 using CrocCostModel = crocoddyl::CostModelAbstractTpl<Scalar>;
17 using CrocActionModel = crocoddyl::ActionModelAbstractTpl<Scalar>;
21
22 boost::shared_ptr<CrocCostModel> croc_cost_;
23 boost::shared_ptr<CrocActionModel> action_model_;
24
26 explicit CrocCostModelWrapperTpl(boost::shared_ptr<CrocCostModel> cost)
27 : Base(StateWrap(cost->get_state()), (int)cost->get_nu()),
28 croc_cost_(cost) {}
29
32 boost::shared_ptr<CrocActionModel> action_model)
33 : Base(StateWrap(action_model->get_state()), (int)action_model->get_nu()),
34 action_model_(action_model) {}
35
36 void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
37 BaseData &data) const {
39 Data &d = static_cast<Data &>(data);
40 if (croc_cost_ != 0) {
41 croc_cost_->calc(d.croc_cost_data_, x, u);
42 d.value_ = d.croc_cost_data_->cost;
43 } else {
44 action_model_->calc(d.croc_act_data_, x);
45 d.value_ = d.croc_act_data_->cost;
46 }
47 }
48
49 void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u,
50 BaseData &data) const {
52 Data &d = static_cast<Data &>(data);
53 if (croc_cost_ != 0) {
54 croc_cost_->calcDiff(d.croc_cost_data_, x, u);
55 d.Lx_ = d.croc_cost_data_->Lx;
56 d.Lu_ = d.croc_cost_data_->Lu;
57 } else {
58 action_model_->calcDiff(d.croc_act_data_, x);
59 d.Lx_ = d.croc_act_data_->Lx;
60 d.Lu_ = d.croc_act_data_->Lu;
61 }
62 }
63
64 void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u,
65 BaseData &data) const {
67 Data &d = static_cast<Data &>(data);
68 if (croc_cost_ != 0) {
69 croc_cost_->calcDiff(d.croc_cost_data_, x, u);
70 d.Lxx_ = d.croc_cost_data_->Lxx;
71 d.Lxu_ = d.croc_cost_data_->Lxu;
72 d.Luu_ = d.croc_cost_data_->Luu;
73 } else {
74 action_model_->calcDiff(d.croc_act_data_, x);
75 d.Lxx_ = d.croc_act_data_->Lxx;
76 d.Lxu_ = d.croc_act_data_->Lxu;
77 d.Luu_ = d.croc_act_data_->Luu;
78 }
79 }
80
81 shared_ptr<BaseData> createData() const {
82 if (action_model_ != 0) {
83 boost::shared_ptr<crocoddyl::ActionDataAbstractTpl<Scalar>> am_data =
84 action_model_->createData();
85 return std::make_shared<CrocCostDataWrapperTpl<Scalar>>(am_data);
86 } else {
87 ALIGATOR_DOMAIN_ERROR("Invalid call. Cannot build Data from"
88 "crocoddyl cost model only.");
89 }
90 }
91};
92
93template <typename Scalar>
94struct CrocCostDataWrapperTpl : CostDataAbstractTpl<Scalar> {
95 using CostData = ::crocoddyl::CostDataAbstractTpl<Scalar>;
96 using ActionData = ::crocoddyl::ActionDataAbstractTpl<Scalar>;
98 boost::shared_ptr<CostData> croc_cost_data_;
99 boost::shared_ptr<ActionData> croc_act_data_;
100
101 explicit CrocCostDataWrapperTpl(const boost::shared_ptr<CostData> &crocdata)
102 : Base((int)crocdata->Lx.rows(), (int)crocdata->Lu.rows()),
103 croc_cost_data_(crocdata) {}
104
105 explicit CrocCostDataWrapperTpl(const boost::shared_ptr<ActionData> &actdata)
106 : Base((int)actdata->Lx.rows(), (int)actdata->Lu.rows()),
107 croc_act_data_(actdata) {}
108};
109
110#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
111extern template struct CrocCostModelWrapperTpl<context::Scalar>;
112extern template struct CrocCostDataWrapperTpl<context::Scalar>;
113#endif
114
115} // namespace aligator::compat::croc
#define ALIGATOR_DOMAIN_ERROR(msg)
Headers for the Crocoddyl compatibility module.
Stage costs for control problems.
Definition fwd.hpp:62
Data struct for CostAbstractTpl.
Definition fwd.hpp:65
CrocCostDataWrapperTpl(const boost::shared_ptr< ActionData > &actdata)
CrocCostDataWrapperTpl(const boost::shared_ptr< CostData > &crocdata)
::crocoddyl::ActionDataAbstractTpl< Scalar > ActionData
Definition cost-wrap.hpp:96
::crocoddyl::CostDataAbstractTpl< Scalar > CostData
Definition cost-wrap.hpp:95
boost::shared_ptr< CostData > croc_cost_data_
Definition cost-wrap.hpp:98
boost::shared_ptr< ActionData > croc_act_data_
Definition cost-wrap.hpp:99
void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const
Definition cost-wrap.hpp:64
crocoddyl::ActionModelAbstractTpl< Scalar > CrocActionModel
Definition cost-wrap.hpp:17
CrocCostModelWrapperTpl(boost::shared_ptr< CrocActionModel > action_model)
Constructor using a terminal action model.
Definition cost-wrap.hpp:31
boost::shared_ptr< CrocActionModel > action_model_
Definition cost-wrap.hpp:23
crocoddyl::CostModelAbstractTpl< Scalar > CrocCostModel
Definition cost-wrap.hpp:16
void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const
Definition cost-wrap.hpp:49
boost::shared_ptr< CrocCostModel > croc_cost_
Definition cost-wrap.hpp:22
void evaluate(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const
Definition cost-wrap.hpp:36
shared_ptr< BaseData > createData() const
Definition cost-wrap.hpp:81
CrocCostModelWrapperTpl(boost::shared_ptr< CrocCostModel > cost)
Constructor from a crocoddyl cost model.
Definition cost-wrap.hpp:26
Wraps a crocoddyl::StateAbstractTpl to a manifold (proxsuite::nlp::ManifoldAbstractTpl).