cvxpylayers icon indicating copy to clipboard operation
cvxpylayers copied to clipboard

[Feature request] Hide unused parameters as constants

Open JinraeKim opened this issue 3 years ago • 0 comments
trafficstars

I'd like to change the value of "Parameter" x for efficient multiple solves, which is not in the scope of cvxpylayer. Instead, A is the Parameter for cvxpylayer. MWE (minimum working example) is as follows.

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


if __name__ == "__main__":
    n, m = 3, 2
    x = cp.Parameter((1, n))
    A = cp.Parameter((n, m))
    A_tch = torch.randn((n, m))
    u = cp.Variable((1, m))
    obj = cp.Minimize(x @ A @ u.T)
    constraints = [cp.norm(u) <= 1]
    prob = cp.Problem(obj, constraints)
    x.value = np.random.randn(1, n)
    cvxpylayer = CvxpyLayer(prob, parameters=[A], variables=[u])
    sol, = cvxpylayer(A_tch)  # DPP error

Even only injecting A into CvxpyLayer, the MWE gave me an error as it's not DPP.

Traceback (most recent call last):
  File "/Users/jinrae/github/other-projects/parametrised-convex-q-learning/tmp/tmp.py", line 17, in <module>
    cvxpylayer = CvxpyLayer(prob, parameters=[A], variables=[u])
  File "/Users/jinrae/Library/Caches/pypoetry/virtualenvs/parametrised-convex-q-learning-2h6IOcxJ-py3.9/lib/python3.9/site-packages/cvxpylayers/torch/cvxpylayer.py", line 79, in __init__
    raise ValueError('Problem must be DPP.')
ValueError: Problem must be DPP.

Can the CvxpyLayer hide unused cvxpy Parameters as constants for this usage?

JinraeKim avatar Feb 12 '22 07:02 JinraeKim