Class MoeaDSampler
- Namespace
- OptunaSharp.Samplers
- Assembly
- OptunaSharp.dll
Multi-objective sampler using the MOEA/D algorithm (Zhang & Li, 2007): a decomposition-based evolutionary algorithm that splits the multi-objective problem into a spread of scalar subproblems (one per Das-Dennis weight vector) and optimizes them cooperatively through neighborhood mating and decomposition-based replacement. The scalarizing function is Tchebycheff by default (MoeadScalarization also offers PBI and weighted sum).
Beyond-core. Optuna has no MOEA/D. OptunaHub's samplers/moead
(MOEADSampler) is another implementation of the algorithm, which this class does not follow: it
ports pymoo's MOEAD, so a study moved over from the hub package gets other defaults and other
trials (map below). Like SmsEmoaSampler it is validated by convergence and achieved
hypervolume, not bit-matched to an upstream. The crossover/mutation are the shared NSGA operators
(SBXCrossover + PolynomialMutation by default), so only the selection
and replacement differ from the NSGA-II family.
OptunaHub's MOEADSampler and this class (registry pinned in refs/optunahub-registry):
OptunaHub MOEADSampler | This class |
|---|---|
population_size=100 | populationSize = 50, rounded down to a
Das-Dennis lattice size (the hub uses the number as given) |
n_neighbors=None (then population_size // 10) | nNeighbors = 20 |
scalar_aggregation_func="tchebycheff" (also "weighted_sum", "PBI") | scalarization:
Tchebycheff by default, plus PBI and weighted sum |
crossover=None, meaning UniformCrossover(swapping_prob) | crossover:
SBXCrossover by default |
mutation=None, meaning UniformMutation() | mutation:
PolynomialMutation by default |
crossover_prob=0.9 (otherwise a random parent is copied) | none: every offspring is crossed |
mutation_prob=None (1 / number of parameters), swapping_prob=0.5, seed | mutationProb,
swappingProb, seed, same defaults |
| (mates within the neighborhood) | neighborSelectionProb = 0.9 (pymoo's
prob_neighbor_mating) and nReplacements = 2 |
In-memory state. The N-solution population, its ideal point and the folded-offspring bookkeeping are reconstructed incrementally and held in memory for the study's process lifetime, so a MOEA/D study does not resume across process boundaries (same concession as SmsEmoaSampler). Categorical and single-value distributions are handled by the independent sampler.
Actual population size. The structured Das-Dennis lattice has a fixed size for each
(objectives, partitions) pair, so the sampler uses the largest lattice whose size does not exceed
the requested populationSize; the effective N (and hence the number of subproblems) can
therefore be slightly below populationSize.
public class MoeaDSampler : BaseSampler
- Inheritance
-
MoeaDSampler
- Inherited Members
Examples
var study = Optuna.CreateStudy(
directions: new[] { StudyDirection.Minimize, StudyDirection.Minimize },
sampler: new MoeaDSampler(populationSize: 50, nNeighbors: 15, seed: 0));
study.Optimize(t =>
{
double x = t.SuggestFloat("x", 0, 1), y = t.SuggestFloat("y", 0, 1);
return new[] { x, (1 - Math.Sqrt(x)) + y };
}, nTrials: 600);
Constructors
MoeaDSampler(int, int, double, int, MoeadScalarization, BaseMutation?, double?, BaseCrossover?, double, int?)
Configures decomposition weights, neighborhood mating, and bounded incumbent replacement.
public MoeaDSampler(int populationSize = 50, int nNeighbors = 20, double neighborSelectionProb = 0.9, int nReplacements = 2, MoeadScalarization scalarization = MoeadScalarization.Tchebycheff, BaseMutation? mutation = null, double? mutationProb = null, BaseCrossover? crossover = null, double swappingProb = 0.5, int? seed = null)
Parameters
populationSizeintTarget number of subproblems / weight vectors. The effective N is the largest Das-Dennis lattice size not exceeding this (see the class remarks).
nNeighborsintNeighborhood size T — how many nearest weight vectors form each subproblem's mating/replacement neighborhood (clamped to N). Default 20.
neighborSelectionProbdoubleProbability δ that a subproblem's parents are drawn from its neighborhood rather than the whole population (Zhang & Li's
δ). Default 0.9.nReplacementsintMaximum number of neighbors an offspring may replace (Zhang & Li's
nr, which preserves diversity). Default 2.scalarizationMoeadScalarizationDecomposition / scalarizing function (default Tchebycheff).
mutationBaseMutationMutation applied to child individuals; defaults to PolynomialMutation.
mutationProbdouble?Probability of mutating each parameter. If
null,1 / len(params)is used.crossoverBaseCrossoverCrossover applied to child individuals; defaults to SBXCrossover (MOEA/D's classic SBX). UniformCrossover-style crossover is always applied to categorical parameters.
swappingProbdoubleProbability of swapping each categorical parameter of the parents during crossover.
seedint?Seed for the random number generator.
Exceptions
- ValueError
Population size is below two or the crossover parent count, neighborhood/replacement counts are below one, or neighborhood-selection probability is outside [0, 1].
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.
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.