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};
63 std::array<double, 4>
x_ref = {0, M_PI, 0, 0};
64 std::array<double, 1>
r = {1};
67 std::array<double, nub>
umin = {-15.0};
68 std::array<double, nub>
umax = {15.0};
71 void disp(std::ostream& os)
const {
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]);
141 const double x1 = 1.0/(
m_c +
m_p*pow(x0, 2));
142 const double x2 = cos(x[1]);
143 const double x3 =
l*pow(x[1], 2);
144 const double x4 =
m_p*x0;
147 dx[2] = x1*(u[0] + x4*(
g*x2 + x3));
148 dx[3] = x1*(-
g*x0*(
m_c +
m_p) - u[0]*x2 - x2*x3*x4)/
l;
161 void eval_phix(
const double t,
const double* x,
double* phix)
const {
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;
188 const double x6 =
m_p*(x3 + x5);
189 const double x7 = pow(x1, 2);
190 const double x8 =
m_c +
m_p*x7;
191 const double x9 =
m_p*x1;
192 const double x10 = x2*x9;
193 const double x11 = 2*x10/pow(x8, 2);
194 const double x12 = 1.0/x8;
195 const double x13 =
g*x1;
196 const double x14 =
m_c +
m_p;
197 const double x15 = lmd[3]/
l;
198 hx[0] = (1.0/2.0)*
q[0]*(2*x[0] - 2*
x_ref[0]);
199 hx[1] = -lmd[2]*x11*(u[0] + x1*x6) + lmd[2]*x12*(x2*x6 + x9*(2*
l*x[1] - x13)) + (1.0/2.0)*
q[1]*(x0 - 2*
x_ref[1]) - x11*x15*(-u[0]*x2 - x10*x5 - x13*x14) + x12*x15*(
l*
m_p*x4*x7 -
l*x0*x10 -
m_p*pow(x2, 2)*x5 + u[0]*x1 - x14*x3);
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 {
218 const double x0 = 1.0/(
m_c +
m_p*pow(sin(x[1]), 2));
219 hu[0] = lmd[2]*x0 +
r[0]*u[0] - lmd[3]*x0*cos(x[1])/
l;
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));
259 if (phix.size() !=
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:50
Eigen::MatrixBase< MatrixType > MatrixBase
Alias of Eigen::MatrixBase.
Definition: types.hpp:29
External reference of the cart pole.
Definition: ocp.hpp:107
double cart_position
Position of the cart. Default is 0.
Definition: ocp.hpp:111