pyglobalsearch.optimize#
- pyglobalsearch.optimize(problem, params, observer=None, local_solver=None, local_solver_config=None, seed=None, target_objective=None, max_time=None, verbose=None, exclude_out_of_bounds=None, parallel=None)#
Perform global optimization on the given problem.
This function implements the OQNLP (OptQuest/NLP) algorithm, which combines scatter search metaheuristics with local optimization to find global minima of nonlinear problems. It’s particularly effective for multi-modal functions with multiple local minima.
The algorithm works in two stages:
Scatter search to explore the parameter space and identify promising regions
Local optimization from multiple starting points to refine solutions
- Parameters:
problem (PyProblem) – The optimization problem to solve (objective, bounds, constraints, etc.)
params (PyOQNLPParams) – Parameters controlling the optimization algorithm behavior
observer (PyObserver, optional) – Optional observer for tracking algorithm progress and metrics
local_solver (str, optional) – Local optimization algorithm (“COBYLA”, “LBFGS”, “NewtonCG”, “TrustRegion”, “NelderMead”, “SteepestDescent”)
local_solver_config (object, optional) – Custom configuration for the local solver (must match solver type)
seed (int, optional) – Random seed for reproducible results
target_objective (float, optional) – Stop optimization when this objective value is reached
max_time (float, optional) – Maximum time in seconds for Stage 2 optimization (unlimited if None)
verbose (bool, optional) – Print progress information during optimization
exclude_out_of_bounds (bool, optional) – Filter out solutions that violate bounds
parallel (bool, optional) – Enable parallel processing using rayon (default: False)
- Returns:
A set of local solutions found during optimization
- Return type:
- Raises:
ValueError – If solver configuration doesn’t match the specified solver type, or if the problem is not properly defined
Examples#
Basic optimization:
>>> result = gs.optimize(problem, params) >>> best = result.best_solution()
With observer for progress tracking:
>>> observer = gs.observers.Observer().with_stage1_tracking().with_stage2_tracking().with_default_callback() >>> result = gs.optimize(problem, params, observer=observer)
With custom solver configuration:
>>> cobyla_config = gs.builders.cobyla(max_iter=1000) >>> result = gs.optimize(problem, params, ... local_solver="COBYLA", ... local_solver_config=cobyla_config)
With early stopping:
>>> result = gs.optimize(problem, params, ... target_objective=-1.0316, # Stop when reached ... max_time=60.0, # Max 60 seconds ... verbose=True) # Show progress
Enable parallel processing:
>>> result = gs.optimize(problem, params, parallel=True)