Giter Site home page Giter Site logo

rssalessio / pydeepc Goto Github PK

View Code? Open in Web Editor NEW
40.0 1.0 12.0 241 KB

Python library that implements DeePC: Data-Enabled Predictive Control

License: MIT License

Python 100.00%
data-driven-control data-driven-mpc mpc deepc adaptive-control data-driven model-free-control data-enabled-predictive-control python

pydeepc's Introduction

PyDeePC

License: MIT Last Release

Python library that implements DeePC: Data-Enabled Predictive Control.

Original paper: Data-Enabled Predictive Control: In the Shallows of the DeePC
Library Author: Alessio Russo (PhD Student at KTH - [email protected])
License: MIT

Other contributors: - Many thanks to Edgar W. for spotting out a bug that let the slack variables slack_u and slack_y assume any value when the value of the corresponding regularizer was 0.

Closed loop results

DeePC applied to a 3-pulley system with transfer function

$$T(z) = \frac{0.28z+0.51}{z^4-1.42z^3 +1.59z^2 -1.32z+0.89}$$

with sampling time Ts=0.05 [s]. Check the file in examples\example_siso_pulley.py for more information.

Requirements

  • Python 3.7
  • Numpy, Scipy, CVXPY

Installation

Use the setup.py file to install the library (execute the command pip install .)

Usage/Examples

The library makes extensive use of the CVXPY library. If you are unfamiliar with CVXPY, we strongly recommend you to first learn about CVXPY.

The algorithm can be instantiated by creating a DeePC object (see example below). Use the DeePC.build_problem to build the optimization problem and DeePC.solve to solve the problem.

To learn how to use the library, check the examples located in the examples/ folder.

In general the code has the following structure

import numpy as np
import cvxpy as cp

from typing import List
from cvxpy.expressions.expression import Expression
from cvxpy.constraints.constraint import Constraint
from pydeepc import DeePC
from pydeepc.utils import Data

# Define the loss function for DeePC. The callback should accept
# 2 input/output variables, each of type Variable (see CVXPY library)
# The callback must return the objective function
def loss_callback(u: cp.Variable, y: cp.Variable) -> Expression:
    horizon, M, P = u.shape[0], u.shape[1], y.shape[1]
    # Sum_t ||y_t - 1||^2
    return cp.norm(y - 1, 'fro') + 0.1 * cp.norm(u, 'fro)

# Define the constraints for DeePC. See also how constraints are defined
# in CVXPY. The callback should accept # 2 input/output variables, each
# of type Variable (see CVXPY library). The callback must return a list of
# constraints
def constraints_callback(u: cp.Variable, y: cp.Variable) -> List[Constraint]:
    horizon, M, P = u.shape[0], u.shape[1], y.shape[1]
    # Define a list of input/output constraints
    return [y <= 10, y >= -10, u >= -20, u <= 20]

# DeePC paramters
s = 3                       # How many steps before we solve again the DeePC problem
T_INI = 5                   # Size of the initial set of data
T = 200                     # Number of data points used to estimate the system
HORIZON = 30                # Horizon length
LAMBDA_G_REGULARIZER = 0    # g regularizer (see DeePC paper, eq. 8)
LAMBDA_Y_REGULARIZER = 0    # y regularizer (see DeePC paper, eq. 8)

# Define plant
sys = ...

# Generate initial data and initialize DeePC
u = ... # define input data of length T
y = ... # apply input to system and measure output
data = Data(u, y)
deepc = DeePC(data, Tini = T_INI, horizon = HORIZON)

# Build the deepc problem
deepc.build_problem(
    build_loss = loss_callback,
    build_constraints = constraints_callback,
    lambda_g = LAMBDA_G_REGULARIZER,
    lambda_y = LAMBDA_Y_REGULARIZER)

# Simulate for a number of steps
for idx in range(300):
    # Update initial data and solve DeepC
    u_optimal, info = deepc.solve(data_ini = data_ini)

    output = ... # Apply optimal control input of size s to the system and measure output
    data_ini = Data(..., ...) # Use last T_INI samples to build a new initial condition

Known problems

  • When using the projection regularizer you may encounter some problems while using the ECOS solver. We suggest setting the regularizer to 0, or using another solver (e.g., MOSEK)

TODO

  • Add tests

License

Our code is released under the MIT license

License: MIT

pydeepc's People

Contributors

rssalessio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

pydeepc's Issues

Bug of the cvxpy variables with cp.reshape

Describe the bug
There is a small issue in deepc.py lines 139 and 140 with the following lines:
u = cp.reshape(u, (self.horizon, self.M))
y = cp.reshape(y, (self.horizon, self.P))
This may lead to a bug in the constraints when using multiple inputs or outputs, because the cp.reshape is Fortran type by default.

To Fix
This can be fixed by simply forcing the C type reshape as follows:
u = cp.reshape(u, (self.horizon, self.M), 'C')
y = cp.reshape(y, (self.horizon, self.P), 'C')

** Other comments**
Please do more tests if you can to make sure the reshape doesn't affect other things in the code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.