MCPcopy
hub / github.com/optuna/optuna / add_trial

Method add_trial

optuna/study/study.py:936–1006  ·  view source on GitHub ↗

Add trial to study. The trial is validated before being added. Example: .. testcode:: import optuna from optuna.distributions import FloatDistribution def objective(trial): x = trial.suggest_float("

(self, trial: FrozenTrial)

Source from the content-addressed store, hash-verified

934 )
935
936 def add_trial(self, trial: FrozenTrial) -> None:
937 """Add trial to study.
938
939 The trial is validated before being added.
940
941 Example:
942
943 .. testcode::
944
945 import optuna
946 from optuna.distributions import FloatDistribution
947
948
949 def objective(trial):
950 x = trial.suggest_float("x", 0, 10)
951 return x**2
952
953
954 study = optuna.create_study()
955 assert len(study.trials) == 0
956
957 trial = optuna.trial.create_trial(
958 params={"x": 2.0},
959 distributions={"x": FloatDistribution(0, 10)},
960 value=4.0,
961 )
962
963 study.add_trial(trial)
964 assert len(study.trials) == 1
965
966 study.optimize(objective, n_trials=3)
967 assert len(study.trials) == 4
968
969 other_study = optuna.create_study()
970
971 for trial in study.trials:
972 other_study.add_trial(trial)
973 assert len(other_study.trials) == len(study.trials)
974
975 other_study.optimize(objective, n_trials=2)
976 assert len(other_study.trials) == len(study.trials) + 2
977
978 .. seealso::
979
980 This method should in general be used to add already evaluated trials
981 (``trial.state.is_finished() == True``). To queue trials for evaluation,
982 please refer to :func:`~optuna.study.Study.enqueue_trial`.
983
984 .. seealso::
985
986 See :func:`~optuna.trial.create_trial` for how to create trials.
987
988 .. seealso::
989 Please refer to :ref:`add_trial_tutorial` for the tutorial of specifying
990 hyperparameters with the evaluated value manually.
991
992 Args:
993 trial: Trial to add.

Calls 2

_validateMethod · 0.80
create_new_trialMethod · 0.45