Class JanusDeSampler
- Namespace
- OptunaSharp.Samplers
- Assembly
- OptunaSharp.dll
JANUS-DE: the training-free JANUS Jacobian infill on a Differential Evolution host. JANUS estimates
a local Jacobian from the recent evaluation trace and bounded-injects a damped Gauss–Newton
exploitation elite into each DE generation, while the DifferentialEvolutionSampler-style
DE host keeps full control of selection. Because DE has no Gaussian proposal covariance, this is the
paper's JANUS-GN configuration — the exploitation injection only, no CMIX exploration
candidates. It is the host-agnostic complement to the CMA-hosted JanusSampler.
Residual coordinates (optional but recommended). If the objective decomposes into
components f(x) = mean_i r_i(x), expose the residual vector on each trial via
trial.SetUserAttr("component_losses", new double[]{ r1, r2, … }); JANUS then estimates the
local Jacobian directly from those residuals. Without it, JANUS falls back to a local
linear/quadratic fit on the recent window of scalar objective values (the constructor's pseudoMode option).
Beyond-core, in-memory state. Like JanusSampler, JANUS-DE holds its optimizer (JANUS window + DE population) in memory for the study's duration; it does not resume its infill state across process boundaries. Categorical and single-value distributions are handled by the independent sampler.
public sealed class JanusDeSampler : BaseSampler
- Inheritance
-
JanusDeSampler
- Inherited Members
Examples
var study = Optuna.CreateStudy(sampler: new JanusDeSampler(seed: 0, nStartupTrials: 16));
study.Optimize(t =>
{
double s = 0;
for (int i = 0; i < 10; i++) { double x = t.SuggestFloat($"x{i}", -5, 5); double r = (x - 0.5) * (x - 0.5); s += r; }
return s;
}, nTrials: 400);
Constructors
JanusDeSampler(int?, int?, int, BaseSampler?, bool, int, double, double, string, int, int?, double, int?, double, double, string, string?, string, string)
Configures Jacobian-informed candidates injected into a differential-evolution population.
public JanusDeSampler(int? seed = null, int? popsize = null, int nStartupTrials = 1, BaseSampler? independentSampler = null, bool warnIndependentSampling = true, int nJobs = 1, double f = 0.8, double cr = 0.9, string strategy = "rand1bin", int fuseEvery = 4, int? fuseStart = null, double kappa = 1, int? gnWindow = null, double gnLambda = 1, double gnRidge = 1, string pseudoMode = "linear", string? gnPseudoMode = null, string pseudoTarget = "raw", string pcaK = "auto")
Parameters
seedint?Random seed. When
null, a random seed is drawn (from MT19937, never System.Random).popsizeint?DE population size NP (default
max(8, 4 + ⌊3 ln d⌋)).nStartupTrialsintIndependent sampling is used until this many trials finish.
independentSamplerBaseSamplerSampler for parameters outside the relative space (default RandomSampler).
warnIndependentSamplingboolWarn when a parameter is sampled independently.
nJobsintNumber of parallel workers; a separate optimizer is kept per
trial.Number % nJobs.fdoubleDE differential weight F (default 0.8).
crdoubleDE crossover rate CR (default 0.9).
strategystringDE mutation strategy:
rand1bin(default) orbest1bin.fuseEveryintGenerations between Jacobian re-estimation (assembly cadence).
fuseStartint?Window size before the first Jacobian assembly (
null= auto).kappadoubleBounded-injection clip factor (whitened step ‖z‖ ≤ κ√d, in DE's per-dimension spread metric).
gnWindowint?Sliding window of recent
(x, f)pairs for the Jacobian regression (null= auto).gnLambdadoubleLevenberg–Marquardt damping for the Gauss–Newton step.
gnRidgedoubleRidge regularization for the residual-gradient regression.
pseudoModestringScalar residual-coordinate fit:
linear(default),quadratic,diag_quadratic,pca_quadratic,diag_pca_quadratic, ornone.gnPseudoModestringOverride
pseudoModefor the GN direction (null= same).pseudoTargetstringScalar-value transform for the fit:
raw(default),rank, orlog.pcaKstringPCA rank for the
pca_quadraticfit (autoor an integer string).
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.
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.
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
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.
Exceptions
- ValueError
The study has multiple objectives, but this sampler requires a scalar objective.