Table of Contents

Class DifferentialEvolutionSampler

Namespace
OptunaSharp.Samplers
Assembly
OptunaSharp.dll

A sampler driving a textbook-faithful OptunaSharp.Samplers.Differential.De Differential Evolution optimizer (Storn & Price 1997) through the Optuna protocol. DE maintains a population of candidate solutions and, for each target member, forms a trial vector by adding a scaled difference of other members (DE/rand/1 or DE/best/1) and binomially crossing it with the target; the trial replaces the target only if it is no worse (greedy one-to-one selection). It is a robust, derivative-free global optimizer that complements the Gaussian model of CmaEsSampler and the limited-memory LmCmaSampler.

Bootstrapping. DE needs an initial population before it can generate offspring, so the first max(nStartupTrials, populationSize) completed trials are drawn by the independent sampler and become the initial population; DE offspring are proposed from then on.

Two protocol concessions (as LmCmaSampler): (1) DE is unconstrained, so each asked candidate is clamped to the transformed bounds and the next generation's greedy selection re-derives the parent from the stored (clamped) params — the optimizer learns from the actually-evaluated points. (2) The per-trial protocol asks one candidate per trial from freshly-restored state; each trial is stamped with its generation and its target index so the population's greedy selection pairs each offspring with the right parent once the generation completes.

Categorical parameters and single-value distributions are handled by the independent sampler, as in CmaEsSampler.

Not OptunaHub's DESampler. OptunaHub's samplers/differential_evolution is another implementation of DE/rand/1/bin, which this class does not follow, so a study moved over from it gets other defaults and other trials (registry pinned in refs/optunahub-registry):

OptunaHub DESamplerThis class
population_size="auto": max(20, 10d) below 5 dimensions, max(30, 10d) up to 30, then max(50, 5d)popsize: max(8, 4 + ⌊3 ln d⌋)
F=0.8f = 0.8
CR=0.7cr = 0.9
DE/rand/1/bin onlystrategy: "rand1bin" (default) or "best1bin"
search_space, debug, seedseed; the search space is inferred; plus nStartupTrials, independentSampler, warnIndependentSampling
public sealed class DifferentialEvolutionSampler : BaseSampler
Inheritance
DifferentialEvolutionSampler
Inherited Members

Examples

var study = Optuna.CreateStudy(sampler: new DifferentialEvolutionSampler(seed: 42));
study.Optimize(t =>
{
    double s = 0;
    for (int i = 0; i < 10; i++) { double x = t.SuggestFloat($"x{i}", -5, 5); s += x * x; }
    return s;
}, nTrials: 400);

Constructors

DifferentialEvolutionSampler(int, BaseSampler?, bool, int?, int?, double, double, string)

Configures differential-evolution proposals and independent startup sampling.

public DifferentialEvolutionSampler(int nStartupTrials = 1, BaseSampler? independentSampler = null, bool warnIndependentSampling = true, int? seed = null, int? popsize = null, double f = 0.8, double cr = 0.9, string strategy = "rand1bin")

Parameters

nStartupTrials int

Minimum independent trials before DE begins; DE always waits for at least populationSize completed trials to seed the initial population, whichever is larger.

independentSampler BaseSampler

Sampler for parameters outside the relative search space (default RandomSampler).

warnIndependentSampling bool

Emit a warning when a parameter is sampled independently.

seed int?

Random seed for DE.

popsize int?

Population size NP (default max(8, 4 + ⌊3 ln d⌋); must be ≥ 4).

f double

Differential weight F, scaling the difference vector (default 0.8).

cr double

Crossover rate CR, the per-coordinate probability of inheriting the mutant (default 0.9).

strategy string

Mutation strategy: rand1bin (default, DE/rand/1/bin) or best1bin (DE/best/1/bin).

Exceptions

ArgumentException

The requested population size is below four.

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.

Exceptions

ValueError

The study has multiple objectives, but this sampler requires a scalar objective.

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.

Exceptions

ValueError

The study has multiple objectives, but this sampler requires a scalar objective.