Skip to main content

Linear time-variant model predictive control in Python.

Project description

ltv-mpc

Installation | Documentation | Example | Contributing

Build Coverage PyPI version Status

Linear time-variant (LTV) model predictive control in Python. Solve a quadratic program of the form:

ltv-mpc

This module is designed for prototyping. If you need performance, check out one of the related libraries below.

Installation

pip install ltv-mpc

Usage

This module defines a one-stop shop function:

solve_mpc(problem: Problem) -> 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:

2022-03-30-172206_1920x1080_scrot

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.

🏗️ 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

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:

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

ltv-mpc-0.6.1.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

ltv_mpc-0.6.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file ltv-mpc-0.6.1.tar.gz.

File metadata

  • Download URL: ltv-mpc-0.6.1.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for ltv-mpc-0.6.1.tar.gz
Algorithm Hash digest
SHA256 47b63e4cfaaca984430388430160deef9b88a42e1cb1a32d9b827fc175b4a1a5
MD5 a6ec1d5c285850631521866f3b42c879
BLAKE2b-256 caa7eb8578c26429b5df8f92ada6b92e6d2147f0358b45a36dd76c723cd4ae12

See more details on using hashes here.

File details

Details for the file ltv_mpc-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: ltv_mpc-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for ltv_mpc-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11ab622dc8aa170b18afcba49d9b84ff5818c53461cea3001c6f9c7d429d6037
MD5 afafb4e8a046d92cbd91cc698b235d71
BLAKE2b-256 5155d9f40b4daf5a1ea36d2fd9ac903a32676b967df08f6504cfdaf905d7385b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page