aligator  0.6.1
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 {
11namespace compat {
12namespace croc {
13
14template <typename _Scalar>
16 using Scalar = _Scalar;
18 using CrocCostModel = crocoddyl::CostModelAbstractTpl<Scalar>;
19 using CrocActionModel = crocoddyl::ActionModelAbstractTpl<Scalar>;
20 using Base = CostAbstractTpl<Scalar>;
21 using BaseData = CostDataAbstractTpl<Scalar>;
22 using StateWrap = StateWrapperTpl<Scalar>;
23
24 boost::shared_ptr<CrocCostModel> croc_cost_;
25 boost::shared_ptr<CrocActionModel> action_model_;
26
28 explicit CrocCostModelWrapperTpl(boost::shared_ptr<CrocCostModel> cost)
29 : Base(get_state_wrap(cost->get_state()), (int)cost->get_nu()),
30 croc_cost_(cost) {}
31
34 boost::shared_ptr<CrocActionModel> action_model)
35 : Base(get_state_wrap(action_model->get_state()),
36 (int)action_model->get_nu()),
37 action_model_(action_model) {}
38
39 void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
40 BaseData &data) const {
41 using Data = CrocCostDataWrapperTpl<Scalar>;
42 Data &d = static_cast<Data &>(data);
43 if (croc_cost_ != 0) {
44 croc_cost_->calc(d.croc_cost_data_, x, u);
45 d.value_ = d.croc_cost_data_->cost;
46 } else {
47 action_model_->calc(d.croc_act_data_, x);
48 d.value_ = d.croc_act_data_->cost;
49 }
50 }
51
52 void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u,
53 BaseData &data) const {
54 using Data = CrocCostDataWrapperTpl<Scalar>;
55 Data &d = static_cast<Data &>(data);
56 if (croc_cost_ != 0) {
57 croc_cost_->calcDiff(d.croc_cost_data_, x, u);
58 d.Lx_ = d.croc_cost_data_->Lx;
59 d.Lu_ = d.croc_cost_data_->Lu;
60 } else {
61 action_model_->calcDiff(d.croc_act_data_, x);
62 d.Lx_ = d.croc_act_data_->Lx;
63 d.Lu_ = d.croc_act_data_->Lu;
64 }
65 }
66
67 void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u,
68 BaseData &data) const {
69 using Data = CrocCostDataWrapperTpl<Scalar>;
70 Data &d = static_cast<Data &>(data);
71 if (croc_cost_ != 0) {
72 croc_cost_->calcDiff(d.croc_cost_data_, x, u);
73 d.Lxx_ = d.croc_cost_data_->Lxx;
74 d.Lxu_ = d.croc_cost_data_->Lxu;
75 d.Luu_ = d.croc_cost_data_->Luu;
76 } else {
77 action_model_->calcDiff(d.croc_act_data_, x);
78 d.Lxx_ = d.croc_act_data_->Lxx;
79 d.Lxu_ = d.croc_act_data_->Lxu;
80 d.Luu_ = d.croc_act_data_->Luu;
81 }
82 }
83
84 shared_ptr<BaseData> createData() const {
85 if (action_model_ != 0) {
86 boost::shared_ptr<crocoddyl::ActionDataAbstractTpl<Scalar>> am_data =
87 action_model_->createData();
88 return std::make_shared<CrocCostDataWrapperTpl<Scalar>>(am_data);
89 } else {
90 ALIGATOR_DOMAIN_ERROR("Invalid call. Cannot build Data from"
91 "crocoddyl cost model only.");
92 }
93 }
94
95private:
96 static auto
97 get_state_wrap(boost::shared_ptr<crocoddyl::StateAbstractTpl<Scalar>> state) {
98 return std::make_shared<StateWrap>(state);
99 }
100};
101
102template <typename Scalar>
104 using CostData = ::crocoddyl::CostDataAbstractTpl<Scalar>;
105 using ActionData = ::crocoddyl::ActionDataAbstractTpl<Scalar>;
106 using Base = CostDataAbstractTpl<Scalar>;
107 boost::shared_ptr<CostData> croc_cost_data_;
108 boost::shared_ptr<ActionData> croc_act_data_;
109
110 explicit CrocCostDataWrapperTpl(const boost::shared_ptr<CostData> &crocdata)
111 : Base((int)crocdata->Lx.rows(), (int)crocdata->Lu.rows()),
112 croc_cost_data_(crocdata) {}
113
114 explicit CrocCostDataWrapperTpl(const boost::shared_ptr<ActionData> &actdata)
115 : Base((int)actdata->Lx.rows(), (int)actdata->Lu.rows()),
116 croc_act_data_(actdata) {}
117};
118
119} // namespace croc
120} // namespace compat
121} // namespace aligator
#define ALIGATOR_DOMAIN_ERROR(msg)
Main package namespace.
Stage costs for control problems.
CrocCostDataWrapperTpl(const boost::shared_ptr< ActionData > &actdata)
CrocCostDataWrapperTpl(const boost::shared_ptr< CostData > &crocdata)
::crocoddyl::ActionDataAbstractTpl< Scalar > ActionData
::crocoddyl::CostDataAbstractTpl< Scalar > CostData
boost::shared_ptr< CostData > croc_cost_data_
boost::shared_ptr< ActionData > croc_act_data_
CrocCostModelWrapperTpl(boost::shared_ptr< CrocCostModel > cost)
Constructor from a crocoddyl cost model.
Definition cost-wrap.hpp:28
void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const
Compute the cost gradients .
Definition cost-wrap.hpp:52
boost::shared_ptr< CrocCostModel > croc_cost_
Definition cost-wrap.hpp:24
shared_ptr< BaseData > createData() const
Definition cost-wrap.hpp:84
crocoddyl::ActionModelAbstractTpl< Scalar > CrocActionModel
Definition cost-wrap.hpp:19
CrocCostModelWrapperTpl(boost::shared_ptr< CrocActionModel > action_model)
Constructor using a terminal action model.
Definition cost-wrap.hpp:33
void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const
Compute the cost Hessians .
Definition cost-wrap.hpp:67
void evaluate(const ConstVectorRef &x, const ConstVectorRef &u, BaseData &data) const
Evaluate the cost function.
Definition cost-wrap.hpp:39
crocoddyl::CostModelAbstractTpl< Scalar > CrocCostModel
Definition cost-wrap.hpp:18
boost::shared_ptr< CrocActionModel > action_model_
Definition cost-wrap.hpp:25