Table of Contents

Class QMCSampler

Namespace
OptunaSharp.Samplers
Assembly
OptunaSharp.dll

A Quasi Monte Carlo sampler that generates low-discrepancy sequences. Port of optuna/samplers/_qmc.py: QMCSampler (experimental since Optuna 3.0.0).

Optuna's sampler wraps scipy.stats.qmc. OptunaSharp reproduces the non-scrambled Sobol' and Halton sequences bit-for-bit through OptunaSharp.Internal.QuasiMonteCarlo. The search space is fixed by the first trial of the study; the first trial (and any parameter outside the relative search space) is drawn by the independent_sampler (a RandomSampler by default, so those draws stay on MT19937).

Scrambling (PORTING_PLAN Phase 2.12 — complete). Both randomizations are ported bit-exact vs scipy, seeded by numpy.random.default_rng(seed): scrambled Sobol' through OptunaSharp.Internal.ScrambledSobol (the linear-matrix-scramble + digital shift) and scrambled Halton through OptunaSharp.Internal.ScrambledHalton (Owen's digit permutations — scipy's _van_der_corput_permutations over NumSharp's Generator.shuffle). Every (qmc_type, scramble) combination is supported.

public class QMCSampler : BaseSampler
Inheritance
QMCSampler
Inherited Members

Constructors

QMCSampler(string, bool, int?, BaseSampler?, bool, bool)

Configures a Sobol or Halton sequence and its independent fallback sampler.

public QMCSampler(string qmcType = "sobol", bool scramble = false, int? seed = null, BaseSampler? independentSampler = null, bool warnAsynchronousSeeding = true, bool warnIndependentSampling = true)

Parameters

qmcType string

The QMC sequence: "sobol" (default) or "halton".

scramble bool

Apply scrambling (randomization) to the QMC sequence; seeded by seed.

seed int?

Seed for the independent sampler and, when scramble is set, the QMC scrambling.

independentSampler BaseSampler

Sampler for the first trial / non-relative params. Defaults to RandomSampler.

warnAsynchronousSeeding bool

Warn when scrambling is on without a manual seed.

warnIndependentSampling bool

Warn when a parameter falls back to independent sampling.

Exceptions

ValueError

The requested sequence type is neither sobol nor halton.

Methods

AfterTrial(Study, FrozenTrial, TrialState, IReadOnlyList<double>?)

Trial post-processing: called after the objective function returns and right before the trial is finished and its state is stored.

public override void AfterTrial(Study study, FrozenTrial trial, TrialState state, IReadOnlyList<double>? values)

Parameters

study Study

Study that owns the trial being finished.

trial FrozenTrial

Trial snapshot before its terminal state is written to storage.

state TrialState

Terminal state being recorded: complete, pruned, or failed.

values IReadOnlyList<double>

Resulting trial values in objective order; non-null for a successful trial.

BeforeTrial(Study, FrozenTrial)

Trial pre-processing: called during trial initialization, just before the InferRelativeSearchSpace(Study, FrozenTrial) call.

public override void BeforeTrial(Study study, FrozenTrial trial)

Parameters

study Study

Study that owns the newly initialized trial.

trial FrozenTrial

New trial snapshot available before search-space inference.

InferRelativeSearchSpace(Study, FrozenTrial)

Infer the search space that will be used by relative sampling in the target trial.

This method is called right before SampleRelative(Study, FrozenTrial, OrderedDict<string, BaseDistribution>), and the search space returned by this method is passed to it. The parameters not contained in the search space will be sampled by using SampleIndependent(Study, FrozenTrial, string, BaseDistribution).

public override OrderedDict<string, BaseDistribution> InferRelativeSearchSpace(Study study, FrozenTrial trial)

Parameters

study Study

Study supplying trial history, directions, and storage.

trial FrozenTrial

Current trial snapshot; copy it before modifying its contents.

Returns

OrderedDict<string, BaseDistribution>

Parameter distributions eligible for joint sampling, in sampler-defined order.

ReseedRng()

Reseed sampler's random number generator.

Parallel optimization calls this hook before a worker's sequential trial loop. Concrete samplers refresh their random streams so workers do not deliberately reuse a seeded sequence. OptunaSharp workers share the study's sampler instance; this hook does not clone it or make concurrent sampling reproducible.

public override void ReseedRng()

SampleIndependent(Study, FrozenTrial, string, BaseDistribution)

Sample a parameter for a given distribution.

This method is called only for the parameters not contained in the search space returned by SampleRelative(Study, FrozenTrial, OrderedDict<string, BaseDistribution>). This method is suitable for sampling algorithms that do not use relationship between parameters such as random sampling and TPE.

public override object? SampleIndependent(Study study, FrozenTrial trial, string paramName, BaseDistribution paramDistribution)

Parameters

study Study

Study supplying trial history, directions, and storage.

trial FrozenTrial

Current trial snapshot; copy it before modifying its contents.

paramName string

Name of the parameter being suggested.

paramDistribution BaseDistribution

Distribution defining the parameter's domain and scale.

Returns

object

A value in the distribution's external representation, including null categorical choices.

SampleRelative(Study, FrozenTrial, OrderedDict<string, BaseDistribution>)

Sample parameters in a given search space.

The trial invokes this method lazily when it first needs relative parameters and caches the result for its remaining suggestions. This method is suitable for sampling algorithms that use relationship between parameters such as Gaussian Process and CMA-ES.

public override OrderedDict<string, object?> SampleRelative(Study study, FrozenTrial trial, OrderedDict<string, BaseDistribution> searchSpace)

Parameters

study Study

Study supplying trial history, directions, and storage.

trial FrozenTrial

Current trial snapshot; copy it before modifying its contents.

searchSpace OrderedDict<string, BaseDistribution>

Distributions returned by InferRelativeSearchSpace(Study, FrozenTrial).

Returns

OrderedDict<string, object>

Jointly sampled external parameter values; omitted parameters use independent sampling.

See Also