Linear time-variant model predictive control in Python.
Project description
ltv-mpc
Installation | Documentation | Usage | Contributing
Linear time-variant model predictive control in Python.
This module is designed for prototyping. If you need performance, check out one of the related libraries below.
Installation
pip install ltv-mpc
Usage
The module defines two types:
Problem
defines the model predictive control problem, that is:- Constrained linear time-variant system
- Cost function to optimize
- Initial (and goal) state
Solution
holds the solution to a given problem.
The solve_mpc
function takes a Problem
as input and outputs the corresponding Solution
.
Example
Let us define a triple integrator:
import numpy as np
horizon_duration = 1.0
nb_timesteps = 16
T = horizon_duration / nb_timesteps
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])
ineq_matrix = np.vstack([+accel_from_state, -accel_from_state])
ineq_vector = np.array([+max_accel, +max_accel])
This leads us to the following linear MPC problem:
from ltv_mpc import Problem
initial_pos = 0.0
goal_pos = 1.0
problem = ltv_mpc.Problem(
transition_state_matrix=A,
transition_input_matrix=B,
ineq_state_matrix=ineq_matrix,
ineq_input_matrix=None,
ineq_vector=ineq_vector,
initial_state=np.array([initial_pos, 0.0, 0.0]),
goal_state=np.array([goal_pos, 0.0, 0.0]),
nb_timesteps=nb_timesteps,
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, mpc)
The solution holds complete state and input trajectories as stacked vectors. For instance, we can extract and plot positions, velocities and accelerations as follows:
import pylab
t = np.linspace(0.0, horizon_duration, nb_timesteps + 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)
🏗️ Work in progress
This module is still under development and its API might change. Future works may include:
- Complete documentation
- Complete test coverage
- General linear stage cost functions
See also
System | Library | Language | License |
---|---|---|---|
Linear time-invariant | Copra (original) | C++ | BSD-2-Clause |
Linear time-variant | Copra (fork) | C++ | BSD-2-Clause |
Nonlinear | Crocoddyl | 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-0.6.0.tar.gz
.
File metadata
- Download URL: ltv-mpc-0.6.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf92d894b6f1fd71be21d436f77eefc6b8df12c89b93194cc3246d09b126d1d3 |
|
MD5 | 1e253557aa46da2ae88379dcb3df3621 |
|
BLAKE2b-256 | 43f18443af3dba85335209c80ce7e21471d19bf74dc269a0be7cd59fd503bc59 |
File details
Details for the file ltv_mpc-0.6.0-py3-none-any.whl
.
File metadata
- Download URL: ltv_mpc-0.6.0-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb2d9c0a4494e3cf4bcc49023d57b0f243e9cb3fa4104a80fd55799556daa2be |
|
MD5 | 880550edb09d2a1cb1ddceefac6a9b17 |
|
BLAKE2b-256 | 3d80963a531f29531efdb3e05fa86a658314f950627576d321fad8a45c43c41c |