autogenu-jupyter
An automatic code generator and the continuation/GMRES (C/GMRES) based numerical solvers for nonlinear MPC
Loading...
Searching...
No Matches
Python examples

Here we show how to use Python interfaces. First, run cartpole.ipynb, hexacopter.ipynb, mobilerobot.ipynb, or pendubot.ipynb in the project root directory and install Python interfaces. Second, set PYTHONPATH according to the generated messages in the notebooks. Then you can run examples, e.g., via

python3 cartpole.py

Documentation

Python API is very similar to the C++ API.
Please refere to the C++ API documentation https://mayataka.github.io/autogenu-jupyter/ as well as the following tips of the API conversions between C++ and Python.

Tips of API conversions between C++ and Python

Horizon

C++ Horizon documentation

C++ Python
Include #include "cgmres/horizon.hpp" import cgmres.common
Constructor const double Tf = ...;
const double alpha = ...;
cgmres::Horizon horizon(Tf, alpha);
horizon = cgmres.common.Horizon(Tf=..., alpha=...)
Shallow copy auto& other = horizon; other = horizon
Deep copy auto other = horizon; other = horizon.clone()
Methods const double t = ...;
const double T = horizon.T(t);
T = horizon.T(t=...)
Print out std::cout << horizon << std::endl; print(horizon)

SolverSettings

C++ SolverSettings documentation

C++ Python
Include #include "cgmres/solver_settings.hpp" import cgmres.common
Constructor cgmres::SolverSettings settings; settings = cgmres.common.SolverSettings()
Shallow copy auto& other = settings; other = settings
Deep copy auto other = settings; other = settings.clone()
Scalar member variables settings.max_iter = ...; settings.max_iter = ...
Print out std::cout << settings << std::endl; print(settings)

OCP (OCP_cartpole as an example)

C++ OCP_cartpole documentation

C++ Python
Include #include "ocp.hpp" import cgmres.cartpole
Constructor cgmres::OCP_cartpole ocp; ocp = cgmres.cartpole.OCP()
Shallow copy auto& other = ocp; other = ocp
Deep copy auto other = ocp; other = ocp.clone()
Scalar member variables ocp.g = 9.80665; ocp.g = 9.80665
Array member variables ocp.q = std::array<double, 4>({2.5, 10, 0.1, 0.1}); ocp.q = np.array([2.5, 10, 0.1, 0.1])
Static member variables const int nx = cgmres::OCP_cartpole::nx; nx = cgmres.cartpole.OCP.nx
Member functions const double t = ...;
const cgmres::VectorX x = ...;
const cgmres::VectorX u = ...;
cgmres::VectorX dx = ...;
ocp.eval_f(t, x, u, dx);
t = ...
x = np.array([...])
u = np.array([...])
dx = ocp.eval_f(t, x, u)
Print out std::cout << ocp << std::endl; print(ocp)

ZeroHorizonOCPSolver (OCP_cartpole as an example)

C++ ZeroHorizonOCPSolver documentation

C++ Python
Include #include "cgmres/zero_horizon_ocp_solver.hpp"
#include "ocp.hpp"
import cgmres.common
import cgmres.cartpole
Constructor cgmres::OCP_cartpole ocp;
constexpr int kmax = ...;
cgmres::SolverSettings settings;
settings.max_iter = ...;
cgmres::ZeroHorizonOCPSolver<cgmres::OCP_cartpole, kmax> solver(ocp, settings);
ocp = cgmres.cartpole.OCP()
settins = cgmres.common.SolverSettings()
settings.max_iter = ...
solver = cgmres.cartpole.ZeroHorizonOCPSolver(ocp, settings)
Shallow copy auto& other = solver; other = solver
Deep copy auto other = solver; other = solver.clone()
Member functions const double t = ...;
const cgmres::VectorX x = ...;
solver.solve(t, x);
t = ...
x = np.array([...])
solver.solve(t, x)
Setter functions const cgmres::VectorX u = ...;
solver.set_u(u);
u = np.array([...])
solver.set_u(u)
Getter functions const auto& uopt = solver.uopt(); uopt = solver.uopt
Print out std::cout << solver << std::endl; print(solver)

MultipleShootingCGMRESSolver (OCP_cartpole as an example)

C++ MultipleShootingCGMRESSolver documentation

C++ Python
Include #include "cgmres/multiple_shooting_cgmres_solver.hpp"
#include "ocp.hpp"
import cgmres.common
import cgmres.cartpole
Constructor cgmres::OCP_cartpole ocp;
constexpr int N = ...;
constexpr int kmax = ...;
cgmres::SolverSettings settings;
settings.sampling_time = ...;
cgmres::Horizon horizon(...);
cgmres::MultipleShootingCGMRESSolver<cgmres::OCP_cartpole, N, kmax> mpc(ocp, horizon, settings);
ocp = cgmres.cartpole.OCP()
settings = cgmres.common.SolverSettings()
settings.sampling_time = ...
horizon = cgmres.common.Horizon(Tf=..., alpha=...)
mpc = cgmres.cartpole.MultipleShootingCGMRESSolver(ocp, horizon, settings)
Shallow copy auto& other = mpc; other = mpc
Deep copy auto other = mpc; other = mpc.clone()
Member functions const double t = ...;
const cgmres::VectorX x = ...;
mpc.update(t, x);
t = ...
x = np.array([...])
mpc.update(t, x)
Setter functions const cgmres::VectorX u = ...;
mpc.set_u(u);
u = np.array([...])
mpc.set_u(u)
Getter functions const auto& uopt0 = solver.uopt()[0]; uopt0 = solver.uopt[0]
Print out std::cout << mpc << std::endl; print(mpc)