cotengra.hyperoptimizers.hyper_neldermead¶
Hyper optimization using the Nelder-Mead simplex method.
Attributes¶
Classes¶
Minimal Nelder-Mead simplex state machine on raw vectors. Manages n+1 |
|
Nelder-Mead simplex optimizer in raw |
|
Hyper-optimization backend using the Nelder-Mead simplex method. |
Functions¶
Module Contents¶
- cotengra.hyperoptimizers.hyper_neldermead._INIT = 'init'¶
- cotengra.hyperoptimizers.hyper_neldermead._REFLECT = 'reflect'¶
- cotengra.hyperoptimizers.hyper_neldermead._EXPAND = 'expand'¶
- cotengra.hyperoptimizers.hyper_neldermead._CONTRACT = 'contract'¶
- cotengra.hyperoptimizers.hyper_neldermead._SHRINK = 'shrink'¶
- cotengra.hyperoptimizers.hyper_neldermead._clip(x, lo=-1.0, hi=1.0)[source]¶
Clip scalar
xto the interval[lo, hi].
- cotengra.hyperoptimizers.hyper_neldermead.clamp(xs, lo=-1.0, hi=1.0)[source]¶
Clip each element of the list
xsto the interval[lo, hi].
- class cotengra.hyperoptimizers.hyper_neldermead._NMCore(ndim, center, scales, adaptive=False, alpha=1.0, gamma=2.0, rho=0.5, sigma=0.5, convergence_tol=0.01, psi=None, inject_diameter_fraction=1.5, inject_restart_fraction=0.6)[source]¶
Minimal Nelder-Mead simplex state machine on raw vectors. Manages n+1 vertices and iteratively improves via reflection, expansion, contraction, and shrink. When the simplex diameter drops below
convergence_toltheconvergedflag is set and no further points are issued.- Parameters:
ndim (int) – Number of raw dimensions.
center (list[float]) – Center point for the initial simplex.
scales (list[float]) – Per-dimension scale for axis-aligned perturbations used to construct the initial simplex around
center.adaptive (bool) – Whether to use the adaptive NM coefficients recommended by Gao and Han (2010), which scale with problem dimension. If True then alpha, gamma, rho, and sigma are ignored.
alpha (float) – Reflection coefficient (standard NM default: 1). If
adaptiveis True then this is ignored and set to 1.0.gamma (float) – Expansion coefficient (standard NM default: 2). If
adaptiveis True then this is ignored and set to1 + 2 / ndim.rho (float) – Contraction coefficient (standard NM default: 0.5). If
adaptiveis True then this is ignored and set to0.75 - 1 / (2 * ndim).sigma (float) – Shrink coefficient (standard NM default: 0.5). If
adaptiveis True then this is ignored and set to1 - 1 / ndim.convergence_tol (float) – When the Chebyshev diameter of the simplex falls below this value the core signals convergence.
psi (float, optional) – Relative simplex reduction target. When set, the core also converges once the simplex diameter has been reduced below
psitimes its initialized diameter. This mirrors the internal convergence mode used by Sbplx in NLopt.inject_diameter_fraction (float) – Maximum allowed simplex diameter inflation when injecting an external vertex. The candidate’s max distance to any non-worst vertex must be at most
inject_diameter_fraction * current_diameter. Use1.0(default) to prevent any inflation,> 1.0to allow some growth, orfloat('inf')to disable the gate entirely.inject_restart_fraction (float) – When injecting an external vertex that is far from the current simplex (beyond the diameter-based gate above), if its score is better than this fraction of the current best score, flag convergence to trigger an early restart focused on the better region. Set to
0.0to disable this behavior.
- ndim¶
- convergence_tol = 0.01¶
- psi = None¶
- inject_diameter_fraction = 1.5¶
- inject_restart_fraction = 0.6¶
- _token_counter = 0¶
- _tell_count = 0¶
- _best_vertex = None¶
- _best_score¶
- _converged = False¶
- _initial_simplex_diameter = None¶
- _vertices = []¶
- _scores = []¶
- _ask_queue = []¶
- _token_map¶
- _results¶
- _state = 'init'¶
- _centroid = None¶
- _reflect_x = None¶
- _reflect_score = None¶
- _contract_inside = False¶
- _pending_injection = None¶
- property converged¶
True when simplex diameter < convergence_tol.
- property best_vertex¶
- property best_score¶
- inject_vertex(x, score)[source]¶
Defer replacement of the worst vertex with an external point. The replacement is applied at the next
_begin_reflectcall so that in-progress NM operations are not disrupted. Only accepted when the simplex is initialized andscoreimproves on the current worst vertex.
- class cotengra.hyperoptimizers.hyper_neldermead.HyperNelderMeadSampler(space, seed=None, adaptive=False, alpha=1.0, gamma=2.0, rho=0.5, sigma=0.5, initial_scale=0.6, restart_tol=0.01, restart_scale=0.5, filler_scale=0.3, n_initial=None, explore_prob=0.05, inject_diameter_fraction=1.5, inject_restart_fraction=0.6, exponential_param_power=None)[source]¶
Nelder-Mead simplex optimizer in raw
[-1, 1]space.Wraps a
_NMCorestate machine and adds parameter mapping, random filler point generation while the core is blocked, and automatic restarts when the simplex converges.- Parameters:
space (dict[str, dict]) – The search space for a single contraction method.
seed (None or int or random.Random, optional) – Random seed.
adaptive (bool, optional) – Whether to use the adaptive NM coefficients recommended by Gao and Han (2010), which scale with problem dimension. If True then alpha, gamma, rho, and sigma are ignored.
alpha (float, optional) – Reflection coefficient.
gamma (float, optional) – Expansion coefficient.
rho (float, optional) – Contraction coefficient.
sigma (float, optional) – Shrink coefficient.
initial_scale (float, optional) – Scale of the initial simplex around the origin.
restart_tol (float, optional) – When the simplex diameter falls below this, restart.
restart_scale (float, optional) – Scale of the restarted simplex around the best point.
filler_scale (float, optional) – Standard deviation of the gaussian noise used for filler points issued while the core Nelder-Mead routine is blocked.
n_initial (int or None, optional) – Number of Latin Hypercube Sampled (LHS) warm-up points to evaluate before starting the simplex. The best result seeds the initial simplex center. Default
Nonemeans2 * ndim. Set to0to disable.explore_prob (float, optional) – Probability of issuing a uniform random point instead of the NM-directed point during normal operation. Maintains diversity throughout the search.
inject_diameter_fraction (float, optional) – Passed to
_NMCore— controls the maximum allowed simplex diameter inflation when injecting an external vertex.exponential_param_power (float, optional) – Passed through to the shared parameter mapping.
- rng¶
- params = []¶
- ndim¶
- adaptive = False¶
- alpha = 1.0¶
- gamma = 2.0¶
- rho = 0.5¶
- sigma = 0.5¶
- initial_scale = 0.6¶
- restart_tol = 0.01¶
- restart_scale = 0.5¶
- filler_scale = 0.3¶
- explore_prob = 0.05¶
- inject_diameter_fraction = 1.5¶
- inject_restart_fraction = 0.6¶
- n_initial = None¶
- _trial_counter = 0¶
- _restart_count = 0¶
- _best_x = None¶
- _best_score¶
- _trial_map¶
- ask()[source]¶
Return the next candidate setting. During the LHS warm-up phase, pre-generated Latin Hypercube points are issued one at a time. Once all warm-up results have been collected, the NM simplex is initialized centered on the best warm-up point. During normal operation, with probability
explore_proba uniform random point is returned to maintain diversity. If the NM state machine is blocked waiting for results, a filler point is returned instead.
- class cotengra.hyperoptimizers.hyper_neldermead.NelderMeadOptLib[source]¶
Bases:
cotengra.hyperoptimizers.hyper.HyperOptLibHyper-optimization backend using the Nelder-Mead simplex method.
- setup(methods, space, optimizer=None, adaptive=False, alpha=1.0, gamma=2.0, rho=0.5, sigma=0.5, initial_scale=0.6, restart_tol=0.01, restart_scale=0.5, filler_scale=0.3, n_initial=None, explore_prob=0.05, inject_diameter_fraction=1.5, inject_restart_fraction=0.6, method_exploration=1.0, method_temperature=1.0, exponential_param_power=None, seed=None, **kwargs)[source]¶
Initialize Nelder-Mead optimizers for each method.
- Parameters:
methods (list[str]) – The contraction methods to optimize over.
space (dict[str, dict[str, dict]]) – The per-method hyperparameter search space.
optimizer (HyperOptimizer, optional) – The parent optimizer.
adaptive (bool, optional) – Whether to use the adaptive NM coefficients recommended by Gao and Han (2010), which scale with problem dimension. If True then alpha, gamma, rho, and sigma are ignored.
alpha (float, optional) – Reflection coefficient.
gamma (float, optional) – Expansion coefficient.
rho (float, optional) – Contraction coefficient.
sigma (float, optional) – Shrink coefficient.
initial_scale (float, optional) – Scale of the initial simplex.
restart_tol (float, optional) – Simplex diameter threshold for restart.
restart_scale (float, optional) – Scale of the restarted simplex.
filler_scale (float, optional) – Gaussian noise scale for filler points.
n_initial (int or None, optional) – Number of LHS warm-up points per method. Default
Nonemeans2 * ndim. Set to0to disable.explore_prob (float, optional) – Probability of issuing a uniform random exploration point during normal operation.
inject_diameter_fraction (float, optional) – Passed to
_NMCore— controls the maximum allowed simplex diameter inflation when injecting an external vertex.method_exploration (float, optional) – Exploration strength for the LCB method chooser.
method_temperature (float, optional) – Noise temperature for the LCB method chooser.
exponential_param_power (float, optional) – Passed to the shared parameter mapping.
seed (None or int, optional) – Random seed.