Python: Semidefinite program with CVXPy 'Error parsing inputs'

  • Context: Python 
  • Thread starter Thread starter Master1022
  • Start date Start date
  • Tags Tags
    Program Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
Master1022
Messages
590
Reaction score
116
TL;DR
I am trying to run a semidefinite program in Python using CVXPy. I have gone to the documentation and the example code in there doesn't run in Google Colab for me and yields the error: 'Error parsing inputs'.
Hi,

Question:
Why is the error 'Error parsing inputs' in CVXPy occurring?

Context:
I am trying to solve a semidefinite program in CVXPy (using Google Colab). I went to the documentation (HERE) and I copied the example code into a cell. It doesn't work for some reason and I don't understand why. It is throwing the following message: 'Error parsing inputs'. Why is this happening?

Code:
[CODE lang="python" title="CVXPy Semidefinite Code Documentation Example"]# Import packages.
import cvxpy as cp
import numpy as np

# Generate a random SDP.
n = 3
p = 3
np.random.seed(1)
C = np.random.randn(n, n)
A = []
b = []
for i in range(p):
A.append(np.random.randn(n, n))
b.append(np.random.randn())

# Define and solve the CVXPY problem.
# Create a symmetric matrix variable.
X = cp.Variable((n,n), symmetric=True)
# The operator >> denotes matrix inequality.
constraints = [X >> 0]
constraints += [
cp.trace(A @ X) == b for i in range(p)
]
prob = cp.Problem(cp.Minimize(cp.trace(C @ X)),
constraints)
prob.solve()

# Print result.
print("The optimal value is", prob.value)
print("A solution X is")
print(X.value)[/CODE]Any help would be greatly appreciated
 
Physics news on Phys.org
This appears to be a compatibility problem with the cvxpy package and the Google Colab Python 3.7 environment. It works for me in a local Jupyter Notebook environment (Python 3.9).
 
Reply
  • Informative
  • Like
Likes   Reactions: Master1022 and berkeman
pbuk said:
This appears to be a compatibility problem with the cvxpy package and the Google Colab Python 3.7 environment. It works for me in a local Jupyter Notebook environment (Python 3.9).
Thanks for the reply @pbuk ! That's correct - when I used a version on my local machine via a conda environment, then it worked