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

_StopOptimization

Raised inside the objective to abort the scipy optimizer.

Classes

ScipyAskTell

Ask/tell wrapper around a scipy global optimizer.

HyperScipySampler

Per-method optimizer that wraps a pool of ScipyAskTell

ScipyOptLib

Hyper-optimization using scipy gradient-free optimizers with

Module Contents

cotengra.experimental.hyper_scipy._OPTIMIZER_NAMES
exception cotengra.experimental.hyper_scipy._StopOptimization

Bases: Exception

Raised 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_q and blocks on _tell_q. The caller drives progress by alternating ask() / tell() calls from the main thread.

Parameters:
  • method (str) – One of the supported scipy optimizer names.

  • bounds (list[tuple[float, float]]) – Bounds for every raw dimension.

  • kwargs – Forwarded to the underlying scipy optimizer.

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 None if 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 ScipyAskTell workers with the Param-based space mapping.

Each ScipyAskTell worker is strictly serial (ask-tell-ask-tell), so to support parallel pre-dispatch (multiple ask calls before any tell), we maintain a pool of workers. Each ask grabs a candidate from the next idle worker; each tell feeds the score back to the specific worker that produced that candidate.

Parameters:
  • space (dict[str, dict]) – The search space for one method.

  • method (str) – Which scipy optimizer to use.

  • n_workers (int, optional) – Number of concurrent ScipyAskTell threads to run.

  • exponential_param_power (float, optional) – Power for ParamFloatExp.

  • kwargs – Extra keyword arguments forwarded to the scipy optimizer.

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 ScipyAskTell instance.

_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.HyperOptLib

Hyper-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.

  • space (dict[str, dict[str, dict]]) – The search space.

  • 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.