optiprofiler.Problem

Contents

optiprofiler.Problem#

class optiprofiler.Problem(fun, x0, name=None, xl=None, xu=None, aub=None, bub=None, aeq=None, beq=None, cub=None, ceq=None, grad=None, hess=None, jcub=None, jceq=None, hcub=None, hceq=None)[source]#

Optimization problem to be used in the benchmarking.

Problem describes an optimization problem with the following structure:

\[\begin{split}\min \quad & \mathrm{fun}(x) \\ \text{s.t.} \quad & x_l \le x \le x_u, \\ & A_{\mathrm{ub}} x \le b_{\mathrm{ub}}, \\ & A_{\mathrm{eq}} x = b_{\mathrm{eq}}, \\ & c_{\mathrm{ub}}(x) \le 0, \\ & c_{\mathrm{eq}}(x) = 0, \\ & \text{with initial point } x_0,\end{split}\]

where fun is the objective function, x is the variable to optimize, xl and xu are the lower and upper bounds, aub and bub are the coefficient matrix and right-hand side vector of the linear inequality constraints, aeq and beq are the coefficient matrix and right-hand side vector of the linear equality constraints, cub is the function of nonlinear inequality constraints, and ceq is the function of nonlinear equality constraints.

Parameters:
funcallable

Objective function to be minimized: fun(x) -> float, where x is an array with shape (n,).

x0array_like, shape (n,)

Initial guess.

namestr, optional

Name of the problem. Default is 'Unnamed Problem'.

xlarray_like, shape (n,), optional

Lower bounds on the variables xl <= x. Default is -numpy.inf for each component.

xuarray_like, shape (n,), optional

Upper bounds on the variables x <= xu. Default is numpy.inf for each component.

aubarray_like, shape (m_linear_ub, n), optional

Coefficient matrix of the linear inequality constraints aub @ x <= bub. Default is an empty matrix.

bubarray_like, shape (m_linear_ub,), optional

Right-hand side of the linear inequality constraints aub @ x <= bub. Default is an empty vector.

aeqarray_like, shape (m_linear_eq, n), optional

Coefficient matrix of the linear equality constraints aeq @ x == beq. Default is an empty matrix.

beqarray_like, shape (m_linear_eq,), optional

Right-hand side of the linear equality constraints aeq @ x == beq. Default is an empty vector.

cubcallable, optional

Nonlinear inequality constraints cub(x) <= 0: cub(x) -> array_like, shape (m_nonlinear_ub,). Default returns an empty array.

ceqcallable, optional

Nonlinear equality constraints ceq(x) == 0: ceq(x) -> array_like, shape (m_nonlinear_eq,). Default returns an empty array.

gradcallable, optional

Gradient of the objective function: grad(x) -> array, shape (n,). Default returns an empty array.

hesscallable, optional

Hessian of the objective function: hess(x) -> array, shape (n, n). Default returns an empty matrix.

jcubcallable, optional

Jacobian of the nonlinear inequality constraints: jcub(x) -> array, shape (m_nonlinear_ub, n). The number of columns must equal n and the number of rows must equal m_nonlinear_ub. Default returns an empty matrix.

jceqcallable, optional

Jacobian of the nonlinear equality constraints: jceq(x) -> array, shape (m_nonlinear_eq, n). Default returns an empty matrix.

hcubcallable, optional

Hessians of the nonlinear inequality constraints: hcub(x) -> list of arrays, each shape (n, n). The i-th element is the Hessian of the i-th constraint in cub. Default returns an empty list.

hceqcallable, optional

Hessians of the nonlinear equality constraints: hceq(x) -> list of arrays, each shape (n, n). Default returns an empty list.

See also

Feature

Feature applied to problems during benchmarking.

FeaturedProblem

Problem equipped with a specific feature.

benchmark

Main benchmarking function.

Examples

Consider the unconstrained problem of minimizing the Rosenbrock function

\[f(x) = 100 (x_2 - x_1^2)^2 + (1 - x_1)^2.\]

To create an instance of the class Problem for this problem:

from optiprofiler import Problem

def rosen(x):
    return 100 * (x[1] - x[0] ** 2) ** 2 + (1 - x[0]) ** 2

problem = Problem(rosen, [0, 0])

The second argument [0, 0] is the initial guess. This instance can now be used to evaluate the objective function at any point and access extra information about the problem:

problem.fun(problem.x0)  # returns 1.0
problem.xl               # array([-inf, -inf])
problem.xu               # array([inf, inf])

The optional arguments of the constructor can be used to specify constraints. For example, to specify that the variables must be nonnegative:

problem = Problem(rosen, [0, 0], xl=[0, 0])
problem.xl  # array([0., 0.])

Nonlinear inequality constraints can be specified using cub. For example, to require \(x_1^2 + x_2^2 \le 1\) and \(x_1^3 - x_2^2 \le 1\):

def cub(x):
    return [x[0] ** 2 + x[1] ** 2 - 1, x[0] ** 3 - x[1] ** 2 - 1]

problem = Problem(rosen, [0, 0], cub=cub)
problem.cub(problem.x0)  # array([-1., -1.])

The number of nonlinear inequality constraints is inferred automatically from the return value of cub at x0. Nonlinear equality constraints can be specified in a similar way using ceq.

Attributes:
namestr

Name of the problem.

x0numpy.ndarray, shape (n,)

Initial guess.

xlnumpy.ndarray, shape (n,)

Lower bounds on the variables.

xunumpy.ndarray, shape (n,)

Upper bounds on the variables.

aubnumpy.ndarray, shape (m_linear_ub, n)

Coefficient matrix of the linear constraints aub @ x <= bub.

bubnumpy.ndarray, shape (m_linear_ub,)

Right-hand side of the linear constraints aub @ x <= bub.

aeqnumpy.ndarray, shape (m_linear_eq, n)

Coefficient matrix of the linear constraints aeq @ x == beq.

beqnumpy.ndarray, shape (m_linear_eq,)

Right-hand side of the linear constraints aeq @ x == beq.

ptypestr

Type of the problem.

nint

Dimension of the problem.

mbint

Number of bound constraints.

m_linear_ubint

Number of linear inequality constraints.

m_linear_eqint

Number of linear equality constraints.

m_nonlinear_ubint

Number of nonlinear inequality constraints.

m_nonlinear_eqint

Number of nonlinear equality constraints.

mlconint

Total number of linear constraints (inequality and equality).

mnlconint

Total number of nonlinear constraints (inequality and equality).

mconint

Total number of constraints (linear and nonlinear).

Methods

fun(x)

Evaluate the objective function.

grad(x)

Evaluate the gradient of the objective function.

hess(x)

Evaluate the Hessian of the objective function.

cub(x)

Evaluate the nonlinear inequality constraints.

ceq(x)

Evaluate the nonlinear equality constraints.

jcub(x)

Evaluate the Jacobian of the nonlinear inequality constraints.

jceq(x)

Evaluate the Jacobian of the nonlinear equality constraints.

hcub(x)

Evaluate the Hessians of the nonlinear inequality constraints.

hceq(x)

Evaluate the Hessians of the nonlinear equality constraints.

maxcv(x)

Compute the maximum constraint violation at x, defined as the maximum of the infinity norms of max(xl - x, 0), max(x - xu, 0), max(aub @ x - bub, 0), aeq @ x - beq, max(cub(x), 0), and ceq(x).

project_x0()

Attempt to project the initial guess x0 onto the feasible region (may fail).