Beispiel #1
0
def test_split_and_concat_optimizer_string(dummy_optimizer_str: str,
                                           attr_len: int) -> None:
    with patch("optuna.samplers._cmaes._SYSTEM_ATTR_MAX_LENGTH", 5):
        attrs = _split_optimizer_str(dummy_optimizer_str)
        assert len(attrs) == attr_len
        actual = _concat_optimizer_attrs(attrs)
        assert dummy_optimizer_str == actual
Beispiel #2
0
def test_restore_optimizer_from_substrings() -> None:
    sampler = optuna.samplers.CmaEsSampler()
    optimizer = CMA(np.zeros(10), sigma=1.3)
    optimizer_str = pickle.dumps(optimizer).hex()

    system_attrs: Dict[str, Any] = _split_optimizer_str(optimizer_str)
    assert len(system_attrs) > 1
    system_attrs["cma:n_restarts"] = 1

    completed_trials = [
        create_trial(state=TrialState.COMPLETE, value=0.1),
        create_trial(
            state=TrialState.COMPLETE,
            value=0.1,
            system_attrs=system_attrs,
        ),
        create_trial(state=TrialState.COMPLETE, value=0.1),
    ]
    optimizer, n_restarts = sampler._restore_optimizer(completed_trials)
    assert isinstance(optimizer, CMA)
    assert n_restarts == 1