ncpol2sdpa
ncpol2sdpa copied to clipboard
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (34,) and requested shape (34,1)
` from ncpol2sdpa import * n_vars = 2 # Number of variables level = 2 # Requested level of relaxation x = generate_variables('x', n_vars)
obj = x[0]*x[1] + x[1]*x[0] inequalities = [-x[1]**2 + x[1] + 0.5]
substitutions = {x[0]**2 : x[0]}
sdp = SdpRelaxation(x) sdp.get_relaxation(level, objective=obj, inequalities=inequalities, substitutions=substitutions)
sdp.solve() `
ValueError Traceback (most recent call last)
~/.conda/envs/tensorflow/lib/python3.5/site-packages/ncpol2sdpa/sdp_relaxation.py in solve(self, solver, solverparameters) 110 raise Exception("Relaxation is not generated yet. Call " 111 "'SdpRelaxation.get_relaxation' first") --> 112 solve_sdp(self, solver, solverparameters) 113 114
~/.conda/envs/tensorflow/lib/python3.5/site-packages/ncpol2sdpa/solver_common.py in solve_sdp(sdp, solver, solverparameters)
111 elif solver == "cvxpy":
112 primal, dual, x_mat, y_mat, status =
--> 113 solve_with_cvxpy(sdp, solverparameters)
114 elif solver == "scs":
115 if solverparameters is None:
~/.conda/envs/tensorflow/lib/python3.5/site-packages/ncpol2sdpa/cvxpy_utils.py in solve_with_cvxpy(sdp, solverparameters) 37 status = "optimal" 38 x_pre = sdp.F[:, 1:sdp.n_vars+1].dot(problem.variables()[0].value) ---> 39 x_pre += sdp.F[:, 0] 40 row_offsets = [0] 41 cumulative_sum = 0
~/.conda/envs/tensorflow/lib/python3.5/site-packages/scipy/sparse/base.py in radd(self, other) 420 421 def radd(self,other): # other + self --> 422 return self.add(other) 423 424 def sub(self, other): # self - other
~/.conda/envs/tensorflow/lib/python3.5/site-packages/cvxpy/interface/scipy_wrapper.py in new_method(self, other) 29 return NotImplemented 30 else: ---> 31 return method(self, other) 32 return new_method 33
~/.conda/envs/tensorflow/lib/python3.5/site-packages/scipy/sparse/base.py in add(self, other) 414 return self._add_sparse(other) 415 elif isdense(other): --> 416 other = broadcast_to(other, self.shape) 417 return self._add_dense(other) 418 else:
~/.conda/envs/tensorflow/lib/python3.5/site-packages/numpy/lib/stride_tricks.py in broadcast_to(array, shape, subok) 171 [1, 2, 3]]) 172 """ --> 173 return _broadcast_to(array, shape, subok=subok, readonly=True) 174 175
~/.conda/envs/tensorflow/lib/python3.5/site-packages/numpy/lib/stride_tricks.py in _broadcast_to(array, shape, subok, readonly) 126 broadcast = np.nditer( 127 (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras, --> 128 op_flags=[op_flag], itershape=shape, order='C').itviews[0] 129 result = _maybe_view_as_subclass(array, broadcast) 130 if needs_writeable and not result.flags.writeable:
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (34,) and requested shape (34,1)
I have the same issue with numpy
version 1.16.4 and scipy
version 1.3.0, but the proprietarymosek
solver doesn't run into this issue. If you can get a license, try:
sdp.solve(solver='mosek')
Not sure if anyone still encounters this problem. I encountered the same problem yesterday when following the same code in the tutorial. Although this can be solved by using solver other than CVXPY, which also mentioned in the issue, #closed #43. Here, I provide an informal solution for people who prefer CVXPY.
The problem comes from the mismatched shape of the numpy array and scipy matrix in line 39 in cvxpy_utils.py in this issue.
---> 39 x_pre += sdp.F[:, 0]
The way I solved it is adding an extra line in front of line 39.
x_pre = x_pre.reshape(x_pre.shape[0], 1)
This will reshape the 1d-array of size (n, ) into a 2d-matrix of size (n, 1), which solves this problem. Hope this can help anyone suffers from the same issue.
@perambluate Thanks for this fix. I'll update my fork with it if you don't mind.
@peterjbrown519 Sure, no problem.