4#ifndef CGMRES__OCP_CARTPOLE_HPP_
5#define CGMRES__OCP_CARTPOLE_HPP_
7#define _USE_MATH_DEFINES
28 static constexpr int nx = 4;
33 static constexpr int nu = 1;
38 static constexpr int nc = 0;
43 static constexpr int nh = 0;
53 static constexpr int nub = 1;
60 std::array<double, 4>
q = {2.5, 10, 0.01, 0.01};
61 std::array<double, 4>
q_terminal = {2.5, 10, 0.01, 0.01};
62 std::array<double, 4>
x_ref = {0, M_PI, 0, 0};
63 std::array<double, 1>
r = {1};
66 std::array<double, nub>
umin = {-15.0};
67 std::array<double, nub>
umax = {15.0};
70 void disp(std::ostream& os)
const {
71 os <<
"OCP_cartpole:" << std::endl;
72 os <<
" nx: " <<
nx << std::endl;
73 os <<
" nu: " <<
nu << std::endl;
74 os <<
" nc: " <<
nc << std::endl;
75 os <<
" nh: " <<
nh << std::endl;
76 os <<
" nuc: " <<
nuc << std::endl;
77 os <<
" nub: " <<
nub << std::endl;
79 os <<
" m_c: " <<
m_c << std::endl;
80 os <<
" m_p: " <<
m_p << std::endl;
81 os <<
" l: " <<
l << std::endl;
82 os <<
" g: " <<
g << std::endl;
84 Eigen::IOFormat fmt(4, 0,
", ",
"",
"[",
"]");
85 Eigen::IOFormat intfmt(1, 0,
", ",
"",
"[",
"]");
119 void eval_f(
const double t,
const double* x,
const double* u,
121 const double x0 = sin(x[1]);
122 const double x1 = 1.0/(
m_c +
m_p*pow(x0, 2));
123 const double x2 = cos(x[1]);
124 const double x3 =
l*pow(x[1], 2);
125 const double x4 =
m_p*x0;
128 dx[2] = x1*(u[0] + x4*(
g*x2 + x3));
129 dx[3] = x1*(-
g*x0*(
m_c +
m_p) - u[0]*x2 - x2*x3*x4)/
l;
142 void eval_phix(
const double t,
const double* x,
double* phix)
const {
161 void eval_hx(
const double t,
const double* x,
const double* u,
162 const double* lmd,
double* hx)
const {
163 const double x0 = 2*x[1];
164 const double x1 = sin(x[1]);
165 const double x2 = cos(x[1]);
166 const double x3 =
g*x2;
167 const double x4 = pow(x[1], 2);
168 const double x5 =
l*x4;
169 const double x6 =
m_p*(x3 + x5);
170 const double x7 = pow(x1, 2);
171 const double x8 =
m_c +
m_p*x7;
172 const double x9 =
m_p*x1;
173 const double x10 = x2*x9;
174 const double x11 = 2*x10/pow(x8, 2);
175 const double x12 = 1.0/x8;
176 const double x13 =
g*x1;
177 const double x14 =
m_c +
m_p;
178 const double x15 = lmd[3]/
l;
179 hx[0] = (1.0/2.0)*
q[0]*(2*x[0] - 2*
x_ref[0]);
180 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);
181 hx[2] = lmd[0] + (1.0/2.0)*
q[2]*(2*x[2] - 2*
x_ref[2]);
182 hx[3] = lmd[1] + (1.0/2.0)*
q[3]*(2*x[3] - 2*
x_ref[3]);
197 void eval_hu(
const double t,
const double* x,
const double* u,
198 const double* lmd,
double* hu)
const {
199 const double x0 = 1.0/(
m_c +
m_p*pow(sin(x[1]), 2));
200 hu[0] = lmd[2]*x0 +
r[0]*u[0] - lmd[3]*x0*cos(x[1])/
l;
211 template <
typename VectorType1,
typename VectorType2,
typename VectorType3>
215 if (x.size() !=
nx) {
216 throw std::invalid_argument(
"[OCP]: x.size() must be " + std::to_string(
nx));
218 if (u.size() !=
nu) {
219 throw std::invalid_argument(
"[OCP]: u.size() must be " + std::to_string(
nu));
221 if (dx.size() !=
nx) {
222 throw std::invalid_argument(
"[OCP]: dx.size() must be " + std::to_string(
nx));
234 template <
typename VectorType1,
typename VectorType2>
237 if (x.size() !=
nx) {
238 throw std::invalid_argument(
"[OCP]: x.size() must be " + std::to_string(
nx));
240 if (phix.size() !=
nx) {
241 throw std::invalid_argument(
"[OCP]: phix.size() must be " + std::to_string(
nx));
255 template <
typename VectorType1,
typename VectorType2,
typename VectorType3,
typename VectorType4>
260 if (x.size() !=
nx) {
261 throw std::invalid_argument(
"[OCP]: x.size() must be " + std::to_string(
nx));
263 if (uc.size() !=
nuc) {
264 throw std::invalid_argument(
"[OCP]: uc.size() must be " + std::to_string(
nuc));
266 if (lmd.size() !=
nx) {
267 throw std::invalid_argument(
"[OCP]: lmd.size() must be " + std::to_string(
nx));
269 if (hx.size() !=
nuc) {
270 throw std::invalid_argument(
"[OCP]: hx.size() must be " + std::to_string(
nx));
284 template <
typename VectorType1,
typename VectorType2,
typename VectorType3,
typename VectorType4>
289 if (x.size() !=
nx) {
290 throw std::invalid_argument(
"[OCP]: x.size() must be " + std::to_string(
nx));
292 if (uc.size() !=
nuc) {
293 throw std::invalid_argument(
"[OCP]: uc.size() must be " + std::to_string(
nuc));
295 if (lmd.size() !=
nx) {
296 throw std::invalid_argument(
"[OCP]: lmd.size() must be " + std::to_string(
nx));
298 if (hu.size() !=
nuc) {
299 throw std::invalid_argument(
"[OCP]: hu.size() must be " + std::to_string(
nuc));
Definition of the optimal control problem (OCP) of cartpole.
Definition: ocp.hpp:22
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:285
static constexpr int nh
Dimension of the Fischer-Burmeister function (already counded in nc).
Definition: ocp.hpp:43
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:256
double l
Definition: ocp.hpp:57
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:142
static constexpr int nub
Dimension of the bound constraints on the control input.
Definition: ocp.hpp:53
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:212
std::array< double, 1 > r
Definition: ocp.hpp:63
double m_p
Definition: ocp.hpp:56
static constexpr int nuc
Dimension of the concatenation of the control input and equality constraints.
Definition: ocp.hpp:48
static constexpr int nc
Dimension of the equality constraints.
Definition: ocp.hpp:38
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:161
std::array< double, nub > umin
Definition: ocp.hpp:66
static constexpr int nx
Dimension of the state.
Definition: ocp.hpp:28
double m_c
Definition: ocp.hpp:55
void disp(std::ostream &os) const
Definition: ocp.hpp:70
std::array< double, nub > dummy_weight
Definition: ocp.hpp:68
void synchronize()
Synchrozies the internal parameters of this OCP with the external references. This method is called a...
Definition: ocp.hpp:107
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:197
static constexpr int nu
Dimension of the control input.
Definition: ocp.hpp:33
friend std::ostream & operator<<(std::ostream &os, const OCP_cartpole &ocp)
Definition: ocp.hpp:97
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:119
double g
Definition: ocp.hpp:58
static constexpr std::array< int, nub > ubound_indices
Definition: ocp.hpp:65
std::array< double, 4 > x_ref
Definition: ocp.hpp:62
std::array< double, nub > umax
Definition: ocp.hpp:67
std::array< double, 4 > q_terminal
Definition: ocp.hpp:61
std::array< double, 4 > q
Definition: ocp.hpp:60
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:235
#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