Table of Contents

Class MaxTrialsCallback

Namespace
OptunaSharp
Assembly
OptunaSharp.dll

Set a maximum number of trials before ending the study. Port of optuna/_callbacks.py: MaxTrialsCallback (exposed as optuna.study.MaxTrialsCallback).

While the nTrials argument of Study.Optimize sets the number of trials that will be run, you may want to continue running until you have a certain number of successfully completed trials, or stop the study when you have a certain number of trials that fail. This MaxTrialsCallback class allows you to set a maximum number of trials for a particular TrialState before stopping the study.

public class MaxTrialsCallback
Inheritance
MaxTrialsCallback
Inherited Members

Examples

var study = Optuna.CreateStudy();
study.Optimize(
    trial => { var x = trial.SuggestFloat("x", -1, 1); return x * x; },
    callbacks: new[]
    {
        (Action<Study, FrozenTrial>)new MaxTrialsCallback(10, new[] { TrialState.Complete }),
    });

Constructors

MaxTrialsCallback(int)

Count only trials in Complete towards the limit — Optuna's default states=(TrialState.COMPLETE,).

public MaxTrialsCallback(int nTrials)

Parameters

nTrials int

The max number of trials. Must be set to an integer.

MaxTrialsCallback(int, IReadOnlyCollection<TrialState>?)

Creates a callback that stops after the selected trial-state count reaches the limit.

public MaxTrialsCallback(int nTrials, IReadOnlyCollection<TrialState>? states)

Parameters

nTrials int

The max number of trials. Must be set to an integer.

states IReadOnlyCollection<TrialState>

The set of TrialState counted towards the max-trials limit. Optuna's default is (TrialState.COMPLETE,) — use the single-argument constructor for that. If null, count trials in all states.

Methods

Invoke(Study, FrozenTrial)

Python call(study, trial).

public void Invoke(Study study, FrozenTrial trial)

Parameters

study Study

Study whose history and storage are used by the operation.

trial FrozenTrial

Most recently finished trial; the count is read from the entire study history.

Exceptions

RuntimeError

The limit is reached but the callback was invoked outside an optimization loop.

Operators

implicit operator Action<Study, FrozenTrial>(MaxTrialsCallback)

Allows passing the callback where an Action<T1, T2> is expected.

public static implicit operator Action<Study, FrozenTrial>(MaxTrialsCallback callback)

Parameters

callback MaxTrialsCallback

Callback instance to expose as a delegate.

Returns

Action<Study, FrozenTrial>

A delegate invoking this callback instance.