Class TPESampler
- Namespace
- OptunaSharp.Samplers
- Assembly
- OptunaSharp.dll
Sampler using the TPE (Tree-structured Parzen Estimator) algorithm — Optuna's default
sampler. Port of optuna/samplers/_tpe/sampler.py: TPESampler.
On each trial, for each parameter, TPE fits one Gaussian Mixture Model l(x) to the
parameter values of the best trials, and another GMM g(x) to the rest, then picks the
value maximizing l(x)/g(x).
Scope (PORTING_PLAN Phase 2.8/2.9). The univariate (independent), the
non-group multivariate, and (Phase 2.9) the group-decomposed multivariate (group=true)
paths are implemented — so a default TPESampler works for single- and
multi-objective studies. Multi-objective splitting inherits the Phase 2.10 HSSP tie-break gap
(an explicit target avoids it).
[Misaligned] erf/ucrt tolerance. The sampled values and acquisition densities
route through OptunaSharp.Internal.TruncNorm → OptunaSharp.Internal.Erf (FreeBSD s_erf), ~1-3 ULP
from Python's math.erf (ucrt). So TPE agrees with Optuna to a few ULP, not bit-for-bit —
the same bound documented for PED-ANOVA. RNG draw order and count are identical, so the study
trajectory stays aligned. See also the argsort tie-break note on OptunaSharp.Samplers.Tpe.ParzenEstimator.
public class TPESampler : BaseSampler
- Inheritance
-
TPESampler
- Inherited Members
Constructors
TPESampler(bool?, double?, bool?, bool?, int, int, Func<int, int>?, Func<int, double[]>?, int?, bool?, bool, bool?, bool, Func<FrozenTrial, IEnumerable<double>>?)
Configures density estimation, startup sampling, and optional joint search-space groups.
public TPESampler(bool? considerPrior = null, double? priorWeight = null, bool? considerMagicClip = null, bool? considerEndpoints = null, int nStartupTrials = 10, int nEiCandidates = 24, Func<int, int>? gamma = null, Func<int, double[]>? weights = null, int? seed = null, bool? multivariate = null, bool group = false, bool? warnIndependentSampling = null, bool constantLiar = true, Func<FrozenTrial, IEnumerable<double>>? constraintsFunc = null)
Parameters
considerPriorbool?Deprecated (v4.3.0). Falls back to
true.priorWeightdouble?Deprecated (v4.9.0). Falls back to
1.0.considerMagicClipbool?Deprecated (v4.9.0). Falls back to
true.considerEndpointsbool?Deprecated (v4.9.0). Falls back to
false.nStartupTrialsintRandom sampling is used until this many trials finish.
nEiCandidatesintNumber of candidates for the expected-improvement computation.
gammaFunc<int, int>Deprecated (v4.9.0). Splits finished trials into below/above.
weightsFunc<int, double[]>Deprecated (v4.9.0). Weight of each finished trial.
seedint?Seed for the random number generator.
multivariatebool?If
true, multivariate TPE is used. Ifnull(default), it istruefor single-objective andfalsefor multi-objective optimization.groupboolIf this and
multivariatearetrue, the multivariate TPE with a group-decomposed search space is used: the search space is partitioned into maximal subspaces from the past trials and each subspace is sampled jointly by multivariate TPE. Requiresmultivariateto not befalse(experimental since v2.8.0).warnIndependentSamplingbool?Deprecated (v4.9.0). Falls back to
false.constantLiarboolPenalize running trials to avoid nearby suggestions. Defaults to
true.constraintsFuncFunc<FrozenTrial, IEnumerable<double>>Deprecated (v5.0.0). Optional constraint function evaluated after each trial.
Exceptions
- ValueError
Grouping is enabled while multivariate estimation is explicitly disabled.
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
studyStudyStudy that owns the trial being finished.
trialFrozenTrialTrial snapshot before its terminal state is written to storage.
stateTrialStateTerminal state being recorded: complete, pruned, or failed.
valuesIReadOnlyList<double>Resulting trial values in objective order; non-null for a successful trial.
Exceptions
- Exception
A configured after-trial or constraint callback fails; its exception propagates.
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
studyStudyStudy that owns the newly initialized trial.
trialFrozenTrialNew 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
studyStudyStudy supplying trial history, directions, and storage.
trialFrozenTrialCurrent 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
studyStudyStudy supplying trial history, directions, and storage.
trialFrozenTrialCurrent trial snapshot; copy it before modifying its contents.
paramNamestringName of the parameter being suggested.
paramDistributionBaseDistributionDistribution 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
studyStudyStudy supplying trial history, directions, and storage.
trialFrozenTrialCurrent trial snapshot; copy it before modifying its contents.
searchSpaceOrderedDict<string, BaseDistribution>Distributions returned by InferRelativeSearchSpace(Study, FrozenTrial).
Returns
- OrderedDict<string, object>
Jointly sampled external parameter values; omitted parameters use independent sampling.