Skip to content

User-defined Functions (Callbacks) #113

@brunoml5

Description

@brunoml5

The idea for this feature is to allow the user to supply custom functions for writing the expressions used on the optimization. This would allow the user to mix expressions written in cvxpy with Callbacks to user-defined functions.

The idea of the Callback is to provide some kind of API that the user can wrap the function in an object. The user should supply methods for initialization (__init__), for evaluating y = f(x), and crucially, should supply a method for evaluating the sensitivity (gradient/jacobian, and maybe hessian).

Another important info that the user could provide is the sparsity of the outputs and inputs. But this is not critical for an initial implementation.

I think this feature is feasible if the Callback function is constrained to be on the "leafs" of the expressions. In other words, x in y = f(x) can only be pure cvxpy Variable, and the output y is only used in affine expressions. With this constraint, it shouldnt be a problem to build the expression tree.

On Discord, @Transurgeon has commented the following: "I am envisioning some sort of "callback" atom, where users can define their own methods and values, and that might just work!"

Is your feature request related to a problem? Please describe.
No.

Describe the solution you'd like
A good initial solution/result would be something like y = f(x), where x are pure cvxpy Variable. Then, y could be used on the objective/constraints with affine expressions (i.e. basically +).

Describe alternatives you've considered
I havent thought much about alternatives solutions.

Additional context
I got this idea from a feature available in CasADi called Callback functions. This feature is what enables embedding functions from other libraries like Tensorflow (check this blog post).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions