Table of Contents

Class NSGAIIICptSampler

Namespace
OptunaSharp.Samplers
Assembly
OptunaSharp.dll

NSGA-III-CPT ("NSGA-III with Categorical Parameter Trees") — an OptunaSharp original that extends NSGA-III to optimize conditional (define-by-run, branch-defined) hyperparameters. It is a standalone copy of NSGAIIISampler: same many-objective genetic core (reference-point niching via NSGAIIIElitePopulationSelectionStrategy, the shared crossovers, mutations and ranking), plus the conditional-parameter machinery that NSGAIIISampler deliberately no longer carries.

Why it exists. Optuna's GA samplers evolve only the intersection search space, so a parameter suggested under one branch (if optimizer == "adam": beta1 = suggest(...)) drops out of the evolved space after the first trial that lacks it and is resampled at random ever after — it gets no crossover, mutation or inheritance. NSGA-III-CPT takes a declarative ConditionalParams rule set that says which parameters are conditional on which activator values; those parameters then stay in the relative search space (NsgaiiiCptSearchSpace), are crossed over, mutated and inherited whenever active (NsgaiiiCptChildGenerationStrategy / OptunaSharp.Samplers.NsgaiiiCpt.NsgaiiiCptCrossover), and are simply absent whenever not. This is the tree of categorically-gated parameters the "CPT" name denotes. See the NSGA-III-CPT documentation page (docs/website-src/docs/our-methods/nsga-iii-cpt.md).

Relationship to NSGA-III. Constructed with conditionalParams: null this sampler is behaviorally identical to NSGAIIISampler (the CPT search space and child generation both fall back to the shared Optuna-parity fold), so it is a strict superset. The conditional behavior is a deliberate divergence from Optuna and has no upstream analog; the underlying NSGA-III algorithm is the bit-exact port documented on NSGAIIISampler.

public class NSGAIIICptSampler : BaseGASampler
Inheritance
NSGAIIICptSampler
Inherited Members

Constructors

NSGAIIICptSampler(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>?>?, ConditionalParams?)

Configures NSGA-III-CPT reference-point selection, conditional-aware reproduction, and trial-completion hooks.

public NSGAIIICptSampler(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, ConditionalParams? conditionalParams = null)

Parameters

populationSize int

Number of individuals (trials) in a generation. Must be >= crossover.NParents (3 for UNDXCrossover/SPXCrossover, else 2).

mutation BaseMutation

Mutation applied when creating child individuals. If null, the parameter selected for mutation is instead resampled independently using RandomSampler.

mutationProb double?

Probability of mutating each parameter. If null, 1 / len(parent.params) is used.

crossover BaseCrossover

Crossover applied when creating child individuals. UniformCrossover is always applied to categorical parameters, and by default to the others.

crossoverProb double

Probability that a crossover occurs when creating a new individual.

swappingProb double

Probability of swapping each parameter of the parents during crossover.

seed int?

Seed for random number generator.

constraintsFunc Func<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.

referencePoints double[,]

A 2-D array with n_objectives columns: the reference points that determine who survives from the borderline front (survival favors sparsely populated reference points). When null (default), uniformly spread points are generated from dividingParameter.

dividingParameter int

Determines 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 referencePoints is not null.

elitePopulationSelectionStrategy Func<Study, List<FrozenTrial>, List<FrozenTrial>>

Overrides elite selection.

childGenerationStrategy Func<Study, OrderedDict<string, BaseDistribution>, List<FrozenTrial>, OrderedDict<string, object>>

Overrides child generation. A custom strategy receives the (conditional-widened) search space and must itself cope with parents that lack conditional parameters; the built-in NsgaiiiCptChildGenerationStrategy is the one that honors conditionalParams.

afterTrialStrategy Action<Study, FrozenTrial, TrialState, IReadOnlyList<double>>

Overrides the after-trial procedure.

conditionalParams ConditionalParams

The activation rules for parameters that only exist under some values of other parameters — the reason to reach for this sampler over NSGAIIISampler. Declared parameters are evolved (crossed over, mutated, inherited) whenever active instead of falling out of the intersection search space and being resampled at random on every trial. null makes this sampler behave exactly like NSGAIIISampler.

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

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.

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

study Study

Study supplying trial history, objective directions, and storage.

generation int

Zero-based generation whose individuals or optimizer state are requested.

Returns

List<FrozenTrial>

The selected parent trials for the requested generation.

See Also