-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from btjanaka/main
Add pyribs visualization
- Loading branch information
Showing
7 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Bryon Tjanaka | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
author: Bryon Tjanaka | ||
title: Pyribs Visualization Wrappers | ||
description: This visualizaton module provides wrappers around the visualization functions from pyribs, which is useful for plotting results from CmaMaeSampler. | ||
tags: [visualization, quality diversity, pyribs] | ||
optuna_versions: [4.0.0] | ||
license: MIT License | ||
--- | ||
|
||
## Class or Function Names | ||
|
||
- `plot_grid_archive_heatmap(study: optuna.Study, ax: plt.Axes, **kwargs)` | ||
|
||
- `study`: Optuna study with a sampler that uses pyribs. This function will plot the result archive from the sampler's scheduler. | ||
- `ax`: Axes on which to plot the heatmap. If None, we retrieve the current axes. | ||
- `**kwargs`: All remaining kwargs will be passed to [`grid_archive_heatmap`](https://docs.pyribs.org/en/stable/api/ribs.visualize.grid_archive_heatmap.html). | ||
|
||
## Installation | ||
|
||
```shell | ||
$ pip install ribs[visualize] | ||
``` | ||
|
||
## Example | ||
|
||
A minimal example would be the following: | ||
|
||
```python | ||
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() | ||
``` | ||
|
||
![Example of this Plot](images/archive.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import matplotlib.pyplot as plt | ||
import optuna | ||
from ribs.visualize import grid_archive_heatmap | ||
|
||
|
||
if TYPE_CHECKING: | ||
from matplotlib.axes._axes import Axes | ||
|
||
|
||
def plot_grid_archive_heatmap( # type: ignore | ||
study: optuna.Study, | ||
ax: Axes | None = None, | ||
**kwargs, | ||
) -> Axes: | ||
"""Wrapper around pyribs grid_archive_heatmap. | ||
Refer to the `grid_archive_heatmap | ||
<https://docs.pyribs.org/en/stable/api/ribs.visualize.grid_archive_heatmap.html>`_ | ||
function from pyribs for information. | ||
Args: | ||
study: Optuna study with a sampler that uses pyribs. This function will | ||
plot the result archive from the sampler's scheduler. | ||
ax: Axes on which to plot the heatmap. If None, we retrieve the current | ||
axes. | ||
kwargs: All remaining kwargs will be passed to `grid_archive_heatmap | ||
<https://docs.pyribs.org/en/stable/api/ribs.visualize.grid_archive_heatmap.html>`_. | ||
Returns: | ||
The axes on which the plot was created. | ||
""" | ||
if ax is None: | ||
ax = plt.gca() | ||
|
||
archive = study.sampler.scheduler.result_archive | ||
grid_archive_heatmap(archive, ax=ax, **kwargs) | ||
|
||
return ax | ||
|
||
|
||
__all__ = ["plot_grid_archive_heatmap"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.