Skip to content

Instantly share code, notes, and snippets.

@c-bata
Created October 1, 2024 01:57
Show Gist options
  • Save c-bata/d76d810ceffd5d83fd695d7d7f942bc3 to your computer and use it in GitHub Desktop.
Save c-bata/d76d810ceffd5d83fd695d7d7f942bc3 to your computer and use it in GitHub Desktop.

Example code

import numpy as np
import optuna
import optunahub


def objective(trial: optuna.Trial) -> float:
    x = trial.suggest_float("x", -50, -40)
    y = trial.suggest_int("y", -5, 5)
    return (x + 43)**2 + (y - 2)**2


if __name__ == "__main__":
    module = optunahub.load_local_module(package="samplers/user_prior_cmaes", registry_root="./package")
    sampler = module.UserPriorCmaEsSampler(param_names=["x", "y"], mu0=np.array([3., -48.]), cov0=np.diag([0.2, 2.0]))
    study = optuna.create_study(sampler=sampler)
    study.optimize(objective, n_trials=20)
    print(study.best_trial.value, study.best_trial.params)

Output

$ python example_160.py
[I 2024-10-01 10:55:23,307] A new study created in memory with name: no-name-1e3cd70e-b73b-4b65-929e-9e8df222e3ee
[I 2024-10-01 10:55:23,312] Trial 0 finished with value: 2.360256325958632 and parameters: {'x': -41.833699727360646, 'y': 1}. Best is trial 0 with value: 2.360256325958632.
[W 2024-10-01 10:55:23,312] Trial 1 failed with parameters: {} because of the following error: AssertionError('invalid bounds').
Traceback (most recent call last):
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
    value_or_values = func(trial)
                      ^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/example_160.py", line 7, in objective
    x = trial.suggest_float("x", -50, -40)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/trial/_trial.py", line 161, in suggest_float
    suggested_value = self._suggest(name, distribution)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/trial/_trial.py", line 627, in _suggest
    elif self._is_relative_param(name, distribution):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/trial/_trial.py", line 659, in _is_relative_param
    if name not in self.relative_params:
                   ^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/trial/_trial.py", line 71, in relative_params
    self._relative_params = self.study.sampler.sample_relative(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/./package/samplers/user_prior_cmaes/sampler.py", line 115, in sample_relative
    return super().sample_relative(study=study, trial=trial, search_space=search_space)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/samplers/_cmaes.py", line 417, in sample_relative
    optimizer = self._init_optimizer(
                ^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/./package/samplers/user_prior_cmaes/sampler.py", line 194, in _init_optimizer
    return cmaes.CMA(
           ^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/cmaes/_cma.py", line 186, in __init__
    assert bounds is None or _is_valid_bounds(bounds, mean), "invalid bounds"
           ^^^^^^^^^^^^^^
AssertionError: invalid bounds
[W 2024-10-01 10:55:23,323] Trial 1 failed with value None.
Traceback (most recent call last):
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/example_160.py", line 16, in <module>
    study.optimize(objective, n_trials=20)
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/study/study.py", line 475, in optimize
    _optimize(
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 63, in _optimize
    _optimize_sequential(
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 160, in _optimize_sequential
    frozen_trial = _run_trial(study, func, catch)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 248, in _run_trial
    raise func_err
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
    value_or_values = func(trial)
                      ^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/example_160.py", line 7, in objective
    x = trial.suggest_float("x", -50, -40)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/trial/_trial.py", line 161, in suggest_float
    suggested_value = self._suggest(name, distribution)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/trial/_trial.py", line 627, in _suggest
    elif self._is_relative_param(name, distribution):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/trial/_trial.py", line 659, in _is_relative_param
    if name not in self.relative_params:
                   ^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/trial/_trial.py", line 71, in relative_params
    self._relative_params = self.study.sampler.sample_relative(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/./package/samplers/user_prior_cmaes/sampler.py", line 115, in sample_relative
    return super().sample_relative(study=study, trial=trial, search_space=search_space)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/optuna/samplers/_cmaes.py", line 417, in sample_relative
    optimizer = self._init_optimizer(
                ^^^^^^^^^^^^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/./package/samplers/user_prior_cmaes/sampler.py", line 194, in _init_optimizer
    return cmaes.CMA(
           ^^^^^^^^^^
  File "/home/cbata/go/src/github.com/optuna/optunahub-registry/venv/lib/python3.11/site-packages/cmaes/_cma.py", line 186, in __init__
    assert bounds is None or _is_valid_bounds(bounds, mean), "invalid bounds"
           ^^^^^^^^^^^^^^
AssertionError: invalid bounds

pip freeze

$ pip freeze
alembic==1.13.3
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.3.2
cmaes==0.11.1
colorlog==6.8.2
cryptography==43.0.1
Deprecated==1.2.14
ga4mp==2.0.4
greenlet==3.1.1
idna==3.10
Mako==1.3.5
MarkupSafe==2.1.5
numpy==2.1.1
optuna==4.0.0
optunahub==0.1.0
packaging==24.1
pycparser==2.22
PyGithub==2.4.0
PyJWT==2.9.0
PyNaCl==1.5.0
PyYAML==6.0.2
requests==2.32.3
SQLAlchemy==2.0.35
tqdm==4.66.5
typing_extensions==4.12.2
urllib3==2.2.3
wrapt==1.16.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment