cvxgrp / scs

Splitting Conic Solver
MIT License
553 stars 136 forks source link

AttributeError: module 'scs' has no attribute 'SCS' #208

Closed ZedongPeng closed 2 years ago

ZedongPeng commented 2 years ago

Specifications

Description

I am a newbee to SCS. I tested the instance provided in the python-example. I got a Erroras follows. AttributeError: module 'scs' has no attribute 'SCS' I haven't installed BLAS or LAPACK. I am not sure if this result in the AttributeError. If not, how can I fix it? Many thanks.

import scs
import numpy as np
from scipy import sparse

# Generate problem data
np.random.seed(1)

# Matrix size parameters
n = 50  # Number of variables
p = 20  # Number of constraints

# Generate random problem data
tmp = np.random.rand(n)
tmp /= np.sum(tmp)
Ad = np.random.randn(p, n)
bd = 0.5 * Ad.dot(tmp) + 0.01 * np.random.rand(p)

# Build the A, b rows corresponding to the exponential cone
A_exp = sparse.lil_matrix((3 * n, 2 * n))
b_exp = np.zeros(3 * n)
for i in range(n):
    A_exp[i * 3, i] = -1  # t
    A_exp[i * 3 + 1, i + n] = -1  # x
    b_exp[i * 3 + 2] = 1

A = sparse.vstack(
    [
        # zero cone
        sparse.hstack([sparse.csc_matrix((1, n)), np.ones((1, n))]),
        # positive cone
        sparse.hstack([sparse.csc_matrix((p, n)), -Ad]),
        # exponential cones
        A_exp,
    ],
    format="csc",
)
b = np.hstack([1, -bd, b_exp])
c = np.hstack([-np.ones(n), np.zeros(n)])

# SCS data
data = dict(A=A, b=b, c=c)
# ep is exponential cone (primal), with n triples
cone = dict(z=1, l=p, ep=n)

# Setup workspace
solver = scs.SCS(
    data,
    cone,
)
sol = solver.solve()
x_scs = sol["x"][-n:]

# Verify solution with CVXPY
try:
    import cvxpy as cp
except ImportError():
    print("This example requires CVXPY installed to run.")
    raise

x = cp.Variable(shape=n)
obj = cp.Maximize(cp.sum(cp.entr(x)))
constraints = [cp.sum(x) == 1, Ad @ x >= bd]
prob = cp.Problem(obj, constraints)
prob.solve(solver=cp.ECOS)
x_cvxpy = x.value

print(f"CVXPY optimal value is:", prob.value)
print(f"Solution norm difference: {np.linalg.norm(x_scs - x_cvxpy, np.inf)}")

How to reproduce

As above.

Output

Traceback (most recent call last):
  File "test.py", line 46, in <module>
    solver = scs.SCS(
AttributeError: module 'scs' has no attribute 'SCS'
bodono commented 2 years ago

Thanks @ZedongPeng for reminding me. This is because I haven't cut a new pip release for the latest version of SCS on python. I will do that now. The example is valid for SCS v3.2.0, but currently pip is at 3.1.0. You can also install directly from source and have it up and running right away.

ZedongPeng commented 2 years ago

Thanks for your reply @bodono . I'll try installing SCS from the source. Btw, since you have begun working on the pip release, when will SCS v3.2.0 be available on pypi? I am just curious.

bodono commented 2 years ago

It's up now: https://pypi.org/project/scs/

ZedongPeng commented 2 years ago

Amazing. Thanks @bodono