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_EXTERNAL_REFERENCE_HPP_
5#define CGMRES__OCP_CARTPOLE_EXTERNAL_REFERENCE_HPP_
6
7#define _USE_MATH_DEFINES
8
9#include <cmath>
10#include <array>
11#include <iostream>
12#include <memory>
13
14#include "cgmres/types.hpp"
16
17namespace cgmres {
18
24public:
25
29 static constexpr int nx = 4;
30
34 static constexpr int nu = 1;
35
39 static constexpr int nc = 0;
40
44 static constexpr int nh = 0;
45
49 static constexpr int nuc = nu + nc;
50
54 static constexpr int nub = 1;
55
56 double m_c = 2;
57 double m_p = 0.2;
58 double l = 0.5;
59 double g = 9.80665;
60
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};
65
66 static constexpr std::array<int, nub> ubound_indices = {0};
67 std::array<double, nub> umin = {-15.0};
68 std::array<double, nub> umax = {15.0};
69 std::array<double, nub> dummy_weight = {0.1};
70
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;
79 os << 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;
84 os << std::endl;
85 Eigen::IOFormat fmt(4, 0, ", ", "", "[", "]");
86 Eigen::IOFormat intfmt(1, 0, ", ", "", "[", "]");
87 os << " q: " << Map<const VectorX>(q.data(), q.size()).transpose().format(fmt) << std::endl;
88 os << " q_terminal: " << Map<const VectorX>(q_terminal.data(), q_terminal.size()).transpose().format(fmt) << std::endl;
89 os << " x_ref: " << Map<const VectorX>(x_ref.data(), x_ref.size()).transpose().format(fmt) << std::endl;
90 os << " r: " << Map<const VectorX>(r.data(), r.size()).transpose().format(fmt) << std::endl;
91 os << std::endl;
92 os << " ubound_indices: " << Map<const VectorXi>(ubound_indices.data(), ubound_indices.size()).transpose().format(intfmt) << std::endl;
93 os << " umin: " << Map<const VectorX>(umin.data(), umin.size()).transpose().format(fmt) << std::endl;
94 os << " umax: " << Map<const VectorX>(umax.data(), umax.size()).transpose().format(fmt) << std::endl;
95 os << " dummy_weight: " << Map<const VectorX>(dummy_weight.data(), dummy_weight.size()).transpose().format(fmt) << std::endl;
96 }
97
98 friend std::ostream& operator<<(std::ostream& os, const OCP_cartpoleExternalReference& ocp) {
99 ocp.disp(os);
100 return os;
101 }
102
111 double cart_position = 0.0;
112 };
113
117 std::shared_ptr<ExternalReference> external_reference = nullptr;
118
123 void synchronize() {
124 if (external_reference != nullptr) {
125 x_ref[0] = external_reference->cart_position;
126 }
127 }
128
138 void eval_f(const double t, const double* x, const double* u,
139 double* dx) const {
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;
145 dx[0] = x[2];
146 dx[1] = x[3];
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;
149
150 }
151
161 void eval_phix(const double t, const double* x, double* phix) const {
162 phix[0] = (1.0/2.0)*q_terminal[0]*(2*x[0] - 2*x_ref[0]);
163 phix[1] = (1.0/2.0)*q_terminal[1]*(2*x[1] - 2*x_ref[1]);
164 phix[2] = (1.0/2.0)*q_terminal[2]*(2*x[2] - 2*x_ref[2]);
165 phix[3] = (1.0/2.0)*q_terminal[3]*(2*x[3] - 2*x_ref[3]);
166
167 }
168
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]);
202
203 }
204
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;
220
221 }
222
230 template <typename VectorType1, typename VectorType2, typename VectorType3>
231 void eval_f(const double t, const MatrixBase<VectorType1>& x,
232 const MatrixBase<VectorType2>& u,
233 const MatrixBase<VectorType3>& dx) const {
234 if (x.size() != nx) {
235 throw std::invalid_argument("[OCP]: x.size() must be " + std::to_string(nx));
236 }
237 if (u.size() != nu) {
238 throw std::invalid_argument("[OCP]: u.size() must be " + std::to_string(nu));
239 }
240 if (dx.size() != nx) {
241 throw std::invalid_argument("[OCP]: dx.size() must be " + std::to_string(nx));
242 }
243 eval_f(t, x.derived().data(), u.derived().data(), CGMRES_EIGEN_CONST_CAST(VectorType3, dx).data());
244 }
245
253 template <typename VectorType1, typename VectorType2>
254 void eval_phix(const double t, const MatrixBase<VectorType1>& x,
255 const MatrixBase<VectorType2>& phix) const {
256 if (x.size() != nx) {
257 throw std::invalid_argument("[OCP]: x.size() must be " + std::to_string(nx));
258 }
259 if (phix.size() != nx) {
260 throw std::invalid_argument("[OCP]: phix.size() must be " + std::to_string(nx));
261 }
262 eval_phix(t, x.derived().data(), CGMRES_EIGEN_CONST_CAST(VectorType2, phix).data());
263 }
264
274 template <typename VectorType1, typename VectorType2, typename VectorType3, typename VectorType4>
275 void eval_hx(const double t, const MatrixBase<VectorType1>& x,
276 const MatrixBase<VectorType2>& uc,
277 const MatrixBase<VectorType3>& lmd,
278 const MatrixBase<VectorType4>& hx) const {
279 if (x.size() != nx) {
280 throw std::invalid_argument("[OCP]: x.size() must be " + std::to_string(nx));
281 }
282 if (uc.size() != nuc) {
283 throw std::invalid_argument("[OCP]: uc.size() must be " + std::to_string(nuc));
284 }
285 if (lmd.size() != nx) {
286 throw std::invalid_argument("[OCP]: lmd.size() must be " + std::to_string(nx));
287 }
288 if (hx.size() != nuc) {
289 throw std::invalid_argument("[OCP]: hx.size() must be " + std::to_string(nx));
290 }
291 eval_hx(t, x.derived().data(), uc.derived().data(), lmd.derived().data(), CGMRES_EIGEN_CONST_CAST(VectorType4, hx).data());
292 }
293
303 template <typename VectorType1, typename VectorType2, typename VectorType3, typename VectorType4>
304 void eval_hu(const double t, const MatrixBase<VectorType1>& x,
305 const MatrixBase<VectorType2>& uc,
306 const MatrixBase<VectorType3>& lmd,
307 const MatrixBase<VectorType4>& hu) const {
308 if (x.size() != nx) {
309 throw std::invalid_argument("[OCP]: x.size() must be " + std::to_string(nx));
310 }
311 if (uc.size() != nuc) {
312 throw std::invalid_argument("[OCP]: uc.size() must be " + std::to_string(nuc));
313 }
314 if (lmd.size() != nx) {
315 throw std::invalid_argument("[OCP]: lmd.size() must be " + std::to_string(nx));
316 }
317 if (hu.size() != nuc) {
318 throw std::invalid_argument("[OCP]: hu.size() must be " + std::to_string(nuc));
319 }
320 eval_hu(t, x.derived().data(), uc.derived().data(), lmd.derived().data(), CGMRES_EIGEN_CONST_CAST(VectorType4, hu).data());
321 }
322
323};
324
325} // namespace cgmres
326
327#endif // CGMRES_OCP_HPP_
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