moehle / cvxpy_codegen Goto Github PK
View Code? Open in Web Editor NEWEmbedded code generation for convex optimization, based on CVXPY
License: GNU General Public License v3.0
Embedded code generation for convex optimization, based on CVXPY
License: GNU General Public License v3.0
If I modify the example code slightly to add a new parameter that requires a transpose operation, i.e.:
import cvxpy_codegen as cg
m = 10
n = 5
A = cg.Parameter(m, n, name='A')
b = cg.Parameter(m, name='b')
c = cg.Parameter(n, name='c') # <--------- New param
x = cg.Variable(n, name='x')
f0 = cg.norm(A*x - b) - c.T*x # <--------- Transpose added
prob = cg.Problem(cg.Minimize(f0))
prob.codegen('least_squares_example')
I get the following exception:
Traceback (most recent call last):
File "transpose_test.py", line 10, in
prob.codegen('least_squares_example')
File "/usr/local/lib/python3.5/dist-packages/cvxpy_codegen-0.0.1-py3.5.egg/cvxpy_codegen/problem.py", line 31, in codegen
CodeGenerator(obj, constraints, vars, params).codegen(target_dir)
File "/usr/local/lib/python3.5/dist-packages/cvxpy_codegen-0.0.1-py3.5.egg/cvxpy_codegen/code_generator.py", line 100, in codegen
self.template_vars.update(param_handler.get_template_vars())
File "/usr/local/lib/python3.5/dist-packages/cvxpy_codegen-0.0.1-py3.5.egg/cvxpy_codegen/param/param_handler.py", line 72, in get_template_vars
self.process_expression(root_param)
File "/usr/local/lib/python3.5/dist-packages/cvxpy_codegen-0.0.1-py3.5.egg/cvxpy_codegen/param/param_handler.py", line 92, in process_expression
data_arg = self.process_expression(expr.atom)
File "/usr/local/lib/python3.5/dist-packages/cvxpy_codegen-0.0.1-py3.5.egg/cvxpy_codegen/param/param_handler.py", line 121, in process_expression
data_list = get_atom_data(expr, arg_data)
File "/usr/local/lib/python3.5/dist-packages/cvxpy_codegen-0.0.1-py3.5.egg/cvxpy_codegen/atoms/atoms.py", line 68, in get_atom_data
raise TypeError("Constant expressions involving the %s atom not supported." % str(type(expr)))
TypeError: Constant expressions involving the <class 'cvxpy.atoms.affine.transpose.transpose'> atom not supported.
I can get it to run by removing the transpose operator and redefining the parameter as a row vector, i.e.: c = cg.Parameter(1,n, name='c')
...but it would be nice to support the canonical constraint form where c is a column vector. As it is I'll have to always remember to transpose that value in particular before passing in to the C function call or Python wrapper.
Scalar parameters are not handled properly in the bit of code that copies parameter values in:
/* Copy parameter values into the parameter structure. */
{% for p in named_params %}
// memcpy(params.{{ p.name }}, {{ p.name }}->data, {{ p.size[0]*p.size[1] }}*sizeof(double));
memcpy(params.{{ p.name }}, PyArray_DATA({{ p.name }}), {{ p.size[0]*p.size[1] }}*sizeof(double));
{% endfor %}
All parameters are being treated as matrices (as pointers to array), and or scalar parameters this is causing the parameter value itself to be passed in as the first argument to memcpy instead of a pointer.
The issue can be replicated by running the following script, which generates code for a second-order cone program with a single constraint of the form ||Ax + b|| - c*x - d <= 0
import numpy as np
import cvxpy as cvx
from cvxpy_codegen import codegen
m = 3
n = 3
A = cvx.Parameter(m,n, name='A')
b = cvx.Parameter(m, name='b')
c = cvx.Parameter(1,n, name='c')
d = cvx.Parameter(1, name='d')
x = cvx.Variable(n, name='x')
f = np.array( [[0,0,0],
[0,0,0],
[0,0,1]] )
objective = cvx.Minimize( cvx.norm(f.T*x) )
A.value = np.zeros( (m,n) )
b.value = np.zeros( (m,1) )
c.value = np.zeros( (1,n) )
d.value = 0
constraints = []
constraints.append( cvx.norm(A*x + b) - c*x - d <= 0 )
prob = cvx.Problem(objective, constraints)
codegen(prob, 'min_z_example')
Building the generated example executable works fine, however building the Python extension fails with the following error:
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DPYTHON -DDLONG -DLDL_LONG -DCTRLC=1 -Iecos/include -Iecos/external/amd/include -Iecos/external/ldl/include -Iecos/external/SuiteSparse_config -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c codegenmodule.c -o build/temp.linux-x86_64-2.7/codegenmodule.o
codegenmodule.c: In function ‘solve_wrapper’:
codegenmodule.c:59:10: error: incompatible type for argument 1 of ‘memcpy’
memcpy(params.d, PyArray_DATA(d), 1*sizeof(double));
^
In file included from /usr/include/features.h:367:0,
from /usr/include/limits.h:25,
from /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed/limits.h:168,
from /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed/syslimits.h:7,
from /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed/limits.h:34,
from /usr/include/python2.7/Python.h:19,
from codegenmodule.c:20:
/usr/include/x86_64-linux-gnu/bits/string3.h:50:1: note: expected ‘void * restrict’ but argument is of type ‘double’
__NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
^
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
The following change seems to fix the issue:
/* Copy parameter values into the parameter structure. */
{% for p in named_params %}
{% if p.is_scalar %}
memcpy(¶ms.{{ p.name }}, PyArray_DATA({{ p.name }}), {{ p.size[0]*p.size[1] }}*sizeof(double));
{% else %}
// memcpy(params.{{ p.name }}, {{ p.name }}->data, {{ p.size[0]*p.size[1] }}*sizeof(double));
memcpy(params.{{ p.name }}, PyArray_DATA({{ p.name }}), {{ p.size[0]*p.size[1] }}*sizeof(double));
{% endif %}
{% endfor %}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.