cotengra.experimental.hyper_scipy¶
Hyper optimization using scipy gradient-free optimizers.
Supported methods: differential_evolution, dual_annealing,
direct, shgo. Since these optimizers use a callback-style
objective, a background thread is used to invert the control flow into
an ask/tell interface. Multiple workers are maintained per method to
support parallel pre-dispatch (ask-ask-…-tell-tell-…).
Attributes¶
Exceptions¶
Raised inside the objective to abort the scipy optimizer. |
Classes¶
Ask/tell wrapper around a scipy global optimizer. |
|
Per-method optimizer that wraps a pool of |
|
Hyper-optimization using scipy gradient-free optimizers with |
Module Contents¶
- cotengra.experimental.hyper_scipy._OPTIMIZER_NAMES¶
- exception cotengra.experimental.hyper_scipy._StopOptimization¶
Bases:
ExceptionRaised inside the objective to abort the scipy optimizer.
- class cotengra.experimental.hyper_scipy.ScipyAskTell(method, bounds, **kwargs)¶
Ask/tell wrapper around a scipy global optimizer.
The optimizer runs in a background thread. Each time it needs an objective evaluation it posts the candidate vector to
_ask_qand blocks on_tell_q. The caller drives progress by alternatingask()/tell()calls from the main thread.- Parameters:
- method¶
- bounds¶
- kwargs¶
- _ask_q¶
- _tell_q¶
- _thread = None¶
- _stop¶
- done = False¶
- _get_optimizer_fn()¶
- _objective(x)¶
- _run()¶
- start()¶
Launch the optimizer in a background thread.
- ask()¶
Block until the optimizer requests an evaluation.
- Returns:
x – The candidate vector, or
Noneif the optimizer finished.- Return type:
ndarray or None
- tell(score)¶
Provide the objective value back to the optimizer.
- stop()¶
Signal the background thread to stop and wait for it.
- class cotengra.experimental.hyper_scipy.HyperScipySampler(space, method='differential_evolution', n_workers=1, exponential_param_power=None, **kwargs)¶
Per-method optimizer that wraps a pool of
ScipyAskTellworkers with theParam-based space mapping.Each
ScipyAskTellworker is strictly serial (ask-tell-ask-tell), so to support parallel pre-dispatch (multipleaskcalls before anytell), we maintain a pool of workers. Eachaskgrabs a candidate from the next idle worker; eachtellfeeds the score back to the specific worker that produced that candidate.- Parameters:
- params¶
- _ndim¶
- _method = 'differential_evolution'¶
- _scipy_opts¶
- _n_workers = 1¶
- _trial_counter = 0¶
- _trial_to_worker¶
- _workers = []¶
- _worker_idx = 0¶
- _make_worker()¶
Create and start a fresh
ScipyAskTellinstance.
- _next_worker()¶
Pick the next worker in round-robin order, restarting any that have finished.
- ask()¶
Return
(trial_number, params_dict).Picks the next idle worker (round-robin), blocking until it has a candidate ready. If a worker has converged, it is automatically restarted.
- tell(trial_number, score)¶
Report a score back to the specific worker that produced this trial.
- stop()¶
Stop all background threads.
- class cotengra.experimental.hyper_scipy.ScipyOptLib¶
Bases:
cotengra.experimental.hyper.HyperOptLibHyper-optimization using scipy gradient-free optimizers with an LCB method selector.
- setup(methods, space, optimizer=None, method='differential_evolution', method_exploration=1.0, method_temperature=1.0, exponential_param_power=None, **scipy_opts)¶
Initialize per-method scipy optimizers.
- Parameters:
methods (list[str]) – The contraction methods to optimize over.
optimizer (HyperOptimizer, optional) – The parent optimizer instance.
method (str, optional) – Which scipy global optimizer to use. One of
'differential_evolution','dual_annealing','direct','shgo'.method_exploration (float, optional) – Exploration parameter for the LCB method selector.
method_temperature (float, optional) – Temperature parameter for the LCB method selector.
exponential_param_power (float, optional) – Power for
ParamFloatExp.scipy_opts – Extra keyword arguments forwarded to the scipy optimizer.
- get_setting()¶
- report_result(setting, trial, score)¶
- cleanup()¶
Stop all background optimizer threads.