Class Study
- Namespace
- OptunaSharp
- Assembly
- OptunaSharp.dll
A study corresponds to an optimization task, i.e., a set of trials.
Port of optuna/study/study.py: Study.
This object provides interfaces to run a new Trial, access trials' history, set/get user-defined attributes of the study itself.
Note that the direct use of this constructor is not recommended. To create and load a study, please refer to CreateStudy(BaseStorage?, BaseSampler?, BasePruner?, string?, StudyDirection?, bool, IEnumerable<StudyDirection>?) and LoadStudy(string?, BaseStorage, BaseSampler?, BasePruner?) respectively.
public class Study
- Inheritance
-
Study
- Inherited Members
Constructors
Study(string, BaseStorage, BaseSampler?, BasePruner?)
Opens an existing named study using a shared storage backend.
public Study(string studyName, BaseStorage storage, BaseSampler? sampler = null, BasePruner? pruner = null)
Parameters
studyNamestringName identifying the study in the selected storage.
storageBaseStorageStorage backend holding study and trial records. An RDB backend (any ICachedStorageBackend, such as the SQLite or EF Core
RDBStorage) is wrapped in a new CachedStorage; every other storage, including one that is already a CachedStorage, is used as given.samplerBaseSamplerSampler for parameter suggestions; null selects TPE.
prunerBasePrunerPruning strategy; null selects the median pruner.
Remarks
Python's Study.init passes its storage through storages.get_storage, and so does
this constructor. Without the wrap, a study opened by LoadStudy(string?, BaseStorage, BaseSampler?, BasePruner?), by the CLI
or by calling this constructor directly would read the whole study from the database each time a
sampler asks for the trial history. CreateStudy(BaseStorage?, BaseSampler?, BasePruner?, string?, StudyDirection?, bool, IEnumerable<StudyDirection>?) has already wrapped its
storage, which then passes through unchanged. Each constructed study wraps separately, as each
Python load_study call does: two studies opened over one RDBStorage have two caches.
Exceptions
- KeyError
The named study does not exist in storage.
Properties
BestParams
Return parameters of the best trial in the study. This feature can only be used for single-objective optimization.
public IReadOnlyDictionary<string, object?> BestParams { get; }
Property Value
Exceptions
- RuntimeError
The study has multiple objectives.
- ValueError
No completed trial, or no feasible completed trial, is available.
- KeyError
The study has been removed from storage.
BestTrial
Return the best trial in the study.
This feature can only be used for single-objective optimization. If your study is multi-objective, use BestTrials instead.
In constrained optimization, the best trial is selected from trials that satisfy all constraints. A trial is considered feasible when all of its constraint values are less than or equal to 0.0.
public FrozenTrial BestTrial { get; }
Property Value
Exceptions
- RuntimeError
The study has multiple objectives.
- ValueError
No completed trial, or no feasible completed trial, is available.
- KeyError
The study has been removed from storage.
BestTrials
Return trials located at the Pareto front in the study.
A trial is located at the Pareto front if there are no trials that dominate the trial.
It's called that a trial t0 dominates another trial t1 if
all(v0 <= v1) and any(v0 < v1) over their values.
public List<FrozenTrial> BestTrials { get; }
Property Value
BestValue
Return the best objective value in the study. This feature can only be used for single-objective optimization.
public double BestValue { get; }
Property Value
Exceptions
- RuntimeError
The study has multiple objectives.
- ValueError
No completed trial, or no feasible completed trial, is available.
- KeyError
The study has been removed from storage.
Direction
Return the direction of the study. This feature can only be used for single-objective optimization. If your study is multi-objective, use Directions instead.
public StudyDirection Direction { get; }
Property Value
Exceptions
- RuntimeError
The study has multiple objectives.
Directions
Return the directions of the study.
public IReadOnlyList<StudyDirection> Directions { get; }
Property Value
ExecutorOptions
The executor options this study's Optimize(nJobs: …) calls use when a call passes none: which
threads run the trials. null (the default) defers to
Default, read at the start of each call. Optuna has no counterpart.
public TrialExecutorOptions? ExecutorOptions { get; set; }
Property Value
Remarks
The second of the executor's levels: a call's executorOptions argument overrides it, and it
overrides Default. The DI study factory sets it from
DefaultExecutorOptions when it builds the study, so a
value set here afterwards replaces that DI default.
It belongs to this Study object and is not stored: a study opened again with
LoadStudy(string?, BaseStorage, BaseSampler?, BasePruner?) starts with null. Two threads running Optimize
on one study object share it; each call takes its own snapshot when it starts.
MetricNames
Return metric names. Use SetMetricNames(IReadOnlyList<string>) to set the metric names first.
public List<string>? MetricNames { get; }
Property Value
Pruner
The pruner in use.
public BasePruner Pruner { get; set; }
Property Value
Sampler
The sampler in use.
public BaseSampler Sampler { get; set; }
Property Value
StudyName
The study name.
public string StudyName { get; }
Property Value
Trials
Return all trials in the study, ordered by trial number.
This is a short form of GetTrials(deepcopy: true, states: null).
public List<FrozenTrial> Trials { get; }
Property Value
UserAttrs
Return user attributes of the study.
public OrderedDict<string, object?> UserAttrs { get; }
Property Value
Methods
AddTrial(FrozenTrial)
Add trial to study. The trial is validated before being added.
This method should in general be used to add already evaluated trials
(trial.State.IsFinished() == true). To queue trials for evaluation, please
refer to EnqueueTrial(OrderedDict<string, object?>, OrderedDict<string, object?>?, bool).
public void AddTrial(FrozenTrial trial)
Parameters
trialFrozenTrialTrial record or active trial on which the operation acts.
Remarks
Records are inserted one at a time; a later failure does not roll back records already added.
Exceptions
- ValueError
A trial has invalid state, timestamps, values, parameters, or distributions, or the objective count differs from this study.
- KeyError
The study has been removed from storage.
AddTrials(IEnumerable<FrozenTrial>)
Add trials to study. The trials are validated before being added.
public void AddTrials(IEnumerable<FrozenTrial> trials)
Parameters
trialsIEnumerable<FrozenTrial>Trial records to process in enumeration order.
Remarks
Records are inserted one at a time; a later failure does not roll back records already added.
Exceptions
- ValueError
A trial has invalid state, timestamps, values, parameters, or distributions, or the objective count differs from this study.
- KeyError
The study has been removed from storage.
Ask(OrderedDict<string, BaseDistribution>?)
Create a new trial from which hyperparameters can be suggested.
This method is part of an alternative to
Optimize(Func<Trial, double>, int?, double?, int, Type[]?, IEnumerable<Action<Study, FrozenTrial>>?, bool, bool, TrialExecutorOptions?, CancellationToken)
that allows controlling the lifetime of a trial outside the scope of func. Each
call to this method should be followed by a call to Tell(Trial, double, bool)
to finish the created trial.
public Trial Ask(OrderedDict<string, BaseDistribution>? fixedDistributions = null)
Parameters
fixedDistributionsOrderedDict<string, BaseDistribution>A dictionary containing the parameter names and parameter's distributions. Each parameter in this dictionary is automatically suggested for the returned trial, even when the suggest method is not explicitly invoked by the user.
Returns
- Trial
An active trial claimed from the waiting queue or newly created; finish it with Tell.
Remarks
A failure while pre-suggesting fixed distributions can leave an already-created trial running. This manual interface does not start a heartbeat worker.
Exceptions
- ValueError
A supplied distribution or pre-suggested parameter is invalid.
- Exception
Storage or a sampler hook fails while creating or initializing the trial.
EnqueueTrial(OrderedDict<string, object?>, OrderedDict<string, object?>?, bool)
Enqueue a trial with given parameter values. You can fix the next sampling parameters which will be evaluated in your objective function.
public void EnqueueTrial(OrderedDict<string, object?> @params, OrderedDict<string, object?>? userAttrs = null, bool skipIfExists = false)
Parameters
paramsOrderedDict<string, object>Parameter values to pass your objective function.
userAttrsOrderedDict<string, object>A dictionary of user-specific attributes other than
params.skipIfExistsboolWhen
true, prevents duplicate trials from being enqueued again.
Remarks
Duplicate checking and insertion are separate storage operations; concurrent callers can enqueue the same parameters.
GetTrials(bool, IReadOnlyCollection<TrialState>?)
Return all trials in the study, ordered by trial number.
public virtual List<FrozenTrial> GetTrials(bool deepcopy = true, IReadOnlyCollection<TrialState>? states = null)
Parameters
deepcopyboolFlag to control whether to apply deep copy to the trials. Note that if you set the flag to
false, you shouldn't mutate any fields of the returned trials.statesIReadOnlyCollection<TrialState>Trial states to filter on. If
null, include all states.
Returns
- List<FrozenTrial>
Trials in ascending trial-number order, optionally filtered and copied.
Exceptions
- KeyError
The study no longer exists in storage.
Optimize(Func<Trial, IReadOnlyList<double>>, int?, double?, int, Type[]?, IEnumerable<Action<Study, FrozenTrial>>?, bool, bool, TrialExecutorOptions?, CancellationToken)
Optimize a multi-objective function. See Optimize(Func<Trial, double>, int?, double?, int, Type[]?, IEnumerable<Action<Study, FrozenTrial>>?, bool, bool, TrialExecutorOptions?, CancellationToken).
public void Optimize(Func<Trial, IReadOnlyList<double>> func, int? nTrials = null, double? timeout = null, int nJobs = 1, Type[]? @catch = null, IEnumerable<Action<Study, FrozenTrial>>? callbacks = null, bool gcAfterTrial = false, bool showProgressBar = false, TrialExecutorOptions? executorOptions = null, CancellationToken ct = default)
Parameters
funcFunc<Trial, IReadOnlyList<double>>Objective delegate returning a scalar or values in study direction order.
nTrialsint?Maximum trials started by this invocation; null leaves the count unlimited.
timeoutdouble?Maximum scheduling duration in seconds; null leaves elapsed time unlimited.
nJobsintConcurrent trial workers; -1 uses the processor count.
catchType[]Objective exception types to mark failed and continue; other objective exceptions stop optimization.
callbacksIEnumerable<Action<Study, FrozenTrial>>Callbacks invoked after a trial finishes, receiving the study and finished trial.
gcAfterTrialboolWhether to request full garbage collection after each trial.
showProgressBarboolWhether to show supported trial-count or timeout progress.
executorOptionsTrialExecutorOptionsWhich threads run the trials when
nJobsis not 1; null uses ExecutorOptions, then Default. Read once, when the call starts.ctCancellationTokenCancels the run: no trial starts once it is cancelled, the trials in flight finish, then OperationCanceledException is thrown; see the single-objective overload.
Remarks
Running objectives are allowed to finish when the count, timeout, Stop request, or cancellation ends scheduling. Callback failures are not covered by the objective catch list.
Exceptions
- RuntimeError
Optimization is invoked recursively on the same thread.
- ValueError
nJobsis 0 or below -1, as Python'sThreadPoolExecutorrefuses.- ArgumentOutOfRangeException
The executor options in effect return an undefined executor kind or an invalid idle timeout; only a derived TrialExecutorOptions class can.
- OperationCanceledException
ctwas cancelled before the scheduling ended, or an objective threw this exception while it was cancelled. Thrown after every trial in flight is stored; CancellationToken isct.- Exception
An objective raises an exception outside catch, or a callback, sampler, pruner, or storage operation fails; the original failure propagates.
Optimize(Func<Trial, double>, int?, double?, int, Type[]?, IEnumerable<Action<Study, FrozenTrial>>?, bool, bool, TrialExecutorOptions?, CancellationToken)
Optimize an objective function.
Optimization is done by choosing a suitable set of hyperparameter values from a given range. Uses a sampler which implements the task of value suggestion based on a specified distribution. The sampler is specified in CreateStudy(BaseStorage?, BaseSampler?, BasePruner?, string?, StudyDirection?, bool, IEnumerable<StudyDirection>?) and the default choice for the sampler is TPE.
public void Optimize(Func<Trial, double> func, int? nTrials = null, double? timeout = null, int nJobs = 1, Type[]? @catch = null, IEnumerable<Action<Study, FrozenTrial>>? callbacks = null, bool gcAfterTrial = false, bool showProgressBar = false, TrialExecutorOptions? executorOptions = null, CancellationToken ct = default)
Parameters
funcFunc<Trial, double>A callable that implements objective function.
nTrialsint?The number of trials for each process.
nullrepresents no limit in terms of the number of trials.timeoutdouble?Stop study after the given number of second(s).
nullrepresents no limit.nJobsintThe number of parallel jobs. If this argument is set to -1, the number is set to CPU count.
catchType[]A study continues to run even when a trial raises one of the exceptions specified in this argument. Default is an empty array, i.e. the study will stop for any exception except for TrialPruned.
callbacksIEnumerable<Action<Study, FrozenTrial>>List of callback functions that are invoked at the end of each trial, receiving the Study and the resulting FrozenTrial.
gcAfterTrialboolFlag to determine whether to automatically run garbage collection after each trial.
showProgressBarboolFlag to show progress bars or not.
executorOptionsTrialExecutorOptionsWhich threads run the trials when
nJobsis not 1. null (the default) uses ExecutorOptions, then Default, which start as OptunaSharp's reused dedicated threads. Read once, when the call starts. Not part of Optuna's API.ctCancellationTokenCancels the run (OptunaSharp only; Python stops on
KeyboardInterrupt). Once it is cancelled no trial starts, the trials in flight finish and are stored, and this method throws OperationCanceledException carrying it. A running objective may stop early through CancellationToken: its trial is then stored FAIL, whatevercatchholds. An already cancelled token runs no trial. StopSignals turns Ctrl+C and SIGTERM into such a token; a .NET Generic Host passesIHostApplicationLifetime.ApplicationStopping.
Remarks
Running objectives are allowed to finish when the count, timeout, Stop request, or cancellation ends scheduling. Callback failures are not covered by the objective catch list.
Exceptions
- RuntimeError
Optimization is invoked recursively on the same thread.
- ValueError
nJobsis 0 or below -1, as Python'sThreadPoolExecutorrefuses.- ArgumentOutOfRangeException
The executor options in effect return an undefined executor kind or an invalid idle timeout; only a derived TrialExecutorOptions class can, because its setters validate.
- OperationCanceledException
ctwas cancelled before the scheduling ended, or an objective threw this exception while it was cancelled. Thrown after every trial in flight is stored; CancellationToken isct.- Exception
An objective raises an exception outside catch, or a callback, sampler, pruner, or storage operation fails; the original failure propagates.
SetMetricNames(IReadOnlyList<string>)
Set metric names. This method names each dimension of the returned values of the objective function. It is particularly useful in multi-objective optimization. (Experimental since Optuna 3.2.0.)
public void SetMetricNames(IReadOnlyList<string> metricNames)
Parameters
metricNamesIReadOnlyList<string>One display name per objective, in objective order.
Exceptions
- ValueError
The number of names differs from the number of objectives.
SetUserAttr(IReadOnlyDictionary<string, object?>)
Optuna's set_user_attr name for SetUserAttrs(IReadOnlyDictionary<string, object?>): several user attributes in one call.
public void SetUserAttr(IReadOnlyDictionary<string, object?> attrs)
Parameters
attrsIReadOnlyDictionary<string, object>The attributes, set in enumeration order. The values should be JSON serializable.
Exceptions
- ArgumentNullException
attrsis null.- KeyError
The study no longer exists in storage; nothing is set.
- Exception
A configured storage fails; its exception propagates.
SetUserAttr(string, object?)
Set a user attribute to the study.
public void SetUserAttr(string key, object? value)
Parameters
keystringA key string of the attribute.
valueobjectA value of the attribute. The value should be JSON serializable.
SetUserAttrs(IReadOnlyDictionary<string, object?>)
Set several user attributes to the study at once (Rustuna's set_user_attrs; plan 13, item S8), with one
storage call: the stored result is what SetUserAttr(string, object?) once per entry leaves.
public void SetUserAttrs(IReadOnlyDictionary<string, object?> attrs)
Parameters
attrsIReadOnlyDictionary<string, object>The attributes, set in enumeration order. The values should be JSON serializable.
Exceptions
- ArgumentNullException
attrsis null.- KeyError
The study no longer exists in storage; nothing is set.
- Exception
A configured storage fails; its exception propagates.
Stop()
Exit from the current optimization loop after the running trials finish.
This method lets the running Optimize(Func<Trial, double>, int?, double?, int, Type[]?, IEnumerable<Action<Study, FrozenTrial>>?, bool, bool, TrialExecutorOptions?, CancellationToken) method return immediately after all trials which the method spawned finishes. This method only works when it is called inside an objective function or callback.
public virtual void Stop()
Exceptions
- RuntimeError
The call is outside this thread's active optimization loop.
Tell(Trial, IReadOnlyList<double>?, TrialState?, bool)
Finish a trial created with Ask(OrderedDict<string, BaseDistribution>?).
public FrozenTrial Tell(Trial trial, IReadOnlyList<double>? values = null, TrialState? state = null, bool skipIfFinished = false)
Parameters
trialTrialA Trial object.
valuesIReadOnlyList<double>Optional objective value or a sequence of such values in case the study is used for multi-objective optimization. Argument must be provided if
stateis COMPLETE and should benullif it is FAIL or PRUNED.stateTrialState?State to be reported. Must be
null, COMPLETE, FAIL or PRUNED. Ifnull, it will be updated to COMPLETE or FAIL depending on whether validation forvaluesreported succeed or not.skipIfFinishedboolFlag to control whether exception should be raised when values for already finished trial are told. If
true, tell is skipped without any error when the trial is already finished.
Returns
- FrozenTrial
A FrozenTrial representing the resulting trial (deep-copied).
Remarks
Thread-safe for one trial: tells of it through this study's storage instance, from any thread and from
the optimize loop, act in turn. Exactly one stores its result and reaches the sampler's AfterTrial;
every later one is refused with Cannot tell a COMPLETE trial. (or the trial's state), or skipped
when skipIfFinished is set. Optuna behaves the same in practice, because its GIL makes that order
near certain; without the ordering, two tells could both pass the check and both reach AfterTrial.
Exceptions
- ValueError
The trial number is invalid, the trial is not running, its requested state and values disagree, or explicit completion has invalid objective values.
- KeyError
The requested trial cannot be found.
- Exception
The sampler AfterTrial hook or the storage backend fails.
- UpdateFinishedTrialError
Another storage instance or process finished the trial while this tell was writing it; the storage admits one of them.
Tell(Trial, double, bool)
Finish a trial created with Ask(OrderedDict<string, BaseDistribution>?) reporting a single objective value.
public FrozenTrial Tell(Trial trial, double value, bool skipIfFinished = false)
Parameters
trialTrialTrial record or active trial on which the operation acts.
valuedoubleSingle objective value used to infer the completed state.
skipIfFinishedboolWhether to return an already-finished record without changing its state or values.
Returns
- FrozenTrial
A FrozenTrial representing the resulting trial (deep-copied).
Remarks
Thread-safe for one trial: tells of it through this study's storage instance, from any thread and from
the optimize loop, act in turn. Exactly one stores its result and reaches the sampler's AfterTrial;
every later one is refused with Cannot tell a COMPLETE trial. (or the trial's state), or skipped
when skipIfFinished is set. Optuna behaves the same in practice, because its GIL makes that order
near certain; without the ordering, two tells could both pass the check and both reach AfterTrial.
Exceptions
- ValueError
The trial number is invalid, the trial is not running, its requested state and values disagree, or explicit completion has invalid objective values.
- KeyError
The requested trial cannot be found.
- Exception
The sampler AfterTrial hook or the storage backend fails.
- UpdateFinishedTrialError
Another storage instance or process finished the trial while this tell was writing it; the storage admits one of them.
Tell(int, IReadOnlyList<double>?, TrialState?, bool)
Finish a trial identified by its number.
public FrozenTrial Tell(int trialNumber, IReadOnlyList<double>? values = null, TrialState? state = null, bool skipIfFinished = false)
Parameters
trialNumberintZero-based trial number within the study, not a storage identifier.
valuesIReadOnlyList<double>Objective values in study direction order, or null when no objective values are recorded.
stateTrialState?Complete, Pruned, Fail, or null to infer Complete or Fail from objective-value validation.
skipIfFinishedboolWhether to return an already-finished record without changing its state or values.
Returns
- FrozenTrial
A detached copy of the resulting stored trial.
Remarks
Thread-safe for one trial: tells of it through this study's storage instance, from any thread and from
the optimize loop, act in turn. Exactly one stores its result and reaches the sampler's AfterTrial;
every later one is refused with Cannot tell a COMPLETE trial. (or the trial's state), or skipped
when skipIfFinished is set. Optuna behaves the same in practice, because its GIL makes that order
near certain; without the ordering, two tells could both pass the check and both reach AfterTrial.
Exceptions
- ValueError
The trial number is invalid, the trial is not running, its requested state and values disagree, or explicit completion has invalid objective values.
- KeyError
The requested trial cannot be found.
- Exception
The sampler AfterTrial hook or the storage backend fails.
- UpdateFinishedTrialError
Another storage instance or process finished the trial while this tell was writing it; the storage admits one of them.
Tell(int, double, bool)
Finish a trial identified by its number, reporting a single objective value.
public FrozenTrial Tell(int trialNumber, double value, bool skipIfFinished = false)
Parameters
trialNumberintZero-based trial number within the study, not a storage identifier.
valuedoubleSingle objective value used to infer the completed state.
skipIfFinishedboolWhether to return an already-finished record without changing its state or values.
Returns
- FrozenTrial
A detached copy of the resulting stored trial.
Remarks
Thread-safe for one trial: tells of it through this study's storage instance, from any thread and from
the optimize loop, act in turn. Exactly one stores its result and reaches the sampler's AfterTrial;
every later one is refused with Cannot tell a COMPLETE trial. (or the trial's state), or skipped
when skipIfFinished is set. Optuna behaves the same in practice, because its GIL makes that order
near certain; without the ordering, two tells could both pass the check and both reach AfterTrial.
Exceptions
- ValueError
The trial number is invalid, the trial is not running, its requested state and values disagree, or explicit completion has invalid objective values.
- KeyError
The requested trial cannot be found.
- Exception
The sampler AfterTrial hook or the storage backend fails.
- UpdateFinishedTrialError
Another storage instance or process finished the trial while this tell was writing it; the storage admits one of them.
TrialsDataframe(string[]?, bool)
Export trials as a TrialsTable (the Core-tier analog of
Study.trials_dataframe; the pandas-equivalent adapter is a Tier X package).
If there are no trials, an empty table is returned.
public TrialsTable TrialsDataframe(string[]? attrs = null, bool multiIndex = false)
Parameters
attrsstring[]Field names of FrozenTrial to include as columns.
multiIndexboolWhether the returned table keeps two-level column labels; when
false, hierarchical columns such as(params, x)are flattened toparams_x.
Returns
- TrialsTable
An ordered TrialsTable of selected trial fields; this port does not require a pandas runtime.