pyglobalsearch.PyProblem#
- class pyglobalsearch.PyProblem(objective, variable_bounds, gradient=None, hessian=None, constraints=None)#
Bases:
objectDefines an optimization problem to be solved.
A PyProblem encapsulates all the mathematical components needed for optimization: the objective function to minimize, variable bounds, and optional gradient, Hessian, and constraint functions.
- Parameters:
objective (callable) – Function that takes x (array-like) and returns the value to minimize (float)
variable_bounds (callable) – Function that returns bounds array of shape (n_vars, 2) with [lower, upper] bounds
gradient (callable, optional) – Function that takes x and returns gradient array (for gradient-based solvers)
hessian (callable, optional) – Function that takes x and returns Hessian matrix (for Newton-type solvers)
constraints (list[callable], optional) – List of constraint functions where constraint(x) >= 0 means satisfied
Examples#
Basic unconstrained problem:
>>> def objective(x): return x[0]**2 + x[1]**2 >>> def bounds(): return np.array([[-5, 5], [-5, 5]]) >>> problem = gs.PyProblem(objective, bounds)
Problem with gradient for faster convergence:
>>> def gradient(x): return np.array([2*x[0], 2*x[1]]) >>> problem = gs.PyProblem(objective, bounds, gradient=gradient)
Constrained problem (requires COBYLA solver):
>>> def constraint(x): return x[0] + x[1] - 1 # x[0] + x[1] >= 1 >>> problem = gs.PyProblem(objective, bounds, constraints=[constraint])
- __init__()#
Methods
__init__()Attributes
List of constraint functions
Function returning the gradient
Function returning the Hessian matrix
Objective function to minimize
Function returning variable bounds
- constraints#
List of constraint functions
- Parameters:
x – Input parameters as a list or array of floats
- Returns:
Constraint value (float), should be >= 0 to be satisfied
- Raises:
ValueError – If any constraint function does not return a float
- gradient#
Function returning the gradient
- Parameters:
x – Input parameters as a list or array of floats
- Returns:
Gradient as a list or array of floats
- Raises:
ValueError – If the gradient length does not match the number of variables
- hessian#
Function returning the Hessian matrix
- Parameters:
x – Input parameters as a list or array of floats
- Returns:
Hessian matrix as a 2D list or array of floats
- Raises:
ValueError – If the Hessian is not square or does not match the number of variables
- objective#
Objective function to minimize
- Parameters:
x – Input parameters as a list or array of floats
- Returns:
Objective function value (float)
- variable_bounds#
Function returning variable bounds
- Returns:
2D array-like of shape (n_vars, 2) with [lower, upper] bounds for each variable
- Return type:
array-like