Skip to main content

A data-driven framework for synthesizing feedback controllers using learned Control Lyapunov Functions

Project description

Learning Control Lyapunov Functions with Formal Verification

A data-driven framework for synthesizing Control Lyapunov Functions (CLFs) and stabilizing feedback controllers using formal verification algorithms for nonlinear systems.

Problem Overview

Designing feedback-stabilizing controllers for general nonlinear systems is often analytically intractable. This package implements a machine learning approach to synthesize stabilizing controllers for nonlinear control-affine systems of the form:

ẋ = f(x) + g(x)u

where x ∈ ℝⁿ, u ∈ ℝᵐ, and f(x), g(x) are nonlinear vector fields.

The package workflow:

  1. Learning: Uses a neural network to represent a candidate Lyapunov function V(x)
  2. Verification: Verifies V(x) using symbolic reasoning via the dReal solver
  3. Synthesis: Synthesizes a stabilizing controller using Sontag's universal formula
  4. Simulation: Simulates the closed-loop system using RK45 integration

Installation

Step 1: Installing dReal (REQUIRED)

The package relies on dReal for symbolic verification of Lyapunov functions. Installation instructions for Ubuntu:

Ubuntu (Recommended Method)

# Add the dReal PPA repository
sudo apt-get update
sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:dreal/dreal
sudo apt-get update

# Install dReal with dependencies
sudo apt-get install -y libdreal-dev

# Install the Python package
pip install dreal

You can verify the installation by running:

import dreal
x = dreal.Variable("x")
print(dreal.sin(x))  # Should print sin(x) without errors

Troubleshooting dReal Installation

If you encounter any issues:

  1. Make sure all dependencies are installed:

    sudo apt-get install -y build-essential pkg-config libgmp-dev
    
  2. Check for any error messages when importing dReal:

    import dreal
    
  3. If you see errors about missing libraries, you may need to install:

    sudo apt-get install -y libstdc++6
    
  4. For other Linux distributions or platforms, refer to the dReal documentation.

Step 2: Install the Control Lyapunov Package

Option 1: Install from PyPI

pip install control_lyapunov

Option 2: Install from source

git clone https://github.com/Vrushabh27/control_lyapunov.git
cd control_lyapunov
pip install -e .

Usage

1. Define Your Nonlinear System

Create a custom system by implementing the required functions, or use one of the predefined models:

from control_lyapunov.models.van_der_pol import VanDerPol

# Create an instance of the van der Pol oscillator
system = VanDerPol(mu=1.0)  # mu is the system parameter

For a custom system, implement a class with the following methods:

  • f(x): Drift dynamics
  • g(x): Control dynamics
  • system(x, u): Combined system dynamics
  • f_symbolic(x_vars): Symbolic drift dynamics for verification
  • g_symbolic(x_vars): Symbolic control dynamics for verification

2. Create and Train a Lyapunov Function

from control_lyapunov.learning import MultiLayerNet, train_lyapunov_network

# Define neural network architecture
state_dim = system.state_dim
hidden_layers = [6]  # Single hidden layer with 6 neurons
output_dim = 1  # Scalar Lyapunov function
lqr_init = system.initial_control_weights()

# Create the model
model = MultiLayerNet(state_dim, hidden_layers, output_dim, lqr_init)

# Configure training
data_config = {
    'num_samples': 500,
    'state_dim': state_dim,
    'bounds': (-6, 6)
}

training_config = {
    'learning_rate': 0.01,
    'max_iterations': 2000,
    'optimizer': 'adam'
}

# Create a verifier
from control_lyapunov.verification import create_verifier

verifier = create_verifier(
    system.f_symbolic,
    system.g_symbolic,
    None,  # We'll use the learned control
    state_dim,
    (0.5, 6.0),  # (ball_lb, ball_ub) for verification
    1e-2,  # precision
    0  # epsilon (0 for strict Lyapunov)
)

# Train the model
model, loss_history, valid = train_lyapunov_network(
    system.system,
    model,
    data_config,
    training_config,
    verifier
)

3. Synthesize a Controller

from control_lyapunov.sontag import controller_closed_form

# Create a Sontag controller
controller = controller_closed_form(
    model,
    state_dim,
    system.f,
    system.g
)

4. Simulate the Closed-Loop System

from control_lyapunov.simulation import simulate_system, plot_simulation_results

# Define initial state and simulation parameters
x0 = np.array([1.5, 0.0])  # Initial state
t_span = (0, 10.0)  # 10 seconds simulation
dt = 0.01  # Time step

# Simulate
t, x, u = simulate_system(
    x0,
    t_span,
    controller,
    system.f,
    system.g,
    dt
)

# Plot results
state_labels = ['$x_1$ (position)', '$x_2$ (velocity)']
control_labels = ['$u$ (control)']

fig, axes = plot_simulation_results(
    t, x, u,
    state_labels=state_labels, 
    control_labels=control_labels,
    title='Controlled System'
)

Complete Example

See the complete example for the van der Pol oscillator:

import control_lyapunov as cl
from control_lyapunov.models.van_der_pol import VanDerPol

# Create the system
vdp = VanDerPol(mu=1.0)

# Create and train the model
# ... (see examples/van_der_pol_example.py for the complete workflow)

The examples directory contains full examples for different systems.

Running Examples

To run the Van der Pol example:

# Make sure dReal is installed first
python -m control_lyapunov.examples.van_der_pol_example

Configuration Options

The package offers numerous tuning options:

Neural Network Configuration

  • Number of hidden layers and neurons
  • Activation functions
  • Initial control weights

Training Configuration

  • Learning rate
  • Optimizer (Adam, SGD)
  • Number of iterations
  • Training data size and sampling bounds

Verification Configuration

  • Verification region (ball_lb, ball_ub)
  • Precision for dReal solver
  • Epsilon value for Lie derivative bound

Simulation Configuration

  • Integration method
  • Time step
  • Simulation duration
  • Initial state

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

control_lyapunov-0.1.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

control_lyapunov-0.1.0-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file control_lyapunov-0.1.0.tar.gz.

File metadata

  • Download URL: control_lyapunov-0.1.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for control_lyapunov-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e59b7bd5f5fa803294b32bc796ba44a402e960d61502400f02c3861859facd9b
MD5 db1d1b2c4fd5c4ccf90d4d086608d9dc
BLAKE2b-256 5a4b396c4406000233c2aeaa328ed6731498141a3fa0fa699e0550d54eda5b63

See more details on using hashes here.

File details

Details for the file control_lyapunov-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for control_lyapunov-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b94cc4c6fe5f521acef52ea1ff8975c1cbb922f002d2497bc43c4481fca6d568
MD5 c4451f2bff9332161a4b9b7ff012558b
BLAKE2b-256 c5fd9103e6fb1f9e26061632bbcde03dcbb6adde2a0de4d0af5c681a515594e5

See more details on using hashes here.

Supported by

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