4#ifndef CGMRES__OCP_CARTPOLE_EXTERNAL_REFERENCE_HPP_
5#define CGMRES__OCP_CARTPOLE_EXTERNAL_REFERENCE_HPP_
7#define _USE_MATH_DEFINES
29 static constexpr int nx = 4;
34 static constexpr int nu = 1;
39 static constexpr int nc = 0;
44 static constexpr int nh = 0;
54 static constexpr int nub = 1;
61 std::array<double, 4>
q = {2.5, 10, 0.01, 0.01};
62 std::array<double, 4>
q_terminal = {2.5, 10, 0.01, 0.01};
64 std::array<double, 1>
r = {1};
67 std::array<double, nub>
umin = {-15.0};
68 std::array<double, nub>
umax = {15.0};
72 os <<
"OCP_cartpoleExternalReference:" << std::endl;
73 os <<
" nx: " <<
nx << std::endl;
74 os <<
" nu: " <<
nu << std::endl;
75 os <<
" nc: " <<
nc << std::endl;
76 os <<
" nh: " <<
nh << std::endl;
77 os <<
" nuc: " <<
nuc << std::endl;
78 os <<
" nub: " <<
nub << std::endl;
80 os <<
" m_c: " <<
m_c << std::endl;
81 os <<
" m_p: " <<
m_p << std::endl;
82 os <<
" l: " <<
l << std::endl;
83 os <<
" g: " <<
g << std::endl;
85 Eigen::IOFormat
fmt(4, 0,
", ",
"",
"[",
"]");
86 Eigen::IOFormat
intfmt(1, 0,
", ",
"",
"[",
"]");
138 void eval_f(
const double t,
const double* x,
const double*
u,
140 const double x0 =
sin(x[1]);
142 const double x2 =
cos(x[1]);
143 const double x3 =
l*
pow(x[1], 2);
180 void eval_hx(
const double t,
const double* x,
const double*
u,
181 const double* lmd,
double*
hx)
const {
182 const double x0 = 2*x[1];
183 const double x1 =
sin(x[1]);
184 const double x2 =
cos(x[1]);
185 const double x3 =
g*
x2;
186 const double x4 =
pow(x[1], 2);
187 const double x5 =
l*
x4;
194 const double x12 = 1.0/
x8;
197 const double x15 = lmd[3]/
l;
198 hx[0] = (1.0/2.0)*
q[0]*(2*x[0] - 2*
x_ref[0]);
200 hx[2] = lmd[0] + (1.0/2.0)*
q[2]*(2*x[2] - 2*
x_ref[2]);
201 hx[3] = lmd[1] + (1.0/2.0)*
q[3]*(2*x[3] - 2*
x_ref[3]);
216 void eval_hu(
const double t,
const double* x,
const double*
u,
217 const double* lmd,
double*
hu)
const {
230 template <
typename VectorType1,
typename VectorType2,
typename VectorType3>
234 if (x.size() !=
nx) {
235 throw std::invalid_argument(
"[OCP]: x.size() must be " + std::to_string(
nx));
237 if (
u.size() !=
nu) {
238 throw std::invalid_argument(
"[OCP]: u.size() must be " + std::to_string(
nu));
240 if (
dx.size() !=
nx) {
241 throw std::invalid_argument(
"[OCP]: dx.size() must be " + std::to_string(
nx));
253 template <
typename VectorType1,
typename VectorType2>
256 if (x.size() !=
nx) {
257 throw std::invalid_argument(
"[OCP]: x.size() must be " + std::to_string(
nx));
260 throw std::invalid_argument(
"[OCP]: phix.size() must be " + std::to_string(
nx));
274 template <
typename VectorType1,
typename VectorType2,
typename VectorType3,
typename VectorType4>
279 if (x.size() !=
nx) {
280 throw std::invalid_argument(
"[OCP]: x.size() must be " + std::to_string(
nx));
282 if (
uc.size() !=
nuc) {
283 throw std::invalid_argument(
"[OCP]: uc.size() must be " + std::to_string(
nuc));
285 if (lmd.size() !=
nx) {
286 throw std::invalid_argument(
"[OCP]: lmd.size() must be " + std::to_string(
nx));
288 if (
hx.size() !=
nuc) {
289 throw std::invalid_argument(
"[OCP]: hx.size() must be " + std::to_string(
nx));
303 template <
typename VectorType1,
typename VectorType2,
typename VectorType3,
typename VectorType4>
308 if (x.size() !=
nx) {
309 throw std::invalid_argument(
"[OCP]: x.size() must be " + std::to_string(
nx));
311 if (
uc.size() !=
nuc) {
312 throw std::invalid_argument(
"[OCP]: uc.size() must be " + std::to_string(
nuc));
314 if (lmd.size() !=
nx) {
315 throw std::invalid_argument(
"[OCP]: lmd.size() must be " + std::to_string(
nx));
317 if (
hu.size() !=
nuc) {
318 throw std::invalid_argument(
"[OCP]: hu.size() must be " + std::to_string(
nuc));
Definition of the optimal control problem (OCP) of cartpoleExternalReference.
Definition ocp.hpp:23
void eval_hx(const double t, const MatrixBase< VectorType1 > &x, const MatrixBase< VectorType2 > &uc, const MatrixBase< VectorType3 > &lmd, const MatrixBase< VectorType4 > &hx) const
Computes the partial derivative of the Hamiltonian with respect to the state, i.e....
Definition ocp.hpp:275
void eval_hx(const double t, const double *x, const double *u, const double *lmd, double *hx) const
Computes the partial derivative of the Hamiltonian with respect to state, i.e., hx = dH/dx(t,...
Definition ocp.hpp:180
void disp(std::ostream &os) const
Definition ocp.hpp:71
static constexpr int nu
Dimension of the control input.
Definition ocp.hpp:34
void eval_f(const double t, const MatrixBase< VectorType1 > &x, const MatrixBase< VectorType2 > &u, const MatrixBase< VectorType3 > &dx) const
Computes the state equation dx = f(t, x, u).
Definition ocp.hpp:231
static constexpr int nh
Dimension of the Fischer-Burmeister function (already counded in nc).
Definition ocp.hpp:44
void eval_hu(const double t, const MatrixBase< VectorType1 > &x, const MatrixBase< VectorType2 > &uc, const MatrixBase< VectorType3 > &lmd, const MatrixBase< VectorType4 > &hu) const
Computes the partial derivative of the Hamiltonian with respect to control input and the equality con...
Definition ocp.hpp:304
std::array< double, nub > umin
Definition ocp.hpp:67
static constexpr std::array< int, nub > ubound_indices
Definition ocp.hpp:66
void eval_phix(const double t, const double *x, double *phix) const
Computes the partial derivative of terminal cost with respect to state, i.e., phix = dphi/dx(t,...
Definition ocp.hpp:161
std::array< double, 4 > x_ref
Definition ocp.hpp:63
double l
Definition ocp.hpp:58
std::array< double, nub > umax
Definition ocp.hpp:68
void eval_phix(const double t, const MatrixBase< VectorType1 > &x, const MatrixBase< VectorType2 > &phix) const
Computes the partial derivative of terminal cost with respect to state, i.e., phix = dphi/dx(t,...
Definition ocp.hpp:254
std::array< double, 4 > q
Definition ocp.hpp:61
friend std::ostream & operator<<(std::ostream &os, const OCP_cartpoleExternalReference &ocp)
Definition ocp.hpp:98
void eval_f(const double t, const double *x, const double *u, double *dx) const
Computes the state equation dx = f(t, x, u).
Definition ocp.hpp:138
std::shared_ptr< ExternalReference > external_reference
Shared ptr to the external reference of the cart pole.
Definition ocp.hpp:117
static constexpr int nuc
Dimension of the concatenation of the control input and equality constraints.
Definition ocp.hpp:49
static constexpr int nub
Dimension of the bound constraints on the control input.
Definition ocp.hpp:54
void synchronize()
Synchrozies the internal parameters of this OCP with the external references. This method is called a...
Definition ocp.hpp:123
double m_p
Definition ocp.hpp:57
static constexpr int nc
Dimension of the equality constraints.
Definition ocp.hpp:39
double g
Definition ocp.hpp:59
double m_c
Definition ocp.hpp:56
std::array< double, 1 > r
Definition ocp.hpp:64
std::array< double, 4 > q_terminal
Definition ocp.hpp:62
void eval_hu(const double t, const double *x, const double *u, const double *lmd, double *hu) const
Computes the partial derivative of the Hamiltonian with respect to control input and the equality con...
Definition ocp.hpp:216
std::array< double, nub > dummy_weight
Definition ocp.hpp:69
static constexpr int nx
Dimension of the state.
Definition ocp.hpp:29
#define CGMRES_EIGEN_CONST_CAST(TYPE, OBJ)
Definition macros.hpp:7
Definition continuation_gmres.hpp:11
Eigen::Map< MatrixType > Map
Alias of Eigen::Map.
Definition types.hpp:54
External reference of the cart pole.
Definition ocp.hpp:107
double cart_position
Position of the cart. Default is 0.
Definition ocp.hpp:111