-
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 CMA-ES with user prior #160
Conversation
@c-bata Could you review this PR? |
def _calculate_initial_params( | ||
self, trans: _SearchSpaceTransform | ||
) -> tuple[np.ndarray, float, np.ndarray]: | ||
# NOTE(nabenabe): Except this method, everything is basically based on Optuna v4.0.0. |
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.
Please check the note here before the review 🙇
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_module(package="samplers/user_prior_cmaes") | ||
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) |
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 tried to execute this example, but bumped into AssertionErrors as shown below. Is this problem reproducible in your environment?
https://gist.github.com/c-bata/d76d810ceffd5d83fd695d7d7f942bc3
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 fixed this issue!
In principle, this was because the mean vector was out of bound of the parameter domain.
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 LGTM. Do you think we should use with_margin
option in the example?
|
||
if __name__ == "__main__": | ||
module = optunahub.load_module(package="samplers/user_prior_cmaes") | ||
sampler = module.UserPriorCmaEsSampler(param_names=["x", "y"], mu0=np.array([-48., 3.]), cov0=np.diag([2., 0.2])) |
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.
nit: We might want to consider specifying with_margin=True
since there is an integer parameter in the search space.
sampler = module.UserPriorCmaEsSampler(param_names=["x", "y"], mu0=np.array([-48., 3.]), cov0=np.diag([2., 0.2])) | |
sampler = module.UserPriorCmaEsSampler(param_names=["x", "y"], mu0=np.array([-48., 3.]), cov0=np.diag([2., 0.2]), with_margin=True) |
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.
Thank you for the suggestion!
I addressed your comment and confirmed the new code works:)
Contributor Agreements
Please read the contributor agreements and if you agree, please click the checkbox below.
Tip
Please follow the Quick TODO list to smoothly merge your PR.
Motivation
As the current CMA-ES implementation does not allow users to give an initial covariance matrix, I modified the CMA-ES so that we can provide a covariance matrix from the user side.
Description of the changes
TODO List towards PR Merge
Please remove this section if this PR is not an addition of a new package.
Otherwise, please check the following TODO list:
./template/
to create your package<COPYRIGHT HOLDER>
inLICENSE
of your package with your nameREADME.md
in your package__init__.py
README.md
README.md