GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/crocoddyl/core/constraints/residual.hpp Lines: 2 4 50.0 %
Date: 2024-02-13 11:12:33 Branches: 0 0 - %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2021-2022, Heriot-Watt University, University of Edinburgh
5
// Copyright note valid unless otherwise stated in individual files.
6
// All rights reserved.
7
///////////////////////////////////////////////////////////////////////////////
8
9
#ifndef CROCODDYL_CORE_CONSTRAINTS_RESIDUAL_CONSTRAINT_HPP_
10
#define CROCODDYL_CORE_CONSTRAINTS_RESIDUAL_CONSTRAINT_HPP_
11
12
#include "crocoddyl/core/fwd.hpp"
13
//
14
#include "crocoddyl/core/constraint-base.hpp"
15
#include "crocoddyl/core/residual-base.hpp"
16
17
namespace crocoddyl {
18
19
/**
20
 * @brief Residual-based constraint
21
 *
22
 * This constraint function uses a residual model to define equality /
23
 * inequality constraint as \f[ \mathbf{\underline{r}} \leq
24
 * \mathbf{r}(\mathbf{x}, \mathbf{u}) \leq \mathbf{\bar{r}} \f] where
25
 * \f$\mathbf{r}(\cdot)\f$ describes the residual function, and
26
 * \f$\mathbf{\underline{r}}\f$, \f$\mathbf{\bar{r}}\f$ are the lower and upper
27
 * bounds, respectively. We can define element-wise equality constraints by
28
 * defining the same value for both: lower and upper values. Additionally, if we
29
 * do not define the bounds, then it is assumed that
30
 * \f$\mathbf{\underline{r}}=\mathbf{\bar{r}}=\mathbf{0}\f$.
31
 *
32
 * The main computations are carring out in `calc` and `calcDiff` routines.
33
 * `calc` computes the constraint residual and `calcDiff` computes the Jacobians
34
 * of the constraint function. Concretely speaking, `calcDiff` builds a linear
35
 * approximation of the constraint function with the form:
36
 * \f$\mathbf{g_x}\in\mathbb{R}^{ng\times ndx}\f$,
37
 * \f$\mathbf{g_u}\in\mathbb{R}^{ng\times nu}\f$,
38
 * \f$\mathbf{h_x}\in\mathbb{R}^{nh\times ndx}\f$
39
 * \f$\mathbf{h_u}\in\mathbb{R}^{nh\times nu}\f$.
40
 * Additionally, it is important to note that `calcDiff()` computes the
41
 * derivatives using the latest stored values by `calc()`. Thus, we need to run
42
 * first `calc()`.
43
 *
44
 * \sa `ConstraintModelAbstractTpl`, `calc()`, `calcDiff()`, `createData()`
45
 */
46
template <typename _Scalar>
47
class ConstraintModelResidualTpl : public ConstraintModelAbstractTpl<_Scalar> {
48
 public:
49
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50
51
  typedef _Scalar Scalar;
52
  typedef MathBaseTpl<Scalar> MathBase;
53
  typedef ConstraintModelAbstractTpl<Scalar> Base;
54
  typedef ConstraintDataResidualTpl<Scalar> Data;
55
  typedef ConstraintDataAbstractTpl<Scalar> ConstraintDataAbstract;
56
  typedef ResidualModelAbstractTpl<Scalar> ResidualModelAbstract;
57
  typedef typename MathBase::VectorXs VectorXs;
58
59
  /**
60
   * @brief Initialize the residual constraint model as an inequality constraint
61
   *
62
   * @param[in] state       State of the multibody system
63
   * @param[in] residual    Residual model
64
   * @param[in] lower       Lower bound (dimension of the residual vector)
65
   * @param[in] upper       Upper bound (dimension of the residual vector)
66
   */
67
  ConstraintModelResidualTpl(
68
      boost::shared_ptr<typename Base::StateAbstract> state,
69
      boost::shared_ptr<ResidualModelAbstract> residual, const VectorXs& lower,
70
      const VectorXs& upper);
71
72
  /**
73
   * @brief Initialize the residual constraint model as an equality constraint
74
   *
75
   * @param[in] state       State of the multibody system
76
   * @param[in] residual    Residual model
77
   */
78
  ConstraintModelResidualTpl(
79
      boost::shared_ptr<typename Base::StateAbstract> state,
80
      boost::shared_ptr<ResidualModelAbstract> residual);
81
  virtual ~ConstraintModelResidualTpl();
82
83
  /**
84
   * @brief Compute the residual constraint
85
   *
86
   * @param[in] data  Residual constraint data
87
   * @param[in] x     State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
88
   * @param[in] u     Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
89
   */
90
  virtual void calc(const boost::shared_ptr<ConstraintDataAbstract>& data,
91
                    const Eigen::Ref<const VectorXs>& x,
92
                    const Eigen::Ref<const VectorXs>& u);
93
94
  /**
95
   * @brief Compute the residual constraint based on state only
96
   *
97
   * It updates the constraint based on the state only. This function is
98
   * commonly used in the terminal nodes of an optimal control problem.
99
   *
100
   * @param[in] data  Residual constraint data
101
   * @param[in] x     State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
102
   */
103
  virtual void calc(const boost::shared_ptr<ConstraintDataAbstract>& data,
104
                    const Eigen::Ref<const VectorXs>& x);
105
106
  /**
107
   * @brief Compute the derivatives of the residual constraint
108
   *
109
   * @param[in] data  Residual constraint data
110
   * @param[in] x     State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
111
   * @param[in] u     Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
112
   */
113
  virtual void calcDiff(const boost::shared_ptr<ConstraintDataAbstract>& data,
114
                        const Eigen::Ref<const VectorXs>& x,
115
                        const Eigen::Ref<const VectorXs>& u);
116
117
  /**
118
   * @brief Compute the derivatives of the residual constraint with respect to
119
   * the state only
120
   *
121
   * It updates the Jacobian of the constraint function based on the state only.
122
   * This function is commonly used in the terminal nodes of an optimal control
123
   * problem.
124
   *
125
   * @param[in] data  Residual constraint data
126
   * @param[in] x     State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
127
   */
128
  virtual void calcDiff(const boost::shared_ptr<ConstraintDataAbstract>& data,
129
                        const Eigen::Ref<const VectorXs>& x);
130
131
  /**
132
   * @brief Create the residual constraint data
133
   */
134
  virtual boost::shared_ptr<ConstraintDataAbstract> createData(
135
      DataCollectorAbstract* const data);
136
137
  /**
138
   * @brief Print relevant information of the cost-residual model
139
   *
140
   * @param[out] os  Output stream object
141
   */
142
  virtual void print(std::ostream& os) const;
143
144
 private:
145
  void updateCalc(const boost::shared_ptr<ConstraintDataAbstract>& data);
146
  void updateCalcDiff(const boost::shared_ptr<ConstraintDataAbstract>& data);
147
148
 protected:
149
  using Base::lb_;
150
  using Base::ng_;
151
  using Base::nh_;
152
  using Base::nu_;
153
  using Base::residual_;
154
  using Base::state_;
155
  using Base::type_;
156
  using Base::ub_;
157
  using Base::unone_;
158
};
159
160
template <typename _Scalar>
161
struct ConstraintDataResidualTpl : public ConstraintDataAbstractTpl<_Scalar> {
162
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
163
164
  typedef _Scalar Scalar;
165
  typedef MathBaseTpl<Scalar> MathBase;
166
  typedef ConstraintDataAbstractTpl<Scalar> Base;
167
  typedef DataCollectorAbstractTpl<Scalar> DataCollectorAbstract;
168
169
  template <template <typename Scalar> class Model>
170
229668
  ConstraintDataResidualTpl(Model<Scalar>* const model,
171
                            DataCollectorAbstract* const data)
172
229668
      : Base(model, data) {}
173
174
  using Base::g;
175
  using Base::Gu;
176
  using Base::Gx;
177
  using Base::h;
178
  using Base::Hu;
179
  using Base::Hx;
180
  using Base::residual;
181
  using Base::shared;
182
};
183
184
}  // namespace crocoddyl
185
186
/* --- Details -------------------------------------------------------------- */
187
/* --- Details -------------------------------------------------------------- */
188
/* --- Details -------------------------------------------------------------- */
189
#include "crocoddyl/core/constraints/residual.hxx"
190
191
#endif  // CROCODDYL_CORE_CONSTRAINTS_RESIDUAL_CONSTRAINT_HPP_