Linear time-variant model predictive control in Python.
Project description
ltv-mpc
Installation | Usage | Example | Areas of improvement | Alternatives
Linear time-variant (LTV) model predictive control in Python. Solve a quadratic program of the form:
This module is designed for prototyping. If you need performance, check out the alternatives below.
📢 2022-08: the brand new mpc_interface handles more general cost functions.
Installation
pip install ltv-mpc
Usage
This module defines a one-stop shop function:
solve_mpc(problem: Problem, solver: str) -> Solution
The Problem
type defines the model predictive control problem (LTV system, LTV constraints, initial state and cost function to optimize) while the Solution
holds the resulting state and input trajectories.
Example
Let us define a triple integrator:
import numpy as np
horizon_duration = 1.0 # [s]
N = 16 # number of discretization steps
T = horizon_duration / N
A = np.array([[1.0, T, T ** 2 / 2.0], [0.0, 1.0, T], [0.0, 0.0, 1.0]])
B = np.array([T ** 3 / 6.0, T ** 2 / 2.0, T]).reshape((3, 1))
Suppose for the sake of example that acceleration is the main constraint acting on our system. We thus define an acceleration constraint |acceleration| <= max_accel
:
max_accel = 3.0 # [m] / [s] / [s]
accel_from_state = np.array([0.0, 0.0, 1.0])
C = np.vstack([+accel_from_state, -accel_from_state])
e = np.array([+max_accel, +max_accel])
This leads us to the following linear MPC problem:
from ltv_mpc import Problem
x_init = np.array([0.0, 0.0, 0.0])
x_goal = np.array([1.0, 0.0, 0.0])
problem = Problem(
transition_state_matrix=A,
transition_input_matrix=B,
ineq_state_matrix=C,
ineq_input_matrix=None,
ineq_vector=e,
initial_state=x_init,
goal_state=x_goal,
nb_timesteps=N,
terminal_cost_weight=1.0,
stage_state_cost_weight=None,
stage_input_cost_weight=1e-6,
)
We can solve it with:
from ltv_mpc import solve_mpc
solution = solve_mpc(problem, solver="quadprog")
The solution holds complete state and input trajectories as stacked vectors. For instance, we can plot positions, velocities and accelerations as follows:
import pylab
t = np.linspace(0.0, horizon_duration, N + 1)
X = solution.stacked_states
positions, velocities, accelerations = X[:, 0], X[:, 1], X[:, 2]
pylab.ion()
pylab.plot(t, positions)
pylab.plot(t, velocities)
pylab.plot(t, accelerations)
pylab.grid(True)
pylab.legend(("position", "velocity", "acceleration"))
This example produces the following trajectory:
The behavior is a weighted compromise between reaching the goal state (weight 1.0
) and keeping reasonable finite jerk inputs (weight 1e-6
). The latter mitigate bang-bang accelerations but prevent fully reaching the goal within the horizon. See the examples folder for more examples.
Areas of improvement
This module is incomplete with regards to the following points:
- Cost functions: can be extended to general linear stage cost functions
- Documentation: there are some undocumented functions
- Test coverage: only one end-to-end test
New contributions are welcome :)
Alternatives
This module is designed for prototyping. If you need performance, check out one of the following libraries, and open a PR if you know other relevant ones:
Library | System | Language | License |
---|---|---|---|
Copra (original) | Linear time-invariant | C++ | BSD-2-Clause |
Copra (fork) | Linear time-variant | C++ | BSD-2-Clause |
mpc_interface | Linear time-variant | C++/Python | BSD-2-Clause |
Crocoddyl | Nonlinear | C++ | BSD-3-Clause |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ltv-mpc-1.0.0.tar.gz
.
File metadata
- Download URL: ltv-mpc-1.0.0.tar.gz
- Upload date:
- Size: 33.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c3eadf3c794e2d8c38a445a9109d026a2c121aa6e2a1f49774f967b88a458a1 |
|
MD5 | 9749036976c7a18b4fd6986e5650d51e |
|
BLAKE2b-256 | b9fa28e3ece805630143f21170ee89f8de05635d967249fcbea26e85cd84b5b3 |
File details
Details for the file ltv_mpc-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: ltv_mpc-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d3562a70706e378e011c417fbbbb55b7145aeb882311c88d81c526bbc308aea |
|
MD5 | 67f2b7301ce38012c5d56c8d5c1a709d |
|
BLAKE2b-256 | c9199963a792f151a996788e935e59b9273184a3d462ccd0a0b41766e45674ee |