-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pyribs visualization #191
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bee7d68
Add visualization tool
btjanaka 1766c4b
Update example
btjanaka 0fb885d
Docs fixes
btjanaka dd3cdca
Update example image
btjanaka 5197190
Fix docstring
btjanaka 8500053
Add visualization to CmaMaeSampler
btjanaka 08fd2fb
Note viz module
btjanaka 17878b4
Update package/visualization/plot_pyribs/README.md
nabenabe0928 e289417
Update package/visualization/plot_pyribs/README.md
nabenabe0928 e519d36
Update package/visualization/plot_pyribs/README.md
nabenabe0928 43e2e0a
Fix pre-commit
nabenabe0928 48e1bf4
Fix pre-commit
nabenabe0928 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,76 @@ | ||
--- | ||
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()` | ||
|
||
- `HEBOSampler(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. | ||
nabenabe0928 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- 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). | ||
nabenabe0928 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
nabenabe0928 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Installation | ||
|
||
```shell | ||
$ pip install ribs[visualize] | ||
``` | ||
|
||
## Example | ||
|
||
A minimal example would be the following: | ||
|
||
```python | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nabenabe0928 Do you think I should put this visualization example in the CMA-MAE sampler documentation too? |
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nabenabe0928 What is the correct way to link between plugins in the OptunaHub documentation? i.e., How can I provide a link to the
plot_pyribs
plugin here? Similarly, in theplot_pyribs
plugin, I want to provide a link to theCmaMaeSampler
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will quickly merge this PR, so you can surely open another PR later, but basically, the URL for each package is published like this, so you could link these URLs to your package!