cotengra.hyperoptimizers.hyper_random¶
Hyper optimization using random or Latin Hypercube sampling.
Classes¶
Latin Hypercube Sampled random space. Pre-generates |
|
Random parameter sampler with optional LHS warm-up. |
|
Random sampling optimizer with optional Latin Hypercube warm-up. |
Functions¶
|
|
|
|
|
|
|
|
|
Module Contents¶
- class cotengra.hyperoptimizers.hyper_random.LHSRandomSpace(space, n, seed=None)[source]¶
Latin Hypercube Sampled random space. Pre-generates
nsamples with stratified coverage over each parameter’s native domain. For continuous parameters (FLOAT, FLOAT_EXP, INT) the range is divided intonequal strata with one sample per stratum. For categorical parameters (STRING, BOOL) the options are cycled through in balanced fashion and randomly permuted. Once all pre-generated samples are exhausted, falls back to pure random sampling.- Parameters:
- rng¶
- _n¶
- _params¶
- _fallback¶
- _samples = []¶
- _stratify_float(n, lo, hi)[source]¶
Divide
[lo, hi]intonequal strata, sample one uniform point per stratum, then randomly permute.
- _stratify_int(n, lo, hi)[source]¶
Stratify an integer range
[lo, hi].The continuous interval
[lo, hi + 1)is divided intonstrata, a uniform float is drawn per stratum, and then floored to an integer (clamped tohi).
- class cotengra.hyperoptimizers.hyper_random.RandomSampler(methods, spaces, n_samples=None, seed=None)[source]¶
Random parameter sampler with optional LHS warm-up.
- Parameters:
methods (list[str]) – The contraction methods to sample from.
spaces (dict[str, dict[str, dict]]) – The per-method search spaces.
n_samples (int or None, optional) – If given, use Latin Hypercube Sampling for the first
n_samplescalls per method, then fall back to pure random. IfNone, use pure random sampling throughout.seed (None or int or random.Random, optional) – Random seed.
- rng¶
- _rmethods¶
- class cotengra.hyperoptimizers.hyper_random.RandomOptLib[source]¶
Bases:
cotengra.hyperoptimizers.hyper.HyperOptLibRandom sampling optimizer with optional Latin Hypercube warm-up.
When
lhs=True(the default), the first batch of samples per method uses Latin Hypercube Sampling for better coverage of the search space. The batch size is derived from the parent optimizer’smax_repeatsattribute divided by the number of methods. After the LHS batch is exhausted, sampling falls back to pure random.- setup(methods, space, optimizer=None, lhs=False, seed=None, **kwargs)[source]¶
Initialize random sampling.
- Parameters:
methods (list[str]) – The list of contraction methods to sample from.
optimizer (HyperOptimizer, optional) – The parent optimizer instance, used for
max_repeats.lhs (bool, optional) – Whether to use Latin Hypercube Sampling for the initial batch. Default
True.seed (None or int, optional) – Random seed.