autogenu-jupyter
An automatic code generator and the continuation/GMRES (C/GMRES) based numerical solvers for nonlinear MPC
Loading...
Searching...
No Matches
ocp.hpp
Go to the documentation of this file.
1// This file was automatically generated by autogenu-jupyter (https://github.com/mayataka/autogenu-jupyter).
2// The autogenu-jupyter copyright holders make no ownership claim of its contents.
3
4#ifndef CGMRES__OCP_CARTPOLE_HPP_
5#define CGMRES__OCP_CARTPOLE_HPP_
6
7#define _USE_MATH_DEFINES
8
9#include <cmath>
10#include <array>
11#include <iostream>
12
13#include "cgmres/types.hpp"
15
16namespace cgmres {
17
23public:
24
28 static constexpr int nx = 4;
29
33 static constexpr int nu = 1;
34
38 static constexpr int nc = 0;
39
43 static constexpr int nh = 0;
44
48 static constexpr int nuc = nu + nc;
49
53 static constexpr int nub = 1;
54
55 double m_c = 2;
56 double m_p = 0.2;
57 double l = 0.5;
58 double g = 9.80665;
59
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};
64
65 static constexpr std::array<int, nub> ubound_indices = {0};
66 std::array<double, nub> umin = {-15.0};
67 std::array<double, nub> umax = {15.0};
68 std::array<double, nub> dummy_weight = {0.1};
69
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;
78 os << 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;
83 os << std::endl;
84 Eigen::IOFormat fmt(4, 0, ", ", "", "[", "]");
85 Eigen::IOFormat intfmt(1, 0, ", ", "", "[", "]");
86 os << " q: " << Map<const VectorX>(q.data(), q.size()).transpose().format(fmt) << std::endl;
87 os << " q_terminal: " << Map<const VectorX>(q_terminal.data(), q_terminal.size()).transpose().format(fmt) << std::endl;
88 os << " x_ref: " << Map<const VectorX>(x_ref.data(), x_ref.size()).transpose().format(fmt) << std::endl;
89 os << " r: " << Map<const VectorX>(r.data(), r.size()).transpose().format(fmt) << std::endl;
90 os << std::endl;
91 os << " ubound_indices: " << Map<const VectorXi>(ubound_indices.data(), ubound_indices.size()).transpose().format(intfmt) << std::endl;
92 os << " umin: " << Map<const VectorX>(umin.data(), umin.size()).transpose().format(fmt) << std::endl;
93 os << " umax: " << Map<const VectorX>(umax.data(), umax.size()).transpose().format(fmt) << std::endl;
94 os << " dummy_weight: " << Map<const VectorX>(dummy_weight.data(), dummy_weight.size()).transpose().format(fmt) << std::endl;
95 }
96
97 friend std::ostream& operator<<(std::ostream& os, const OCP_cartpole& ocp) {
98 ocp.disp(os);
99 return os;
100 }
101
102
107 void synchronize() {
108 }
109
119 void eval_f(const double t, const double* x, const double* u,
120 double* dx) const {
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;
126 dx[0] = x[2];
127 dx[1] = x[3];
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;
130
131 }
132
142 void eval_phix(const double t, const double* x, double* phix) const {
143 phix[0] = (1.0/2.0)*q_terminal[0]*(2*x[0] - 2*x_ref[0]);
144 phix[1] = (1.0/2.0)*q_terminal[1]*(2*x[1] - 2*x_ref[1]);
145 phix[2] = (1.0/2.0)*q_terminal[2]*(2*x[2] - 2*x_ref[2]);
146 phix[3] = (1.0/2.0)*q_terminal[3]*(2*x[3] - 2*x_ref[3]);
147
148 }
149
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]);
183
184 }
185
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;
201
202 }
203
211 template <typename VectorType1, typename VectorType2, typename VectorType3>
212 void eval_f(const double t, const MatrixBase<VectorType1>& x,
213 const MatrixBase<VectorType2>& u,
214 const MatrixBase<VectorType3>& dx) const {
215 if (x.size() != nx) {
216 throw std::invalid_argument("[OCP]: x.size() must be " + std::to_string(nx));
217 }
218 if (u.size() != nu) {
219 throw std::invalid_argument("[OCP]: u.size() must be " + std::to_string(nu));
220 }
221 if (dx.size() != nx) {
222 throw std::invalid_argument("[OCP]: dx.size() must be " + std::to_string(nx));
223 }
224 eval_f(t, x.derived().data(), u.derived().data(), CGMRES_EIGEN_CONST_CAST(VectorType3, dx).data());
225 }
226
234 template <typename VectorType1, typename VectorType2>
235 void eval_phix(const double t, const MatrixBase<VectorType1>& x,
236 const MatrixBase<VectorType2>& phix) const {
237 if (x.size() != nx) {
238 throw std::invalid_argument("[OCP]: x.size() must be " + std::to_string(nx));
239 }
240 if (phix.size() != nx) {
241 throw std::invalid_argument("[OCP]: phix.size() must be " + std::to_string(nx));
242 }
243 eval_phix(t, x.derived().data(), CGMRES_EIGEN_CONST_CAST(VectorType2, phix).data());
244 }
245
255 template <typename VectorType1, typename VectorType2, typename VectorType3, typename VectorType4>
256 void eval_hx(const double t, const MatrixBase<VectorType1>& x,
257 const MatrixBase<VectorType2>& uc,
258 const MatrixBase<VectorType3>& lmd,
259 const MatrixBase<VectorType4>& hx) const {
260 if (x.size() != nx) {
261 throw std::invalid_argument("[OCP]: x.size() must be " + std::to_string(nx));
262 }
263 if (uc.size() != nuc) {
264 throw std::invalid_argument("[OCP]: uc.size() must be " + std::to_string(nuc));
265 }
266 if (lmd.size() != nx) {
267 throw std::invalid_argument("[OCP]: lmd.size() must be " + std::to_string(nx));
268 }
269 if (hx.size() != nuc) {
270 throw std::invalid_argument("[OCP]: hx.size() must be " + std::to_string(nx));
271 }
272 eval_hx(t, x.derived().data(), uc.derived().data(), lmd.derived().data(), CGMRES_EIGEN_CONST_CAST(VectorType4, hx).data());
273 }
274
284 template <typename VectorType1, typename VectorType2, typename VectorType3, typename VectorType4>
285 void eval_hu(const double t, const MatrixBase<VectorType1>& x,
286 const MatrixBase<VectorType2>& uc,
287 const MatrixBase<VectorType3>& lmd,
288 const MatrixBase<VectorType4>& hu) const {
289 if (x.size() != nx) {
290 throw std::invalid_argument("[OCP]: x.size() must be " + std::to_string(nx));
291 }
292 if (uc.size() != nuc) {
293 throw std::invalid_argument("[OCP]: uc.size() must be " + std::to_string(nuc));
294 }
295 if (lmd.size() != nx) {
296 throw std::invalid_argument("[OCP]: lmd.size() must be " + std::to_string(nx));
297 }
298 if (hu.size() != nuc) {
299 throw std::invalid_argument("[OCP]: hu.size() must be " + std::to_string(nuc));
300 }
301 eval_hu(t, x.derived().data(), uc.derived().data(), lmd.derived().data(), CGMRES_EIGEN_CONST_CAST(VectorType4, hu).data());
302 }
303
304};
305
306} // namespace cgmres
307
308#endif // CGMRES_OCP_HPP_
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