Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
btjanaka committed Dec 2, 2024
1 parent bee7d68 commit 1766c4b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
22 changes: 8 additions & 14 deletions package/visualization/plot_pyribs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,23 @@ from optuna.study import StudyDirection
module = optunahub.load_module("samplers/cmamae")
CmaMaeSampler = module.CmaMaeSampler

plot_pyribs = optunahub.load_module(package="visualization/plot_pyribs",)
plot_pyribs = optunahub.load_module(package="visualization/plot_pyribs")
plot_grid_archive_heatmap = plot_pyribs.plot_grid_archive_heatmap


def objective(trial: optuna.trial.Trial) -> tuple[float, float, float]:
def objective(trial: optuna.trial.Trial) -> float:
"""Returns an objective followed by two measures."""
x = trial.suggest_float("x", -10, 10)
y = trial.suggest_float("y", -10, 10)
return x**2 + y**2, x, y
trial.set_user_attr("m0", 2 * x)
trial.set_user_attr("m1", x + y)
return x**2 + y**2


if __name__ == "__main__":
sampler = CmaMaeSampler(
param_names=["x", "y"],
measure_names=["m0", "m1"],
archive_dims=[20, 20],
archive_ranges=[(-1, 1), (-1, 1)],
archive_learning_rate=0.1,
Expand All @@ -56,21 +59,12 @@ if __name__ == "__main__":
emitter_sigma0=0.1,
emitter_batch_size=20,
)
study = optuna.create_study(
sampler=sampler,
directions=[
StudyDirection.MINIMIZE,
# The remaining directions are for the measures, which do not have
# an optimization direction. However, we set MINIMIZE as a
# placeholder direction.
StudyDirection.MINIMIZE,
StudyDirection.MINIMIZE,
],
)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=10000)

fig, ax = plt.subplots(figsize=(8, 6))
plot_grid_archive_heatmap(study, ax=ax)
plt.savefig("archive.png")
plt.show()
```

Expand Down
44 changes: 44 additions & 0 deletions package/visualization/plot_pyribs/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import matplotlib.pyplot as plt
import optuna
import optunahub


module = optunahub.load_module("samplers/cmamae")
CmaMaeSampler = module.CmaMaeSampler

plot_pyribs = optunahub.load_module(package="visualization/plot_pyribs")
plot_grid_archive_heatmap = plot_pyribs.plot_grid_archive_heatmap


def objective(trial: optuna.trial.Trial) -> float:
"""Returns an objective followed by two measures."""
x = trial.suggest_float("x", -10, 10)
y = trial.suggest_float("y", -10, 10)
trial.set_user_attr("m0", 2 * x)
trial.set_user_attr("m1", x + y)
return x**2 + y**2


if __name__ == "__main__":
sampler = CmaMaeSampler(
param_names=["x", "y"],
measure_names=["m0", "m1"],
archive_dims=[20, 20],
archive_ranges=[(-1, 1), (-1, 1)],
archive_learning_rate=0.1,
archive_threshold_min=-10,
n_emitters=1,
emitter_x0={
"x": 0,
"y": 0,
},
emitter_sigma0=0.1,
emitter_batch_size=20,
)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=10000)

fig, ax = plt.subplots(figsize=(8, 6))
plot_grid_archive_heatmap(study, ax=ax)
plt.savefig("archive.png")
plt.show()

0 comments on commit 1766c4b

Please sign in to comment.