Skip to content

Support for CallbackParam #164

Open
Open
@bastienlc

Description

@bastienlc

cvxpy provides a feature called CallbackParam that allows operations on parameters that would not be permissible under the DPP ruleset. Consider the example below: we normally cannot multiply the matrices A1 and A2, but are able to do so with CallbackParam.

import cvxpy as cp


def create_problem_without_callback(k: int, n: int, m: int):
    A1 = cp.Parameter((n, k))
    A2 = cp.Parameter((k, m))
    B = cp.Parameter(n)
    X = cp.Variable(m)

    problem = cp.Problem(cp.Minimize(cp.norm((A1 @ A2) @ X + B, 2) + cp.norm(X, 1)))
    assert problem.is_dcp(dpp=True)


def create_problem_with_callback(k: int, n: int, m: int):
    A1 = cp.Parameter((n, k))
    A2 = cp.Parameter((k, m))
    A = cp.CallbackParam(shape=(n, m), callback=lambda: A1 @ A2)
    B = cp.Parameter(n)
    X = cp.Variable(m)

    problem = cp.Problem(cp.Minimize(cp.norm(A @ X + B, 2) + cp.norm(X, 1)))
    assert problem.is_dcp(dpp=True)


if __name__ == "__main__":
    create_problem_without_callback(2, 3, 4)  # Assertion Error
    create_problem_with_callback(2, 3, 4)  # OK

This feature might be useful if you don't want to compute A1 @ A2 ahead of time for instance.

From my experiments it looks like cvxpylayers does not support CallbackParam; either it does not recognize the set of parameters of the problem correctly, or it does not compute the callback.

I think supporting this feature would be pretty straightforward.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions