dccp icon indicating copy to clipboard operation
dccp copied to clipboard

Error using complex variables

Open pizqleh opened this issue 5 years ago • 5 comments

Hello,

I have been working with CVXPY over a convex problem with complex variables and no constraints of the form

min C_a(x),

where x = cp.Variable(n, complex=True). It worked perfectly, even using DCCP.

Now, it turns out that this is an approximation of a convex-concave problem with complex variables and no constraints of the form

min C_{a_1}(x) - C_{a_2}(x),

where x = cp.Variable(n, complex=True).

Then, I tried to solve it with DCCP using the standard reformulation

min C_{a_1}(x) - t s.t. C_{a_2}(x) = t,

where x = cp.Variable(n, complex=True), t = cp.Variable(1). But I couldn't, the solver threw this error:

File "/Users/P/Desktop/thesis/wave-field-reconstruction/sinusoidal.py", line 139, in minimice_dccp optimum = p_problem.solve(method='dccp') File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/problems/problem.py", line 396, in solve return solve_func(self, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/dccp/problem.py", line 54, in dccp result_temp = iter_dccp( File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/dccp/problem.py", line 241, in iter_dccp temp = convexify_constr(arg) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/dccp/constraint.py", line 76, in convexify_constr right = linearize(constr.args[1]) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/dccp/linearize.py", line 58, in linearize grad_map = expr.grad File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/atoms/atom.py", line 399, in grad grad_arg = arg.grad File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/atoms/atom.py", line 399, in grad grad_arg = arg.grad File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/atoms/atom.py", line 399, in grad grad_arg = arg.grad [Previous line repeated 2 more times] File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/atoms/atom.py", line 393, in grad grad_self = self._grad(arg_values) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/atoms/affine/affine_atom.py", line 131, in _grad canon_mat = canonInterface.get_problem_matrix( File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/cvxcore/python/canonInterface.py", line 327, in get_problem_matrix build_lin_op_tree(lin, linPy_to_linC) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/cvxcore/python/canonInterface.py", line 518, in build_lin_op_tree make_linC_from_linPy(linPy, linPy_to_linC) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/cvxcore/python/canonInterface.py", line 492, in make_linC_from_linPy set_linC_data(linC, linPy) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/cvxcore/python/canonInterface.py", line 466, in set_linC_data set_matrix_data(linC, linPy) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/cvxcore/python/canonInterface.py", line 438, in set_matrix_data linC.set_dense_data(format_matrix(linPy.data, shape=linPy.shape)) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/cvxcore/python/cvxcore.py", line 200, in set_dense_data return _cvxcore.LinOp_set_dense_data(self, matrix) TypeError: Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe'

Maybe the error is related to the utilization of complex and real variables. It's wierd, beacuse if I put t = cp.Variable() instead of t = cp.Variable(1) I get a different error:

File "/Users/P/Desktop/thesis/wave-field-reconstruction/sinusoidal.py", line 139, in minimizar_dccp optimum = p_problem.solve(method='dccp') File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/problems/problem.py", line 396, in solve return solve_func(self, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/dccp/problem.py", line 50, in dccp dccp_ini( File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/dccp/problem.py", line 142, in dccp_ini value_para[count_para].value = np.random.randn(var.size) * 10 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/expressions/constants/parameter.py", line 82, in value self._value = self._validate_value(val) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cvxpy/expressions/leaf.py", line 379, in _validate_value raise ValueError( ValueError: Invalid dimensions (1,) for Parameter value.

Thanks in advance, P.

pizqleh avatar Nov 24 '20 16:11 pizqleh

Can you post a code snippet that reproduces the bug?

SteveDiamond avatar Nov 26 '20 20:11 SteveDiamond

I made up a simple dccp problem, but when I tried to solve it I got the error "ValueError: Invalid dimensons (1,) for Parameter value."

The version of dccp is 1.0.3 and cvxpy is 1.1.15, and python is 3.8.5

Here's the program x,y,z = [cp.Variable(name=_, nonneg=True) for _ in ['x', 'y', 'z']] cons = [cp.square(x + y) == cp.square(x) + cp.square(y) - 2 * z, x + y == 1] prob = cp.Problem(cp.Maximize(z), cons)


when I try

prob.solve(method='dccp')

I get the above error in line 382 of of leaf.py called fro line 142 in dccp_ini

algebravic avatar Aug 16 '21 21:08 algebravic

Hi @algebravic, I do not have this issue using CVXPY 1.1.15 and the most updated DCCP. Did you install DCCP from this repository? If not, you could download the master branch and install by running

python setup.py install

under the dccp directory.

To get a result for this particular example, the parameter tau needs to be set to 1 by

prob.solve(method="dccp", tau=1)

xinyueshen avatar Aug 16 '21 23:08 xinyueshen

It appears that the problem is that x = cvxpy.Variable(name='x', nonneg=True) gives x.shape = (). If I set it to (1,) instead it works. However, I also was unable to do something like

prob.solve(method='dccp', times=10)

because the times parameter is then passed to ecos which then complains.

algebravic avatar Aug 17 '21 14:08 algebravic

To run the algorithm many times with different random initial points, the parameter that needs to be set is ccp_times, not times.

xinyueshen avatar Aug 17 '21 18:08 xinyueshen