Table of Contents

Class BaseGASampler

Namespace
OptunaSharp.Samplers
Assembly
OptunaSharp.dll

Base class for Genetic Algorithm (GA) samplers. Port of optuna/samplers/_ga/_base.py: BaseGASampler.

GA samplers generate new trials by mimicking natural selection, using generations and populations to iteratively improve solutions. This base class defines the interface and provides utility methods for managing generations and populations. Parent selection is handled by SelectParent(Study, int), which subclasses implement.

public abstract class BaseGASampler : BaseSampler
Inheritance
BaseGASampler
Derived
Inherited Members

Remarks

Generation bookkeeping is incremental: each completed trial is inspected exactly once (via OptunaSharp.Study.GetTrialsView()'s zero-copy window) and folded into a per-study tracker, instead of re-scanning every completed trial on every ask as the reference implementation does. The numbers this produces — the running generation, each generation's population, the cached parents — are identical to the full scan's (same attrs read, same formula, same ascending-number order), so optimization trajectories are bit-for-bit unchanged; only the cost drops from O(N) per ask (O(N²) per study) to O(new trials). The generation attr each trial receives and the per-generation parent-id study attrs are still written, so journals and resumed studies read exactly as before.

One sampler, several studies. The bookkeeping is kept per storage object and study id, not per study id alone (every fresh in-memory storage starts at id 0), and it is dropped when the study stored under that id is no longer the one it was built from, so a sampler instance reused for a second study starts it at generation 0 with that study's own populations, exactly as Optuna's stateless scan does.

Constructors

BaseGASampler(int?)

Initializes generation bookkeeping with the requested number of completed individuals per generation.

protected BaseGASampler(int? populationSize)

Parameters

populationSize int?

Number of individuals retained or evaluated per generation.

Properties

PopulationSize

Number of individuals (trials) in a generation.

public int? PopulationSize { get; set; }

Property Value

int?

Methods

GetParentPopulation(Study, int)

Get the parent population of the given generation (cached in the study system attributes). Returns an empty list when generation is 0.

public List<FrozenTrial> GetParentPopulation(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>

Selected parents, or an empty list for generation zero.

Exceptions

FormatException

A cached parent identifier string cannot be parsed as an integer.

InvalidCastException

Cached parent metadata has an unexpected collection or identifier representation.

OverflowException

A cached parent identifier exceeds Int32.

Exception

The configured parent-selection strategy or storage provider fails; its exception propagates.

GetPopulation(Study, int)

Get the population (completed trials) of the given generation.

public List<FrozenTrial> GetPopulation(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>

Completed trial snapshots belonging to the requested generation, in ascending trial-number order.

Exceptions

FormatException

A stored generation string cannot be parsed as an integer.

InvalidCastException

A stored generation value does not support integer conversion.

OverflowException

A stored generation value exceeds Int32.

GetTrialGeneration(Study, FrozenTrial)

Get the generation number of the given trial. If not already set in the trial's system attributes, it is calculated (from the max generation of all completed trials) and stored.

public int GetTrialGeneration(Study study, FrozenTrial trial)

Parameters

study Study

Study supplying trial history, objective directions, and storage.

trial FrozenTrial

Trial snapshot being examined; copy its contents before modifying them.

Returns

int

The stored or newly assigned zero-based generation.

Exceptions

FormatException

A stored generation string cannot be parsed as an integer.

InvalidCastException

A stored generation value does not support integer conversion.

OverflowException

A stored generation value exceeds Int32.

Exception

The configured storage provider fails; its exception propagates.

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 abstract 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.