5.10. UDFBase

class UDFBase

The base class for user-defined functions. A subclass must implement arguments() and eval(), plus either argmin() (proximal operator path) or grad() (gradient path).

Methods

arguments()

Method to get function argument list. Each argument should be a Var or a non-const expression.

eval()

Method to evaluate this function.

grad()

Compute the gradient of the function at the given point.

argmin()

Method to minimize function value.

arguments()

Method to get function argument list. Each argument should be a Var or a non-const expression.

return:

Type: list

A list of Var or Expr objects that this UDF operates on.

eval(tensorlist: list)

Method to evaluate this function.

For indicator functions, return 0.0 if feasible and float(“inf”) otherwise.

param tensorlist:

Type: list

List of numpy.ndarray . Each one is a value for corresponding variable associated.

return:

Type: float

The function value. For indicator functions, return 0.0 if feasible and float(“inf”) otherwise.

grad(tensorlist: list)

Compute the gradient of the function at the given point.

This is an alternative to argmin: implement grad instead of argmin for smooth functions where the proximal operator has no convenient closed form. The C++ backend solves the proximal subproblem automatically using gradient descent with Armijo backtracking line search.

A UDF must implement either argmin or grad, not both.

param tensorlist:

Type: list

List of numpy.ndarray , the current point at which to evaluate the gradient. Each entry corresponds to the matching variable in arguments().

return:

Type: list

A list of arrays (one per variable in arguments()), where each array has the same shape as the corresponding input and contains the partial derivatives of the function with respect to that variable.

argmin(lamb: float, tensorlist: list)

Method to minimize function value.

The implementation must solve: argmin_x { lamb * f(x) + (1/2)||x - v||^2 }, where v is the current iterate passed via tensorlist.

For indicator functions, the argmin reduces to projection onto the feasible set.

param lamb:

Type: float

The penalty weight (rho) provided by the ADMM iteration. The argmin method must solve: argmin_x { lamb * f(x) + (1/2)||x - v||^2 }.

param tensorlist:

Type: list

List of numpy.ndarray , the current iterate values for each associated variable.

return:

Type: list

The variable value list that minimizes the proximal subproblem, or None to stop the optimization.