Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cvxpy and cvxpylayers solutions disagree for cvxpy==1.3.0 and cvxpylayers==0.1.4 #143

Open
Chris-Sorini opened this issue Mar 7, 2023 · 3 comments

Comments

@Chris-Sorini
Copy link

In an anaconda environment with cvxpy==1.3.0 and cvxpylayers==0.1.4 I noticed that cvxpylayers and cvxpy result in different solutions for the following problem:

x = cp.Variable(2)
xc = cp.Parameter(2)
xc.value = np.array([1.0, 2.5])
xc_torch = torch.tensor([1.0, 2.5])
obj = cp.Minimize((x[0] - xc[0])**2 + (x[1] - xc[1])**2)
cons = [(x[0] - 2 * x[1] + 2) >= 0 ,
(-x[0] - 2 * x[1] + 6 >= 0),
(-x[0] + 2 * x[1] + 2) >=0]
prob = cp.Problem(obj, cons)
cvxpylayer = CvxpyLayer(problem=prob, parameters=[xc], variables=[x])

prob.solve(solver_args={"solve_method": "ECOS"}) outputs 0.8
for i in prob.variables(): print(i.value) outputs the optimal variable values: [1.4, 1.7]

cvxpylayer(xc_torch, solver_args={"solve_method": "ECOS"}) outputs:

  • (tensor([2.9926, 1.4963]),) with cvxpy==1.3.0 (w/ cvxpylayers==0.1.4)
  • (tensor([1.4000, 1.7000]),) with cvxpy==1.2.3 (w/ cvxpylayers==0.1.4)

The solutions agree when I use cvxpy==1.2.3, but not with cvxpy==1.3.0 (in both cases using cvxpylayers==0.1.4). Also note that the example on the github readme for cvxpylayers did not show the same issue for either cvxpy==1.2.3 or 1.3.0. I am going to stick with cvxpy==1.2.3 for now, but does anyone know what is causing this issue for cvxpy==1.3.0? Thanks!

@jtorde
Copy link

jtorde commented Mar 22, 2023

I am having the same issue (with a different optimization problem):

  • cvxpylayers==0.1.5 and cvxpy==1.2.3 works (both results agree)
  • cvxpylayers==0.1.5 and cvxpy==1.3.0 generate different results

For now I switched to cvxpy==1.2.3

@bamos
Copy link
Collaborator

bamos commented Mar 22, 2023

Hi, this may be due to cvxpylayers/cvxpy defaulting to using SCS with a quadratic objective without having full support for it (which I believe may be the difference between 1.2.3 and 1.3.0 here). We just merged #142 into the main branch of cvxpylayers to disable this behavior. Can you please try running this latest version of cvxpylayers (in the main branch here) with the latest version of cvxpy (or 1.3.0) and let us know if this is still an issue?

@jtorde
Copy link

jtorde commented May 3, 2023

With the latest versions of both cvxpy (cvxpy==1.3.1) and cvxpylayers (cvxpylayers=0.1.6) this issue seems to be fixed:

import cvxpy as cp
import numpy as np
import torch
from cvxpylayers.torch import CvxpyLayer

x = cp.Variable(2)
xc = cp.Parameter(2)
xc.value = np.array([1.0, 2.5])
xc_torch = torch.tensor([1.0, 2.5])
obj = cp.Minimize((x[0] - xc[0])**2 + (x[1] - xc[1])**2)
cons = [(x[0] - 2 * x[1] + 2) >= 0 ,
(-x[0] - 2 * x[1] + 6 >= 0),
(-x[0] + 2 * x[1] + 2) >=0]
prob = cp.Problem(obj, cons)
cvxpylayer = CvxpyLayer(problem=prob, parameters=[xc], variables=[x])

prob.solve(solver='ECOS')

for i in prob.variables(): 
		print(i.value) 

z=cvxpylayer(xc_torch, solver_args={"solve_method": "ECOS"})
print(z)

Both return [1.4, 1.7]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants