optim_minimize
optim_minimize(
objective,
lower=None,
upper=None,
initial=None,
method='de',
seed=None,
control=None,
gradient=None,
constraints=None,
inner=None,
)Minimize a user-written objective.
Runs one of the fourteen ported Numerics optimizers over a Python function. The optimizer’s random number generator lives in C++, so a seeded run reproduces exactly, and reproduces identically in corehydror.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| objective | callable | Takes a numeric parameter vector (:class:numpy.ndarray) and returns a single number. |
required |
| lower | array_like | Parameter bounds, the same length as the parameter vector. Required for every method, including "de", "brent" and "golden_section", which take no initial. "brent" and "golden_section" are one-dimensional: pass a single bound each. |
None |
| upper | array_like | Parameter bounds, the same length as the parameter vector. Required for every method, including "de", "brent" and "golden_section", which take no initial. "brent" and "golden_section" are one-dimensional: pass a single bound each. |
None |
| initial | array_like | Starting values, the same length as lower/upper. Required for "bfgs", "powell", "mlsl", "multi_start", "adam", "gradient_descent" and "nelder_mead". |
None |
| method | ('de', 'particle_swarm', 'sce', 'simulated_annealing', 'multi_start', 'mlsl', 'bfgs', 'powell', 'adam', 'gradient_descent', 'nelder_mead', 'brent', 'golden_section', 'augmented_lagrange') | "de" (differential evolution) is the default. The five global methods are "de", "particle_swarm", "sce" (shuffled complex evolution), "simulated_annealing" and "multi_start"; "mlsl" is multi-level single linkage; "augmented_lagrange" is the one constrained method; the rest are local. |
"de" |
| seed | int | Seed for the stochastic methods ("de", "particle_swarm", "sce", "simulated_annealing", "multi_start", "mlsl"); an error for any other method. |
None |
| control | dict | Optimizer settings. Every method accepts max_iterations, absolute_tolerance and relative_tolerance. Every method except "nelder_mead" and "brent" (the two classes that do not derive from the ported Optimizer base) additionally accepts max_function_evaluations, report_failure (default True, which surfaces a configuration failure as a Python exception rather than returning a failed status quietly) and compute_hessian. The method-specific settings are population_size ("de", "particle_swarm"); complexes, cce_iterations and tolerance_steps ("sce"); initial_temperature, min_temperature, cooling_rate, update_cycles, temperature_cycles and tolerance_steps ("simulated_annealing"); local_method ("multi_start", "mlsl", one of "bfgs", "nelder_mead", "powell"); local_absolute_tolerance, local_relative_tolerance, polish ("multi_start"); alpha, the step size or learning rate ("adam", "gradient_descent"); and beta1, beta2, the two decay factors ("adam"). Passing a setting a method does not read is an error rather than a silent no-op. compute_hessian DEFAULTS TO True for the Optimizer-base methods (matching the ported C# Optimizer base), so a successful run returns a Hessian, computed by extra objective evaluations, unless control={"compute_hessian": False} turns it off. |
None |
| gradient | callable | Takes the parameter vector and returns one partial derivative per parameter. Accepted only by "adam" and "gradient_descent", an error for every other method. Omitted, both methods differentiate the objective numerically, exactly as the upstream C# classes do with a null gradient. |
None |
| constraints | list of Constraint | Required by, and accepted only by, method="augmented_lagrange". |
None |
| inner | dict | The inner optimizer the augmented Lagrange method drives, with the keys "method", "initial", "lower", "upper", "seed" and "control". Any vector left out falls back to the top-level one, so {"method": "powell"} is enough. Accepted only by method="augmented_lagrange"; omitted, the inner optimizer is "bfgs" over the top-level initial/lower/upper. The inner method may be any method except "augmented_lagrange" itself and the two standalone classes "nelder_mead"/"brent". |
None |
Returns
| Name | Type | Description |
|---|---|---|
| OptimResult |
Examples
>>> from corehydropy import Constraint, optim_minimize
>>> def rosenbrock(p):
... return (1 - p[0]) ** 2 + 100 * (p[1] - p[0] ** 2) ** 2
>>> fit = optim_minimize(rosenbrock, lower=[-5, -5], upper=[5, 5], seed=42)
>>> import numpy as np
>>> np.round(fit.parameters, 3)
array([1., 1.])Constrained: minimize the same function on the unit disk.
>>> con = Constraint(lambda p: p[0] ** 2 + p[1] ** 2, value=2.0, type="le")
>>> fit = optim_minimize(rosenbrock, initial=[0, 0], lower=[-1.5, -1.5], upper=[1.5, 1.5],
... method="augmented_lagrange", constraints=[con])
>>> np.round(fit.parameters, 3)
array([1., 1.])