cvxgrp / cvxpylayers

Differentiable convex optimization layers
Apache License 2.0
1.82k stars 162 forks source link

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

Open Chris-Sorini opened 1 year ago

Chris-Sorini commented 1 year ago

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:

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 commented 1 year ago

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

For now I switched to cvxpy==1.2.3

bamos commented 1 year ago

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 https://github.com/cvxgrp/cvxpylayers/pull/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 commented 1 year ago

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]