Class RandomSampler
- Namespace
- OptunaSharp.Samplers
- Assembly
- OptunaSharp.dll
Sampler using random sampling. Port of optuna/samplers/_random.py: RandomSampler.
This sampler is based on independent sampling. See also BaseSampler for more details of 'independent sampling'.
public class RandomSampler : BaseSampler
- Inheritance
-
RandomSampler
- Inherited Members
Examples
var study = Optuna.CreateStudy(sampler: new RandomSampler());
study.Optimize(trial => Math.Pow(trial.SuggestFloat("x", -5, 5), 2), nTrials: 10);
Constructors
RandomSampler(int?)
Creates an independent sampler with a lazily initialized NumPy-compatible random stream.
public RandomSampler(int? seed = null)
Parameters
seedint?Seed for random number generator.
Methods
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.
Remarks
Optuna draws rng.uniform(trans.bounds[:, 0], trans.bounds[:, 1]) over the transform's bound rows (one row
for a numerical parameter, one per choice for a categorical one) and untransforms the result. This draws the
same rows one at a time through Uniform(NumPyRandom, double, double): each row is NumPy's low + (high - low) * U
from the same uniform in the same order, so the value and the generator's next state are Optuna's bit for bit
(pinned live by RandomSamplerParityTests). The array form's only other difference, raising before any draw
when some range is infinite, cannot arise here: a numerical parameter has one row and every categorical row is
[0, 1].
Why not the array form (plan 14): it built five NumSharp arrays per suggestion (the bounds, two column views, the draws and the untransform's float64 copy) and left them to the finalizer, which made GC pauses 47 % of a 40-parameter random search.
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.