Table of Contents

Class GPSampler

Namespace
OptunaSharp.Samplers
Assembly
OptunaSharp.dll

Sampler using Gaussian-process-based Bayesian optimization — port of optuna/samplers/_gp/sampler.py: GPSampler. It fits a Matérn-5/2 ARD Gaussian process to the past trials (kernel hyperparameters by marginal-likelihood maximization with the Gamma / hand-crafted priors) and suggests the next parameters by maximizing an acquisition function with the mixed continuous/discrete optimizer (OptunaSharp.Gp.OptimMixed).

GP stays native (Tier N): no TorchSharp — the fit rides the hand-derived Matérn-5/2 marginal-likelihood gradient and the managed L-BFGS-B (OptunaSharp.Internal.LBfgsB), and the acquisition optimization the managed acqf gradient. The path is tolerance-exact vs Optuna (ULP drift from scipy's OpenBLAS BLAS + the erf/ucrt class in LogEI), the documented GP boundary — not bit-for-bit like the MT19937 samplers.

Scope (PORTING_PLAN Phase 3.2 part 3e — complete): every path is ported. Sequential (no running trials): single-objective unconstrained (OptunaSharp.Gp.LogEI), constrained (OptunaSharp.Gp.LogCEI), multi-objective (OptunaSharp.Gp.LogEHVI), and multi-objective constrained (OptunaSharp.Gp.LogCEHVI). With running trials (asked but not yet told): the q-batch acqfs built on the OptunaSharp.Gp.ConditionalGPRegressor — OptunaSharp.Gp.QLogEI, OptunaSharp.Gp.QLogCEI, OptunaSharp.Gp.QLogEHVI, and (since Optuna 5.0.0, #6804) OptunaSharp.Gp.QLogCEHVI for the multi-objective constrained case, which earlier versions handled with a Kriging-Believer OptunaSharp.Gp.LogCEHVI.

public class GPSampler : BaseSampler
Inheritance
GPSampler
Inherited Members

Examples

var sampler = new GPSampler(seed: 0);
var study = Optuna.CreateStudy(sampler: sampler);
study.Optimize(t => Math.Pow(t.SuggestFloat("x", -10, 10) - 2, 2), nTrials: 30);

Constructors

GPSampler(int?, BaseSampler?, int, bool, Func<FrozenTrial, IEnumerable<double>>?, bool)

Configures Gaussian-process fitting and the independent sampler used before enough observations exist.

public GPSampler(int? seed = null, BaseSampler? independentSampler = null, int nStartupTrials = 10, bool deterministicObjective = false, Func<FrozenTrial, IEnumerable<double>>? constraintsFunc = null, bool warnIndependentSampling = true)

Parameters

seed int?

Random seed to initialize the internal RNG. Defaults to null (picked randomly).

independentSampler BaseSampler

Sampler used for initial sampling (the first nStartupTrials trials) and for conditional parameters. Defaults to a RandomSampler with the same seed.

nStartupTrials int

Number of initial trials sampled independently. Defaults to 10.

deterministicObjective bool

Whether the objective is deterministic. If true, the noise variance is fixed at the minimum. Defaults to false (experimental).

constraintsFunc Func<FrozenTrial, IEnumerable<double>>

Optional constraints function evaluated after each successful trial. Deprecated in v5.0.0 — use Trial.SetConstraint instead.

warnIndependentSampling bool

Emit a warning when a parameter is sampled independently.

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.

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

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