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.
Problemdescribes 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
funis the objective function,xis the variable to optimize,xlandxuare the lower and upper bounds,aubandbubare the coefficient matrix and right-hand side vector of the linear inequality constraints,aeqandbeqare the coefficient matrix and right-hand side vector of the linear equality constraints,cubis the function of nonlinear inequality constraints, andceqis the function of nonlinear equality constraints.- Parameters:
- funcallable
Objective function to be minimized:
fun(x) -> float, wherexis 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.inffor each component.- xuarray_like, shape (n,), optional
Upper bounds on the variables
x <= xu. Default isnumpy.inffor 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 equalnand the number of rows must equalm_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 incub. 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
FeatureFeature applied to problems during benchmarking.
FeaturedProblemProblem equipped with a specific feature.
benchmarkMain 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
Problemfor 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
cubatx0. Nonlinear equality constraints can be specified in a similar way usingceq.- Attributes:
namestrName 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.ptypestrType of the problem.
nintDimension of the problem.
mbintNumber of bound constraints.
m_linear_ubintNumber of linear inequality constraints.
m_linear_eqintNumber of linear equality constraints.
m_nonlinear_ubintNumber of nonlinear inequality constraints.
m_nonlinear_eqintNumber of nonlinear equality constraints.
mlconintTotal number of linear constraints (inequality and equality).
mnlconintTotal number of nonlinear constraints (inequality and equality).
mconintTotal 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 ofmax(xl - x, 0),max(x - xu, 0),max(aub @ x - bub, 0),aeq @ x - beq,max(cub(x), 0), andceq(x).project_x0()
Attempt to project the initial guess
x0onto the feasible region (may fail).