optuna.storages.RDBStorage

class optuna.storages.RDBStorage(url, engine_kwargs=None, skip_compatibility_check=False, *, heartbeat_interval=None, grace_period=None, failed_trial_callback=None)[source]

Storage class for RDB backend.

Note that library users can instantiate this class, but the attributes provided by this class are not supposed to be directly accessed by them.

Example

Create an RDBStorage instance with customized pool_size and timeout settings.

import optuna


def objective(trial):
    x = trial.suggest_float("x", -100, 100)
    return x ** 2


storage = optuna.storages.RDBStorage(
    url="sqlite:///:memory:",
    engine_kwargs={"pool_size": 20, "connect_args": {"timeout": 10}},
)

study = optuna.create_study(storage=storage)
study.optimize(objective, n_trials=10)
Parameters
  • url – URL of the storage.

  • engine_kwargs – A dictionary of keyword arguments that is passed to sqlalchemy.engine.create_engine function.

  • skip_compatibility_check – Flag to skip schema compatibility check if set to True.

  • heartbeat_interval – Interval to record the heartbeat. It is recorded every interval seconds.

  • grace_period – Grace period before a running trial is failed from the last heartbeat. If it is None, the grace period will be 2 * heartbeat_interval.

  • failed_trial_callback

    A callback function that is invoked after failing each stale trial. The function must accept two parameters with the following types in this order: Study and FrozenTrial.

    Note

    The procedure to fail existing stale trials is called just before asking the study for a new trial.

Note

If you use MySQL, pool_pre_ping will be set to True by default to prevent connection timeout. You can turn it off with engine_kwargs['pool_pre_ping']=False, but it is recommended to keep the setting if execution time of your objective function is longer than the wait_timeout of your MySQL configuration.

Raises
  • ValueError – If the given heartbeat_interval or grace_period is not a positive integer.

  • RuntimeError – When a process tries to finish a trial that has already been set to FAIL by heartbeat.

Methods

check_trial_is_updatable(trial_id, trial_state)

Check whether a trial state is updatable.

create_new_study([study_name])

Create a new study from a name.

create_new_trial(study_id[, template_trial])

Create and add a new trial to a study.

delete_study(study_id)

Delete a study.

fail_stale_trials(study_id)

Fail stale trials.

get_all_study_summaries()

Read a list of StudySummary objects.

get_all_trials(study_id[, deepcopy, states])

Read all trials in a study.

get_all_versions()

Return the schema version list.

get_best_trial(study_id)

Return the trial with the best value in a study.

get_current_version()

Return the schema version currently used by this storage.

get_failed_trial_callback()

Get the failed trial callback function.

get_head_version()

Return the latest schema version.

get_heartbeat_interval()

Get the heartbeat interval if it is set.

get_n_trials(study_id[, state])

Count the number of trials in a study.

get_study_directions(study_id)

Read whether a study maximizes or minimizes an objective.

get_study_id_from_name(study_name)

Read the ID of a study.

get_study_id_from_trial_id(trial_id)

Read the ID of a study to which a trial belongs.

get_study_name_from_id(study_id)

Read the study name of a study.

get_study_system_attrs(study_id)

Read the optuna-internal attributes of a study.

get_study_user_attrs(study_id)

Read the user-defined attributes of a study.

get_trial(trial_id)

Read a trial.

get_trial_id_from_study_id_trial_number(…)

Read the trial ID of a trial.

get_trial_number_from_id(trial_id)

Read the trial number of a trial.

get_trial_param(trial_id, param_name)

Read the parameter of a trial.

get_trial_params(trial_id)

Read the parameter dictionary of a trial.

get_trial_system_attrs(trial_id)

Read the optuna-internal attributes of a trial.

get_trial_user_attrs(trial_id)

Read the user-defined attributes of a trial.

is_heartbeat_enabled()

Check whether the storage enables the heartbeat.

read_trials_from_remote_storage(study_id)

Make an internal cache of trials up-to-date.

record_heartbeat(trial_id)

Record the heartbeat of the trial.

remove_session()

Removes the current session.

set_study_directions(study_id, directions)

Register optimization problem directions to a study.

set_study_system_attr(study_id, key, value)

Register an optuna-internal attribute to a study.

set_study_user_attr(study_id, key, value)

Register a user-defined attribute to a study.

set_trial_intermediate_value(trial_id, step, …)

Report an intermediate value of an objective function.

set_trial_param(trial_id, param_name, …)

Set a parameter to a trial.

set_trial_state(trial_id, state)

Update the state of a trial.

set_trial_system_attr(trial_id, key, value)

Set an optuna-internal attribute to a trial.

set_trial_user_attr(trial_id, key, value)

Set a user-defined attribute to a trial.

set_trial_values(trial_id, values)

Set return values of an objective function.

upgrade()

Upgrade the storage schema.

check_trial_is_updatable(trial_id, trial_state)

Check whether a trial state is updatable.

Parameters
  • trial_id (int) – ID of the trial. Only used for an error message.

  • trial_state (optuna.trial._state.TrialState) – Trial state to check.

Raises

RuntimeError – If the trial is already finished.

Return type

None

create_new_study(study_name=None)[source]

Create a new study from a name.

If no name is specified, the storage class generates a name. The returned study ID is unique among all current and deleted studies.

Parameters

study_name (Optional[str]) – Name of the new study to create.

Returns

ID of the created study.

Raises

optuna.exceptions.DuplicatedStudyError – If a study with the same study_name already exists.

Return type

int

create_new_trial(study_id, template_trial=None)[source]

Create and add a new trial to a study.

The returned trial ID is unique among all current and deleted trials.

Parameters
  • study_id (int) – ID of the study.

  • template_trial (Optional[optuna.trial._frozen.FrozenTrial]) – Template FronzenTrial with default user-attributes, system-attributes, intermediate-values, and a state.

Returns

ID of the created trial.

Raises

KeyError – If no study with the matching study_id exists.

Return type

int

delete_study(study_id)[source]

Delete a study.

Parameters

study_id (int) – ID of the study.

Raises

KeyError – If no study with the matching study_id exists.

Return type

None

fail_stale_trials(study_id)[source]

Fail stale trials.

The running trials whose heartbeat has not been updated for a long time will be failed, that is, those states will be changed to FAIL. The grace period is 2 * heartbeat_interval.

Parameters

study_id (int) – ID of the related study.

Returns

List of trial IDs of the failed trials.

Return type

List[int]

get_all_study_summaries()[source]

Read a list of StudySummary objects.

Returns

A list of StudySummary objects.

Return type

List[optuna.study._study_summary.StudySummary]

get_all_trials(study_id, deepcopy=True, states=None)[source]

Read all trials in a study.

Parameters
  • study_id (int) – ID of the study.

  • deepcopy (bool) – Whether to copy the list of trials before returning. Set to True if you intend to update the list or elements of the list.

  • states (Optional[Tuple[optuna.trial._state.TrialState, ..]]) – Trial states to filter on. If None, include all states.

Returns

List of trials in the study.

Raises

KeyError – If no study with the matching study_id exists.

Return type

List[optuna.trial._frozen.FrozenTrial]

get_all_versions()[source]

Return the schema version list.

Return type

List[str]

get_best_trial(study_id)[source]

Return the trial with the best value in a study.

This method is valid only during single-objective optimization.

Parameters

study_id (int) – ID of the study.

Returns

The trial with the best objective value among all finished trials in the study.

Raises
  • KeyError – If no study with the matching study_id exists.

  • RuntimeError – If the study has more than one direction.

  • ValueError – If no trials have been completed.

Return type

optuna.trial._frozen.FrozenTrial

get_current_version()[source]

Return the schema version currently used by this storage.

Return type

str

get_failed_trial_callback()[source]

Get the failed trial callback function.

Returns

The failed trial callback function if it is set, otherwise None.

Return type

Optional[Callable[[optuna.study.study.Study, optuna.trial._frozen.FrozenTrial], None]]

get_head_version()[source]

Return the latest schema version.

Return type

str

get_heartbeat_interval()[source]

Get the heartbeat interval if it is set.

Returns

The heartbeat interval if it is set, otherwise None.

Return type

Optional[int]

get_n_trials(study_id, state=None)

Count the number of trials in a study.

Parameters
  • study_id (int) – ID of the study.

  • state (Optional[Union[Tuple[optuna.trial._state.TrialState, ..], optuna.trial._state.TrialState]]) – Trial states to filter on. If None, include all states.

Returns

Number of trials in the study.

Raises

KeyError – If no study with the matching study_id exists.

Return type

int

get_study_directions(study_id)[source]

Read whether a study maximizes or minimizes an objective.

Parameters

study_id (int) – ID of a study.

Returns

Optimization directions list of the study.

Raises

KeyError – If no study with the matching study_id exists.

Return type

List[optuna.study._study_direction.StudyDirection]

get_study_id_from_name(study_name)[source]

Read the ID of a study.

Parameters

study_name (str) – Name of the study.

Returns

ID of the study.

Raises

KeyError – If no study with the matching study_name exists.

Return type

int

get_study_id_from_trial_id(trial_id)[source]

Read the ID of a study to which a trial belongs.

Parameters

trial_id (int) – ID of the trial.

Returns

ID of the study.

Raises

KeyError – If no trial with the matching trial_id exists.

Return type

int

get_study_name_from_id(study_id)[source]

Read the study name of a study.

Parameters

study_id (int) – ID of the study.

Returns

Name of the study.

Raises

KeyError – If no study with the matching study_id exists.

Return type

str

get_study_system_attrs(study_id)[source]

Read the optuna-internal attributes of a study.

Parameters

study_id (int) – ID of the study.

Returns

Dictionary with the optuna-internal attributes of the study.

Raises

KeyError – If no study with the matching study_id exists.

Return type

Dict[str, Any]

get_study_user_attrs(study_id)[source]

Read the user-defined attributes of a study.

Parameters

study_id (int) – ID of the study.

Returns

Dictionary with the user attributes of the study.

Raises

KeyError – If no study with the matching study_id exists.

Return type

Dict[str, Any]

get_trial(trial_id)[source]

Read a trial.

Parameters

trial_id (int) – ID of the trial.

Returns

Trial with a matching trial ID.

Raises

KeyError – If no trial with the matching trial_id exists.

Return type

optuna.trial._frozen.FrozenTrial

get_trial_id_from_study_id_trial_number(study_id, trial_number)[source]

Read the trial ID of a trial.

Parameters
  • study_id (int) – ID of the study.

  • trial_number (int) – Number of the trial.

Returns

ID of the trial.

Raises

KeyError – If no trial with the matching study_id and trial_number exists.

Return type

int

get_trial_number_from_id(trial_id)[source]

Read the trial number of a trial.

Note

The trial number is only unique within a study, and is sequential.

Parameters

trial_id (int) – ID of the trial.

Returns

Number of the trial.

Raises

KeyError – If no trial with the matching trial_id exists.

Return type

int

get_trial_param(trial_id, param_name)[source]

Read the parameter of a trial.

Parameters
  • trial_id (int) – ID of the trial.

  • param_name (str) – Name of the parameter.

Returns

Internal representation of the parameter.

Raises

KeyError – If no trial with the matching trial_id exists. If no such parameter exists.

Return type

float

get_trial_params(trial_id)

Read the parameter dictionary of a trial.

Parameters

trial_id (int) – ID of the trial.

Returns

Dictionary of a parameters. Keys are parameter names and values are internal representations of the parameter values.

Raises

KeyError – If no trial with the matching trial_id exists.

Return type

Dict[str, Any]

get_trial_system_attrs(trial_id)[source]

Read the optuna-internal attributes of a trial.

Parameters

trial_id (int) – ID of the trial.

Returns

Dictionary with the optuna-internal attributes of the trial.

Raises

KeyError – If no trial with the matching trial_id exists.

Return type

Dict[str, Any]

get_trial_user_attrs(trial_id)[source]

Read the user-defined attributes of a trial.

Parameters

trial_id (int) – ID of the trial.

Returns

Dictionary with the user-defined attributes of the trial.

Raises

KeyError – If no trial with the matching trial_id exists.

Return type

Dict[str, Any]

is_heartbeat_enabled()

Check whether the storage enables the heartbeat.

Returns

True if the storage supports the heartbeat and the return value of get_heartbeat_interval() is an integer, otherwise False.

Return type

bool

read_trials_from_remote_storage(study_id)[source]

Make an internal cache of trials up-to-date.

Parameters

study_id (int) – ID of the study.

Raises

KeyError – If no study with the matching study_id exists.

Return type

None

record_heartbeat(trial_id)[source]

Record the heartbeat of the trial.

Parameters

trial_id (int) – ID of the trial.

Return type

None

remove_session()[source]

Removes the current session.

A session is stored in SQLAlchemy’s ThreadLocalRegistry for each thread. This method closes and removes the session which is associated to the current thread. Particularly, under multi-thread use cases, it is important to call this method from each thread. Otherwise, all sessions and their associated DB connections are destructed by a thread that occasionally invoked the garbage collector. By default, it is not allowed to touch a SQLite connection from threads other than the thread that created the connection. Therefore, we need to explicitly close the connection from each thread.

Return type

None

set_study_directions(study_id, directions)[source]

Register optimization problem directions to a study.

Parameters
  • study_id (int) – ID of the study.

  • directions (Sequence[optuna.study._study_direction.StudyDirection]) – A sequence of direction whose element is either MAXIMIZE or MINIMIZE.

Raises
  • KeyError – If no study with the matching study_id exists.

  • ValueError – If the directions are already set and the each coordinate of passed directions is the opposite direction or NOT_SET.

Return type

None

set_study_system_attr(study_id, key, value)[source]

Register an optuna-internal attribute to a study.

This method overwrites any existing attribute.

Parameters
  • study_id (int) – ID of the study.

  • key (str) – Attribute key.

  • value (Any) – Attribute value. It should be JSON serializable.

Raises

KeyError – If no study with the matching study_id exists.

Return type

None

set_study_user_attr(study_id, key, value)[source]

Register a user-defined attribute to a study.

This method overwrites any existing attribute.

Parameters
  • study_id (int) – ID of the study.

  • key (str) – Attribute key.

  • value (Any) – Attribute value. It should be JSON serializable.

Raises

KeyError – If no study with the matching study_id exists.

Return type

None

set_trial_intermediate_value(trial_id, step, intermediate_value)[source]

Report an intermediate value of an objective function.

This method overwrites any existing intermediate value associated with the given step.

Parameters
  • trial_id (int) – ID of the trial.

  • step (int) – Step of the trial (e.g., the epoch when training a neural network).

  • intermediate_value (float) – Intermediate value corresponding to the step.

Raises
  • KeyError – If no trial with the matching trial_id exists.

  • RuntimeError – If the trial is already finished.

Return type

None

set_trial_param(trial_id, param_name, param_value_internal, distribution)[source]

Set a parameter to a trial.

Parameters
  • trial_id (int) – ID of the trial.

  • param_name (str) – Name of the parameter.

  • param_value_internal (float) – Internal representation of the parameter value.

  • distribution (optuna.distributions.BaseDistribution) – Sampled distribution of the parameter.

Raises
  • KeyError – If no trial with the matching trial_id exists.

  • RuntimeError – If the trial is already finished.

Return type

None

set_trial_state(trial_id, state)[source]

Update the state of a trial.

Parameters
  • trial_id (int) – ID of the trial.

  • state (optuna.trial._state.TrialState) – New state of the trial.

Returns

True if the state is successfully updated. False if the state is kept the same. The latter happens when this method tries to update the state of RUNNING trial to RUNNING.

Raises
  • KeyError – If no trial with the matching trial_id exists.

  • RuntimeError – If the trial is already finished.

Return type

bool

set_trial_system_attr(trial_id, key, value)[source]

Set an optuna-internal attribute to a trial.

This method overwrites any existing attribute.

Parameters
  • trial_id (int) – ID of the trial.

  • key (str) – Attribute key.

  • value (Any) – Attribute value. It should be JSON serializable.

Raises
  • KeyError – If no trial with the matching trial_id exists.

  • RuntimeError – If the trial is already finished.

Return type

None

set_trial_user_attr(trial_id, key, value)[source]

Set a user-defined attribute to a trial.

This method overwrites any existing attribute.

Parameters
  • trial_id (int) – ID of the trial.

  • key (str) – Attribute key.

  • value (Any) – Attribute value. It should be JSON serializable.

Raises
  • KeyError – If no trial with the matching trial_id exists.

  • RuntimeError – If the trial is already finished.

Return type

None

set_trial_values(trial_id, values)[source]

Set return values of an objective function.

This method overwrites any existing trial values.

Parameters
  • trial_id (int) – ID of the trial.

  • values (Sequence[float]) – Values of the objective function.

Raises
  • KeyError – If no trial with the matching trial_id exists.

  • RuntimeError – If the trial is already finished.

Return type

None

upgrade()[source]

Upgrade the storage schema.

Return type

None