An open API service indexing awesome lists of open source software.

https://github.com/jonnyhyman/convex_symbolic

Python symbolic canonicalizer and C code generator for embedding convex optimization problems.
https://github.com/jonnyhyman/convex_symbolic

algebra c c99 canon canonicalize code codegen convex cvxpy generation optimization python symbolic

Last synced: about 2 months ago
JSON representation

Python symbolic canonicalizer and C code generator for embedding convex optimization problems.

Awesome Lists containing this project

README

        

# Convex Symbolic

##### A Python symbolic canonicalizer and C code generator for embedding convex optimization problems.

It follows the same problem definition syntax, function naming, and internal nomenclature as the leading Python convex optimization package, [cvxpy](https://github.com/cvxgrp/cvxpy/tree/1.0).

For example, the same constrained least-squares problem [cvxpy](https://github.com/cvxgrp/cvxpy/tree/1.0) exhibits can be converted into C code like so :

```python
from cvx_sym import *

# Problem size.
m = 30
n = 20

# Construct the problem.
A = Parameter((m, n), name = 'A')
b = Parameter((m), name = 'b')
x = Variable((n), name = 'x')

objective = Minimize(sum_squares(A*x - b))
constraints = [0 <= x, x <= 1]
problem = Problem(objective, constraints)

gen = Generate(
problem,
name = 'readme_example',
folder = 'examples',
verbose = True # show each stage of the process
)
```
*Code from:* `testing/integrations/readme_example.py`

Which will save into a folder called `examples/readme_example` all the C code required to solve this problem. The canonical problem matrices are explicitly written to a file called `problem.c`. Parameters are handled symbolically, meaning that upon running the C code

Alternatively, instead of calling `Generate` we can assign parameters, canonicalize, and run with ecos-python :

```python
import numpy
numpy.random.seed(1)

canon = Canonicalize(problem)
canon.assign_values({ # Set values of parameters
'A' : numpy.random.randn(m, n),
'B' : numpy.random.randn(m)
})

solution = solve(canon, verbose = True) # returns what ecos.solve(...) returns
print(solution['x'])
```

#### Requires

- [Python 3.6+](https://www.python.org/),
- Takes advantage of ordered dicts, new feature in 3.6

- [jinja2](http://jinja.pocoo.org/docs/2.10/), template engine
- [numpy](http://www.numpy.org/), for parameter assignment tests
- [ECOS](https://github.com/embotech/ecos), solver source code
- **Place the contents in folder named `__solvers__/ecos`**

- To obtain solutions and run *all* tests
- [ecos-python](https://github.com/embotech/ecos-python), for parameter assignment tests
- [scipy](https://www.scipy.org/), for ecos-python input

#### Methods
The canonicalization methods used are mostly defined [in this paper](https://web.stanford.edu/~boyd/papers/pdf/ecos_codegen_ecc.pdf) by Chu, Parikh, Domahidi, and Boyd.

#### Limitations
- ***No DCP Compliance Checking***. The canonicalizer assumes the problem is well formed and [DCP-compliant](http://dcp.stanford.edu/rules).
- ***Only SOCPs*** (and therefore also QPs, and LPs) are supported. There is no support yet for SDPs, CPs, or GFPs.
- ***Only ECOS*** is supported as a solver
- ***Only C99*** is supported as a code generation language
- Not all functions are implemented yet (ie. vstack, hstack, tv, etc...).
- For the complete list of implemented functions, see the files in `cvx_sym/operations/functions`

#### Recommendations
It is suggested to build and test your problem first in cvxpy, then modify it to be canonicalized or code generated by cvx_sym.

#### Examples
In the `tests/integrations` folder and `tests/test_ecos_solution.py` file, there are a bunch of tests which can be used as examples, and are great starting points in understanding module usage.

##### License: *GNU-GPLv3*
##### Version: *0.0 (Alpha)*