| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2021-2025, University of Edinburgh, INRIA, | ||
| 5 | // Heriot-Watt University | ||
| 6 | // Copyright note valid unless otherwise stated in individual files. | ||
| 7 | // All rights reserved. | ||
| 8 | /////////////////////////////////////////////////////////////////////////////// | ||
| 9 | |||
| 10 | namespace crocoddyl { | ||
| 11 | template <typename Scalar> | ||
| 12 | ✗ | CostModelResidualTpl<Scalar>::CostModelResidualTpl( | |
| 13 | std::shared_ptr<typename Base::StateAbstract> state, | ||
| 14 | std::shared_ptr<ActivationModelAbstract> activation, | ||
| 15 | std::shared_ptr<ResidualModelAbstract> residual) | ||
| 16 | ✗ | : Base(state, activation, residual) {} | |
| 17 | |||
| 18 | template <typename Scalar> | ||
| 19 | ✗ | CostModelResidualTpl<Scalar>::CostModelResidualTpl( | |
| 20 | std::shared_ptr<typename Base::StateAbstract> state, | ||
| 21 | std::shared_ptr<ResidualModelAbstract> residual) | ||
| 22 | ✗ | : Base(state, residual) {} | |
| 23 | |||
| 24 | template <typename Scalar> | ||
| 25 | ✗ | void CostModelResidualTpl<Scalar>::calc( | |
| 26 | const std::shared_ptr<CostDataAbstract>& data, | ||
| 27 | const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u) { | ||
| 28 | // Compute the cost residual | ||
| 29 | ✗ | residual_->calc(data->residual, x, u); | |
| 30 | |||
| 31 | // Compute the cost | ||
| 32 | ✗ | activation_->calc(data->activation, data->residual->r); | |
| 33 | ✗ | data->cost = data->activation->a_value; | |
| 34 | ✗ | } | |
| 35 | |||
| 36 | template <typename Scalar> | ||
| 37 | ✗ | void CostModelResidualTpl<Scalar>::calc( | |
| 38 | const std::shared_ptr<CostDataAbstract>& data, | ||
| 39 | const Eigen::Ref<const VectorXs>& x) { | ||
| 40 | ✗ | const bool is_rq = residual_->get_q_dependent(); | |
| 41 | ✗ | const bool is_rv = residual_->get_v_dependent(); | |
| 42 | ✗ | if (!is_rq && !is_rv) { | |
| 43 | ✗ | data->activation->a_value = 0.; | |
| 44 | ✗ | data->cost = 0.; | |
| 45 | ✗ | return; // do nothing | |
| 46 | } | ||
| 47 | |||
| 48 | // Compute the cost residual | ||
| 49 | ✗ | residual_->calc(data->residual, x); | |
| 50 | |||
| 51 | // Compute the cost | ||
| 52 | ✗ | activation_->calc(data->activation, data->residual->r); | |
| 53 | ✗ | data->cost = data->activation->a_value; | |
| 54 | } | ||
| 55 | |||
| 56 | template <typename Scalar> | ||
| 57 | ✗ | void CostModelResidualTpl<Scalar>::calcDiff( | |
| 58 | const std::shared_ptr<CostDataAbstract>& data, | ||
| 59 | const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u) { | ||
| 60 | // Compute the derivatives of the activation and contact wrench cone residual | ||
| 61 | // models | ||
| 62 | ✗ | residual_->calcDiff(data->residual, x, u); | |
| 63 | ✗ | activation_->calcDiff(data->activation, data->residual->r); | |
| 64 | |||
| 65 | // Compute the derivatives of the cost function based on a Gauss-Newton | ||
| 66 | // approximation | ||
| 67 | ✗ | residual_->calcCostDiff(data, data->residual, data->activation); | |
| 68 | ✗ | } | |
| 69 | |||
| 70 | template <typename Scalar> | ||
| 71 | ✗ | void CostModelResidualTpl<Scalar>::calcDiff( | |
| 72 | const std::shared_ptr<CostDataAbstract>& data, | ||
| 73 | const Eigen::Ref<const VectorXs>& x) { | ||
| 74 | // Compute the derivatives of the activation and contact wrench cone residual | ||
| 75 | // models | ||
| 76 | ✗ | const bool is_rq = residual_->get_q_dependent(); | |
| 77 | ✗ | const bool is_rv = residual_->get_v_dependent(); | |
| 78 | ✗ | if (!is_rq && !is_rv) { | |
| 79 | ✗ | data->Lx.setZero(); | |
| 80 | ✗ | data->Lxx.setZero(); | |
| 81 | ✗ | return; // do nothing | |
| 82 | } | ||
| 83 | ✗ | residual_->calcDiff(data->residual, x); | |
| 84 | ✗ | activation_->calcDiff(data->activation, data->residual->r); | |
| 85 | |||
| 86 | // Compute the derivatives of the cost function based on a Gauss-Newton | ||
| 87 | // approximation | ||
| 88 | ✗ | residual_->calcCostDiff(data, data->residual, data->activation, false); | |
| 89 | } | ||
| 90 | |||
| 91 | template <typename Scalar> | ||
| 92 | std::shared_ptr<CostDataAbstractTpl<Scalar> > | ||
| 93 | ✗ | CostModelResidualTpl<Scalar>::createData(DataCollectorAbstract* const data) { | |
| 94 | ✗ | return std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this, | |
| 95 | ✗ | data); | |
| 96 | } | ||
| 97 | |||
| 98 | template <typename Scalar> | ||
| 99 | template <typename NewScalar> | ||
| 100 | ✗ | CostModelResidualTpl<NewScalar> CostModelResidualTpl<Scalar>::cast() const { | |
| 101 | typedef CostModelResidualTpl<NewScalar> ReturnType; | ||
| 102 | ✗ | if (activation_) { | |
| 103 | ✗ | ReturnType ret(state_->template cast<NewScalar>(), | |
| 104 | ✗ | activation_->template cast<NewScalar>(), | |
| 105 | ✗ | residual_->template cast<NewScalar>()); | |
| 106 | ✗ | return ret; | |
| 107 | ✗ | } else { | |
| 108 | ✗ | ReturnType ret(state_->template cast<NewScalar>(), | |
| 109 | ✗ | residual_->template cast<NewScalar>()); | |
| 110 | ✗ | return ret; | |
| 111 | ✗ | } | |
| 112 | } | ||
| 113 | |||
| 114 | template <typename Scalar> | ||
| 115 | ✗ | void CostModelResidualTpl<Scalar>::print(std::ostream& os) const { | |
| 116 | ✗ | os << "CostModelResidual {" << *residual_ << ", " << *activation_ << "}"; | |
| 117 | ✗ | } | |
| 118 | |||
| 119 | } // namespace crocoddyl | ||
| 120 |