Interface IOptunaStudyFactory
- Namespace
- OptunaSharp.Extensions
- Assembly
- OptunaSharp.dll
A dependency-injection composition seam that builds Study instances from the
sampler / pruner / storage registered in the container, applying the container-bound
OptunaStudyOptions defaults. Resolve it (constructor-inject
IOptunaStudyFactory) instead of calling CreateStudy(BaseStorage?, BaseSampler?, BasePruner?, string?, StudyDirection?, bool, IEnumerable<StudyDirection>?) directly, so a
study's algorithm and persistence are configured once at the composition root rather than at
every call site.
Why an interface (and why a factory, not a singleton Study): a study is a per-run object — its name and direction vary, and a persistent storage may already hold one — so it cannot be a container singleton. Each call mints (or re-attaches to) a study. The interface makes the seam mockable in tests.
Lifetime: AddOptunaStudyFactory registers the factory as transient, so it resolves its
sampler, pruner and storage from the provider that resolved it: a request scope resolves scoped
registrations from that scope, and the root provider (a singleton or hosted service) resolves
singletons and transients. The sampler and pruner factory registrations default to transient, so
each study gets its own sampler and pruner unless one instance was registered on purpose.
public interface IOptunaStudyFactory
Methods
CreateStudy(string?, StudyDirection?, bool?, IEnumerable<StudyDirection>?)
Create (or, with load_if_exists, re-attach to) a Study using the
container's registered BaseSampler / BasePruner / BaseStorage (each optional
— an unregistered one falls back to Optuna's default: TPESampler, MedianPruner,
in-memory storage). Every parameter overrides the matching OptunaStudyOptions
default for this one call.
Study CreateStudy(string? studyName = null, StudyDirection? direction = null, bool? loadIfExists = null, IEnumerable<StudyDirection>? directions = null)
Parameters
studyNamestringThe study name, or
nullto use DefaultStudyName (and, if that is alsonull, a storage-generated unique name).directionStudyDirection?The single-objective direction, or
nullto fall back to the options. Must not be combined withdirections.loadIfExistsbool?Whether to return an existing same-named study instead of raising on conflict;
nulluses LoadIfExists.directionsIEnumerable<StudyDirection>The multi-objective directions, or
nullto fall back to the options. Must not be combined withdirection.
Returns
- Study
A study bound to the resolved storage, using the resolved sampler and pruner.
Exceptions
- ValueError
Both a single and a multi direction are in effect (from arguments or options), the directions sequence is empty, or a direction is neither minimize nor maximize.
- DuplicatedStudyError
The effective study name already exists and the effective
load_if_existsisfalse.