Trajectory optimization
The objective of this library is to model and solve optimal control problems (OCPs) of the form
\begin{align} \min_{x,u}~& \int_0^T \ell(x, u)\, dt + \ell_\mathrm{f}(x(T)) \\ \subjectto & \dot x (t) = f(x(t), u(t)) \\ & g(x(t), u(t)) = 0 \\ & h(x(t), u(t)) \leq 0 \end{align}
Transcription
A transcription translates the continuous-time OCP to a discrete-time, finite-dimensional nonlinear program. Aligator allows us to consider transcriptions with implicit discrete dynamics: \begin{aligned} \min_{\bmx,\bmu}~& J(\bmx, \bmu) = \sum_{i=0}^{N-1} \ell_i(x_i, u_i) + \ell_N(x_N) \\ \subjectto & f(x_i, u_i, x_{i+1}) = 0 \\ & g(x_i, u_i) = 0 \\ & h(x_i, u_i) \leq 0 \end{aligned}
In aligator, trajectory optimization problems are described using the class TrajOptProblemTpl. Each TrajOptProblemTpl is described by a succession of stages (StageModelTpl) which encompass the set of constraints and the cost function (class CostAbstractTpl) for this stage.
Additionally, a TrajOptProblemTpl must provide an initial condition \( x_0
= \bar{x} \), a terminal cost $$ \ell_{\mathrm{f}}(x_N) $$ on the terminal state \(x_N \); optionally, a terminal constraint \(g(x_N) = 0, h(x_N) \leq 0 \) on this state may be added.
Stage models
A stage model (StageModelTpl) describes a node in the discrete-time optimal control problem: it consists in a running cost function, and a vector of constraints (StageConstraintTpl), the first of which must describe system dynamics (through a DynamicsModelTpl).
Example
Define and solve an LQR (Python API):
7"""Formulating and solving a linear quadratic regulator with Aligator."""
9import matplotlib.pyplot
as plt
14from aligator
import constraints, dynamics, manifolds
15from aligator.utils.plotting
import plot_convergence
19 term_cstr: bool =
False
23args = Args().parse_args()
28space = manifolds.VectorSpace(nx)
29x0 = space.neutral() + (0.2, 0.3, -0.1)
43N = 1e-5 * np.eye(nx, nu)
52rcost0 = aligator.QuadraticCost(Q, R, N)
53term_cost = aligator.QuadraticCost(Qf, R)
54dynmodel = dynamics.LinearDiscreteDynamics(A, B, c)
55stage = aligator.StageModel(rcost0, dynmodel)
57 u_min = -0.18 * np.ones(nu)
58 u_max = +0.18 * np.ones(nu)
59 ctrl_fn = aligator.ControlErrorResidual(nx, np.zeros(nu))
60 stage.addConstraint(ctrl_fn, constraints.BoxConstraint(u_min, u_max))
65problem = aligator.TrajOptProblem(x0, nu, space, term_cost)
67for i
in range(nsteps):
68 problem.addStage(stage)
70xtar2 = 0.1 * np.ones(nx)
72 term_fun = aligator.StateErrorResidual(space, nu, xtar2)
73 problem.addTerminalConstraint(term_fun, constraints.EqualityConstraintSet())
76mu_init = 1e-1
if args.bounds
else 1e-4
77verbose = aligator.VerboseLevel.VERBOSE
79solver = aligator.SolverProxDDP(tol, mu_init, verbose=verbose)
81his_cb = aligator.HistoryCallback()
82solver.registerCallback(
"his", his_cb)
83print(
"Registered callbacks:", solver.getCallbackNames().tolist())
85solver.rollout_type = aligator.ROLLOUT_LINEAR
90prob_data = aligator.TrajOptData(problem)
91problem.evaluate(xs_i, us_i, prob_data)
94solver.run(problem, xs_i, us_i)
100fig1: plt.Figure = plt.gcf()
102lstyle = {
"lw": 0.9,
"marker":
".",
"markersize": 5}
103trange = np.arange(nsteps + 1)
104plt.plot(res.xs, ls=
"-", **lstyle)
114 label=
r"$x_\mathrm{tar}$",
125plt.title(
"State trajectory $x(t)$")
126plt.xlabel(
"Time $i$")
127plt.legend(frameon=
False)
130plt.plot(res.us, **lstyle)
133 np.concatenate([u_min, u_max]),
141plt.xlabel(
"Time $i$")
142plt.title(
"Controls $u(t)$")
143plt.legend(frameon=
False, loc=
"lower right")
147fig2: plt.Figure = plt.figure()
148ax: plt.Axes = fig2.add_subplot()
158plot_convergence(his_cb, ax, res)
159ax.set_title(
"Convergence (constrained LQR)")
162 "Tolerance $\\epsilon_\\mathrm{tol}$",
math_types< Scalar >::VectorOfVectors rollout(const std::vector< xyz::polymorphic< DynamicsModelTpl< Scalar > > > &dyn_models, const typename math_types< Scalar >::VectorXs &x0, const typename math_types< Scalar >::VectorOfVectors &us, typename math_types< Scalar >::VectorOfVectors &xout)
Perform a rollout of the supplied dynamical models.