Skip to content
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 BIPOP-CMA-ES support in CmaEsSampler #4464

Merged
merged 27 commits into from
May 17, 2023

Conversation

cross32768
Copy link
Contributor

@cross32768 cross32768 commented Feb 24, 2023

Motivation

Current CmaEsSampler has IPOP-CMA-ES as the restart strategy.
BIPOP-CMA-ES is a kind of extension of IPOP-CMA-ES to improve performance in some problems by adding device about the increment of population size.
I want to consider adding BIPOP-CMA-ES as new option.

Description of the changes

  • Add BIPOP-CMA-ES option in optuna/samplers/_cmaes.py . Users can use this new feature by optuna.samplers.CmaEsSampler(restart_strategy="bipop")
    • Add population increment logic of BIPOP-CMA-ES in sampler_relative method of CmaEsSampler
    • Add some system_attrs to manage counter variable required for BIPOP-CMA-ES
    • Change the return format of _restore_optimizer method of CmaEsSampler and add code to load counter variable like n_restarts other place
    • Change the instance variable name _popsize to _initial_popsize because its more adequate
    • Add comments and docstring to describe about BIPOP-CMA-ES
  • Add test for BIPOP-CMA-ES in tests/sampler_tests/test_cmaes.py

@github-actions github-actions bot added the optuna.samplers Related to the `optuna.samplers` submodule. This is automatically labeled by github-actions. label Feb 24, 2023
@cross32768
Copy link
Contributor Author

@c-bata Could you review this PR?

@cross32768
Copy link
Contributor Author

This gif shows behavior of BIPOP-CMA-ES on himmelblau function.
bipop_himmelblau

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2023

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Mar 5, 2023
@HideakiImamura
Copy link
Member

@Alnusjaponica Could you review this PR?

@github-actions github-actions bot removed the stale Exempt from stale bot labeling. label Mar 6, 2023
Copy link
Collaborator

@Alnusjaponica Alnusjaponica left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my delayed reply. I made some small suggestions mainly for code comments. If you don't agree with them, please disregard them.

optuna/samplers/_cmaes.py Outdated Show resolved Hide resolved
optuna/samplers/_cmaes.py Outdated Show resolved Hide resolved
optuna/samplers/_cmaes.py Outdated Show resolved Hide resolved
optuna/samplers/_cmaes.py Outdated Show resolved Hide resolved
optuna/samplers/_cmaes.py Outdated Show resolved Hide resolved
optuna/samplers/_cmaes.py Outdated Show resolved Hide resolved
optuna/samplers/_cmaes.py Outdated Show resolved Hide resolved
optuna/samplers/_cmaes.py Outdated Show resolved Hide resolved
@cross32768
Copy link
Contributor Author

@Alnusjaponica Thanks you for comments. I fixed all points.

Co-authored-by: Shinichi Hemmi <50256998+Alnusjaponica@users.noreply.github.com>
@codecov-commenter
Copy link

codecov-commenter commented Mar 14, 2023

Codecov Report

Merging #4464 (bc3f550) into master (caf38ee) will increase coverage by 0.00%.
The diff coverage is 94.59%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@           Coverage Diff           @@
##           master    #4464   +/-   ##
=======================================
  Coverage   90.89%   90.89%           
=======================================
  Files         184      184           
  Lines       13953    14009   +56     
=======================================
+ Hits        12682    12733   +51     
- Misses       1271     1276    +5     
Impacted Files Coverage Δ
optuna/samplers/_cmaes.py 97.20% <94.59%> (-1.06%) ⬇️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@toshihikoyanase toshihikoyanase added the feature Change that does not break compatibility, but affects the public interfaces. label Mar 14, 2023
Co-authored-by: Shinichi Hemmi <50256998+Alnusjaponica@users.noreply.github.com>
@github-actions
Copy link
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Mar 30, 2023
@cross32768 cross32768 removed the stale Exempt from stale bot labeling. label Mar 31, 2023
Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay in my review. It works fine, but there seems to be room for improvement on how to restore the optimizer. I left one suggestion.

optuna/samplers/_cmaes.py Outdated Show resolved Hide resolved
@github-actions
Copy link
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Apr 19, 2023
@cross32768 cross32768 removed the stale Exempt from stale bot labeling. label Apr 20, 2023
@cross32768 cross32768 modified the milestone: v3.2.0 Apr 27, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2023

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label May 4, 2023
@cross32768 cross32768 removed the stale Exempt from stale bot labeling. label May 8, 2023
@cross32768 cross32768 changed the title Add BIPOP-CMA-ES support in CmaEsSampler. Add BIPOP-CMA-ES support in CmaEsSampler May 11, 2023
@c-bata
Copy link
Member

c-bata commented May 17, 2023

I checked the performance with Rastrigin-2D function, which is the similar settings as c-bata/goptuna#136, and confirmed that BIPOP-CMA-ES works as expected.

rastrigin-dim-2-62028a7346b78ad052b2895c4ff81af1fbc340227f84536bbad2b946de73d972

https://gist.github.com/c-bata/0e739d661e21a5bc4c2ddf2141bf6a9e

Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! The update looks great to me.

Just for the record, I confirmed that BIPOP-CMA-ES does not take over the optimization history of IPOP-CMA-ES with the following Python script.

import optuna
from optuna_dashboard import run_server


seed = 1
storage = optuna.storages.InMemoryStorage()


def objective(trial: optuna.Trial) -> float:
    x = trial.suggest_float('x', -10, 10)
    y = trial.suggest_float('y', -10, 10)
    system_attrs = trial.storage.get_trial_system_attrs(trial._trial_id)
    for key, value in system_attrs.items():
        if "optimizer" in key:
            trial.set_user_attr(key, f"{value[:5]}...")
        else:
            trial.set_user_attr(key, value)
    return x ** 2 + y ** 2


if __name__ == '__main__':

    study1 = optuna.create_study(sampler=optuna.samplers.RandomSampler(seed=seed), storage=storage, study_name="random-to-bipop")
    study1.optimize(objective, n_trials=100)
    study1 = optuna.load_study(sampler=optuna.samplers.CmaEsSampler(restart_strategy="bipop", seed=seed), storage=storage, study_name="random-to-bipop")
    study1.optimize(objective, n_trials=100)

    study2 = optuna.create_study(sampler=optuna.samplers.CmaEsSampler(restart_strategy="ipop", seed=seed), storage=storage, study_name="ipop-to-bipop")
    study2.optimize(objective, n_trials=100)
    study2 = optuna.load_study(sampler=optuna.samplers.CmaEsSampler(restart_strategy="bipop", seed=seed), storage=storage, study_name="ipop-to-bipop")
    study2.optimize(objective, n_trials=100)

    for t1, t2 in zip(study1.trials[100:], study2.trials[100:]):
        assert t1.params == t2.params and t1.value == t2.value
    run_server(storage, port=8080)

@c-bata c-bata added this to the v3.2.0 milestone May 17, 2023
@c-bata c-bata merged commit 387041c into optuna:master May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Change that does not break compatibility, but affects the public interfaces. optuna.samplers Related to the `optuna.samplers` submodule. This is automatically labeled by github-actions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants