Class NSGAIIISampler
- Namespace
- OptunaSharp.Samplers
- Assembly
- OptunaSharp.dll
Multi-objective sampler using the NSGA-III algorithm. Port of
optuna/samplers/_nsgaiii/_sampler.py: NSGAIIISampler (experimental since Optuna 3.2.0).
NSGA-III ("Nondominated Sorting Genetic Algorithm III") is a modification of NSGAIISampler for many-objective optimization. It reuses the whole NSGA-II genetic machinery (BaseGASampler, child generation, crossovers, mutations) and only swaps the elite-selection strategy: instead of crowding distance, the borderline front is truncated by reference-point niching (see NSGAIIIElitePopulationSelectionStrategy). See Deb & Jain (2014).
Parity. The default path stays in exact MT19937 lock-step with Optuna (generation
attribution, parent cache, tournament/crossover/mutation draws, and the niching
rng.shuffle), and objective-value niching is bit-exact: the hyperplane-fit
matrix_rank/solve route through NumSharp's OpenBLAS LAPACK backend (byte-identical
to NumPy), so the earlier ~1-ULP managed-LU drift is gone. Confirmed byte-for-byte over a
400-config differential plus the live parity seeds — see
NSGAIIIElitePopulationSelectionStrategy.
Conditional parameters. To optimize conditional (define-by-run, branch-defined) hyperparameters — which otherwise fall out of the intersection search space and are resampled at random on every trial — use NSGAIIICptSampler (NSGA-III-CPT), the OptunaSharp variant that carries a ConditionalParams rule set. This sampler itself is a bit-exact Optuna port with no such extension.
public class NSGAIIISampler : BaseGASampler
- Inheritance
-
NSGAIIISampler
- Inherited Members
Constructors
NSGAIIISampler(int, BaseMutation?, double?, BaseCrossover?, double, double, int?, Func<FrozenTrial, IEnumerable<double>>?, double[,]?, int, Func<Study, List<FrozenTrial>, List<FrozenTrial>>?, Func<Study, OrderedDict<string, BaseDistribution>, List<FrozenTrial>, OrderedDict<string, object?>>?, Action<Study, FrozenTrial, TrialState, IReadOnlyList<double>?>?)
Configures NSGA-III reference-point selection, reproduction, and trial-completion hooks.
public NSGAIIISampler(int populationSize = 50, BaseMutation? mutation = null, double? mutationProb = null, BaseCrossover? crossover = null, double crossoverProb = 0.9, double swappingProb = 0.5, int? seed = null, Func<FrozenTrial, IEnumerable<double>>? constraintsFunc = null, double[,]? referencePoints = null, int dividingParameter = 3, Func<Study, List<FrozenTrial>, List<FrozenTrial>>? elitePopulationSelectionStrategy = null, Func<Study, OrderedDict<string, BaseDistribution>, List<FrozenTrial>, OrderedDict<string, object?>>? childGenerationStrategy = null, Action<Study, FrozenTrial, TrialState, IReadOnlyList<double>?>? afterTrialStrategy = null)
Parameters
populationSizeintNumber of individuals (trials) in a generation. Must be >=
crossover.NParents(3 for UNDXCrossover/SPXCrossover, else 2).mutationBaseMutationMutation applied when creating child individuals. If
null, the parameter selected for mutation is instead resampled independently using RandomSampler.mutationProbdouble?Probability of mutating each parameter. If
null,1 / len(parent.params)is used.crossoverBaseCrossoverCrossover applied when creating child individuals. UniformCrossover is always applied to categorical parameters, and by default to the others.
crossoverProbdoubleProbability that a crossover occurs when creating a new individual.
swappingProbdoubleProbability of swapping each parameter of the parents during crossover.
seedint?Seed for random number generator.
constraintsFuncFunc<FrozenTrial, IEnumerable<double>>Deprecated (v5.0.0). Optional function computing objective constraints (a value strictly larger than 0 means a constraint is violated). Handled by constrained domination.
referencePointsdouble[,]A 2-D array with
n_objectivescolumns: the reference points that determine who survives from the borderline front (survival favors sparsely populated reference points). Whennull(default),uniformlyspread points are generated fromdividingParameter.dividingParameterintDetermines the density of the default reference points — how many divisions are made between reference points on each axis. Smaller means fewer reference points. Unused when
referencePointsis notnull.elitePopulationSelectionStrategyFunc<Study, List<FrozenTrial>, List<FrozenTrial>>Overrides elite selection.
childGenerationStrategyFunc<Study, OrderedDict<string, BaseDistribution>, List<FrozenTrial>, OrderedDict<string, object>>Overrides child generation.
afterTrialStrategyAction<Study, FrozenTrial, TrialState, IReadOnlyList<double>>Overrides the after-trial procedure.
Exceptions
- ValueError
Population size is below two or the crossover parent count, or a configured reproduction probability is invalid.
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.
SelectParent(Study, int)
Select parent trials from the population for the given generation. Called once per generation; the result is cached in the study system attributes.
public override List<FrozenTrial> SelectParent(Study study, int generation)
Parameters
studyStudyStudy supplying trial history, objective directions, and storage.
generationintZero-based generation whose individuals or optimizer state are requested.
Returns
- List<FrozenTrial>
The selected parent trials for the requested generation.