Giter VIP home page Giter VIP logo

pylmi-sdp's Introduction

PyLMI-SDP

Symbolic linear matrix inequalities (LMI) and semi-definite programming (SDP) tools for Python

This package includes a set of classes to represent and manipulate LMIs symbolically using SymPy. It also includes tools to export LMIs to CVXOPT SDP input and to the SDPA format.

Depends on SymPy and NumPy; and optionally on CVXOPT and on SciPy (for sparse matrices). Single codebase supporting both Python 2.7 and Python 3.x. PyLMI-SDP is tested for various combinations of Python and sympy. See here.

PyLMI-SDP is at GitHub.

Build Status Coverage Status

LMI Definition

Examples

>>> from sympy import symbols, Matrix
>>> from lmi_sdp import LMI_PD, LMI_NSD
>>> variables = symbols('x y z')
>>> x, y, z = variables
>>> lmi = LMI_PD(Matrix([[x+1, y+2], [y+2, z+x]]))
>>> lmi
Matrix([
[x + 1, y + 2],
[y + 2, x + z]]) > 0
>>> from lmi_sdp import init_lmi_latex_printing
>>> from sympy import latex
>>> init_lmi_latex_printing()
>>> print(latex(lmi))
\left[\begin{matrix}x + 1 & y + 2\\y + 2 & x + z\end{matrix}\right] \succ 0

lmi

>>> print(latex(lmi.expanded(variables)))
\left[\begin{matrix}1.0 & 0.0\\0.0 & 1.0\end{matrix}\right] x + \left[\begin{matrix}0.0 & 1.0\\1.0 & 0.0\end{matrix}\right] y + \left[\begin{matrix}0.0 & 0.0\\0.0 & 1.0\end{matrix}\right] z + \left[\begin{matrix}1.0 & 2.0\\2.0 & 0.0\end{matrix}\right] \succ 0

lmi.expanded(variables)

>>> lmi_2 = LMI_NSD( Matrix([[-x, -y], [-y, -z-x]]), Matrix([[1, 2], [2, 0]]))
>>> lmi_2
Matrix([
[-x,     -y],
[-y, -x - z]]) <= Matrix([
[1, 2],
[2, 0]])
>>> lmi_2.canonical()
Matrix([
[x + 1, y + 2],
[y + 2, x + z]]) >= 0
>>> print(latex(lmi_2))
\left[\begin{matrix}- x & - y\\- y & - x - z\end{matrix}\right] \preceq \left[\begin{matrix}1 & 2\\2 & 0\end{matrix}\right]

lmi_2

Convertion to CVXOPT SDP

Example

(from CVXOPT SDP example)

>>> from sympy import symbols, Matrix
>>> from lmi_sdp import LMI_NSD, init_lmi_latex_printing
>>>
>>> init_lmi_latex_printing()
>>>
>>> variables = symbols('x1 x2 x3')
>>> x1, x2, x3 = variables
>>>
>>> min_obj = x1 - x2 + x3
>>>
>>> LMI_1 = LMI_NSD(
...     x1*Matrix([[-7, -11], [-11, 3]]) +
...     x2*Matrix([[7, -18], [-18, 8]]) +
...     x3*Matrix([[-2, -8], [-8, 1]]),
...     Matrix([[33, -9], [-9, 26]]))
>>>
>>> LMI_2 = LMI_NSD(
...     x1*Matrix([[-21, -11, 0], [-11, 10, 8], [0, 8, 5]]) +
...     x2*Matrix([[0, 10, 16], [10, -10, -10], [16, -10, 3]]) +
...     x3*Matrix([[-5, 2, -17], [2, -6, 8], [-17, 8, 6]]),
...     Matrix([[14, 9, 40], [9, 91, 10], [40, 10, 15]]))
>>>
>>> min_obj
x1 - x2 + x3

min_obj

>>> LMI_1.expanded(variables)
Matrix([
[ -7.0, -11.0],
[-11.0,   3.0]])*x1 + Matrix([
[  7.0, -18.0],
[-18.0,   8.0]])*x2 + Matrix([
[-2.0, -8.0],
[-8.0,  1.0]])*x3 <= Matrix([
[33, -9],
[-9, 26]])

LMI_1.expanded(variables)

>>> LMI_2.expanded(variables)
Matrix([
[-21.0, -11.0, 0.0],
[-11.0,  10.0, 8.0],
[  0.0,   8.0, 5.0]])*x1 + Matrix([
[ 0.0,  10.0,  16.0],
[10.0, -10.0, -10.0],
[16.0, -10.0,   3.0]])*x2 + Matrix([
[ -5.0,  2.0, -17.0],
[  2.0, -6.0,   8.0],
[-17.0,  8.0,   6.0]])*x3 <= Matrix([
[14,  9, 40],
[ 9, 91, 10],
[40, 10, 15]])

LMI_2.expanded(variables)

>>> from cvxopt import solvers
>>> from lmi_sdp import to_cvxopt
>>>
>>> solvers.options['show_progress'] = False
>>>
>>> c, Gs, hs = to_cvxopt(min_obj, [LMI_1, LMI_2], variables)
>>>
>>> sol = solvers.sdp(c, Gs=Gs, hs=hs)
>>> print(sol['x'])
[-3.68e-01]
[ 1.90e+00]
[-8.88e-01]
<BLANKLINE>

Export to SDPA Format

Example

>>> from sympy import symbols, Matrix
>>> from lmi_sdp import LMI_PSD, to_sdpa_sparse
>>>
>>> variables = x1, x2 = symbols('x1 x2')
>>>
>>> min_obj = 10*x1 + 20*x2
>>> lmi_1 = LMI_PSD(
...     -Matrix([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]) +
...     Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])*x1 +
...     Matrix([[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 5, 2], [0, 0, 2, 6]])*x2)
>>> lmi_1
Matrix([
[x1 - 1,           0,        0,        0],
[     0, x1 + x2 - 2,        0,        0],
[     0,           0, 5*x2 - 3,     2*x2],
[     0,           0,     2*x2, 6*x2 - 4]]) >= 0
>>>
>>> dat = to_sdpa_sparse(min_obj, lmi_1, variables, comment='test sparse')
>>> print(dat)
"test sparse"
2 = ndim
3 = nblocks
1 1 2 = blockstruct
10.0, 20.0 = objcoeffs
0 1 1 1 1.0
0 2 1 1 2.0
0 3 1 1 3.0
0 3 2 2 4.0
1 1 1 1 1.0
1 2 1 1 1.0
2 2 1 1 1.0
2 3 1 1 5.0
2 3 1 2 2.0
2 3 2 2 6.0
<BLANKLINE>

Author

Cristóvão Duarte Sousa

Install

From PyPi:

pip install PyLMI-SDP

From git source:

git clone https://github.com/cdsousa/PyLMI-SDP.git
cd PyLMI-SDP
python setup.py install

License

Simplified BSD License. See License File

pylmi-sdp's People

Contributors

cdsousa avatar rhaschke 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pylmi-sdp's Issues

Accessing the values of the variables

Hello

How do I access the symbol's values after the optimization is solved?
I need to compute the gain matrix K from the values of the variables.

This package is unmaintained and does not works with up to date SymPy versions

Running the first example from the readme is failing:

lmi = LMI_PD(Matrix([[x+1, y+2], [y+2, z+x]]))

Yields the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/lmi_sdp/lmi.py", line 129, in __new__
    assert_symmetry)
  File "/usr/local/lib/python2.7/site-packages/lmi_sdp/lmi.py", line 38, in __new__
    return rel_cls.__new__(cls, lhs, rhs)
  File "/usr/local/lib/python2.7/site-packages/sympy/core/relational.py", line 295, in __new__
    r = cls._eval_relation(lhs, rhs)
  File "/usr/local/lib/python2.7/site-packages/sympy/core/relational.py", line 616, in _eval_relation
    return _sympify(lhs.__gt__(rhs))
  File "/usr/local/lib/python2.7/site-packages/sympy/matrices/matrices.py", line 3084, in __getattr__
    "%s has no attribute %s." % (self.__class__.__name__, attr))
AttributeError: ImmutableMatrix has no attribute __gt__.

Missing requirement: `packaging`

After using pip to install this package on Ubuntu 20.04:

$ pip3 install PyLMI-SDP
Collecting PyLMI-SDP
  Using cached PyLMI_SDP-1.1-py3-none-any.whl (10 kB)
Requirement already satisfied: sympy in /home/jorolf/.local/lib/python3.8/site-packages (from PyLMI-SDP) (1.11.1)
Requirement already satisfied: mpmath>=0.19 in /home/jorolf/.local/lib/python3.8/site-packages (from sympy->PyLMI-SDP) (1.3.0)
Installing collected packages: PyLMI-SDP
Successfully installed PyLMI-SDP-1.1

I get the following error message after importing the module:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
 in 
      6 import cvxopt
      7 # https://pypi.org/project/PyLMI-SDP is used to formulate LMIs
----> 8 from lmi_sdp import LMI_PD as LMI, lmi_to_coeffs, to_cvxopt
      9 from tf import transformations as tf
     10 from robot_model import adjoint

~/.local/lib/python3.8/site-packages/lmi_sdp/__init__.py in 
      4 
      5 from .lm import *
----> 6 from .lmi import *
      7 from .sdp import *

~/.local/lib/python3.8/site-packages/lmi_sdp/lmi.py in 
      9 from .lm import lm_sym_expanded
     10 
---> 11 from packaging import version
     12 
     13 

ModuleNotFoundError: No module named 'packaging'

Which can be fixed by also installing the packaging package, although the "correct" solution would probably be to add it as a dependency for this package...

Doc tests fail if CVXOPT is not installed

Tests with CVXOPT on tests folder are run only if that package is installed.
However, the docs include include an example with CVXOPT which fails if it is not installed.

Release to PyPI

Dear @cdsousa,
I have merged my fixes and prepared a new release (1.1). However, you need to grant me permissions on PyPI to proceed.
Alternatively, just upload a new release yourself:

pip3 install --upgrade twine
python setup.py sdist bdist_wheel
python3 -m twine check dist/*
python3 -m twine upload dist/*

If you decide for the former option, my PyPI account is rhaschke.

some errors occur when i run the test code

Screenshot from 2019-08-01 09-59-46
the upper image happened when i use the fuction "test_LMI_PSD()" in the file'test_lmi.py '

the lower image happened when i use the fuction "test_LMI_canonical()" in the file'test_lmi.py '
Screenshot from 2019-08-01 10-01-17

And python version in my laptop is Python 3.5.2

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.