Skip to content

Remove restart_strategy and inc_popsize to simplify CmaEsSampler#6025

Merged
c-bata merged 16 commits intooptuna:masterfrom
sawa3030:deprecate-restart-strategy
May 1, 2025
Merged

Remove restart_strategy and inc_popsize to simplify CmaEsSampler#6025
c-bata merged 16 commits intooptuna:masterfrom
sawa3030:deprecate-restart-strategy

Conversation

@sawa3030
Copy link
Copy Markdown
Collaborator

@sawa3030 sawa3030 commented Mar 28, 2025

Motivation

Simplify the implementation of CmaEsSampler by removing support for restart strategies. The current implementation with restart_strategy will be put into OptunaHub

Summary of Changes

  • Removed support for restart_strategy and inc_popsize options in CmaEsSampler
  • Cleaned up related logic:
    • Removed associated attributes from _CmaEsAttrKeys, including popsize, n_restarts, n_restarts_with_large, poptype, small_n_eval, and large_n_eval
    • Eliminated usage of set_trial_system_attr since state tracking is no longer needed
  • Updated unit tests accordingly

@sawa3030 sawa3030 marked this pull request as ready for review April 4, 2025 06:07
@y0z y0z added the compatibility Change that breaks compatibility. label Apr 7, 2025
@y0z
Copy link
Copy Markdown
Member

y0z commented Apr 7, 2025

@c-bata @HideakiImamura
Could you review this PR?

Copy link
Copy Markdown
Member

@HideakiImamura HideakiImamura 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 PR. Basically, LGTM. I have several comments. PTAL.

versions without prior notice. See
https://github.com/optuna/optuna/releases/tag/v2.1.0.
.. warning::
Deprecated in v4.3.0. ``consider_prior`` argument will be removed in the future.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Deprecated in v4.3.0. ``consider_prior`` argument will be removed in the future.
Deprecated in v4.4.0. ``restart_strategy`` argument will be removed in the future.

Deprecated in v4.3.0. ``consider_prior`` argument will be removed in the future.
The removal of this feature is currently scheduled for v6.0.0,
but this schedule is subject to change.
See https://github.com/optuna/optuna/releases/tag/v4.3.0.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
See https://github.com/optuna/optuna/releases/tag/v4.3.0.
See https://github.com/optuna/optuna/releases/tag/v4.4.0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In addition, inc_popsize also should be deprecated and the explanation of popsize should be changed.

) -> None:
if restart_strategy is not None:
msg = _deprecated._DEPRECATION_WARNING_TEMPLATE.format(
name="`restart_strategy`", d_ver="4.3.0", r_ver="6.0.0"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
name="`restart_strategy`", d_ver="4.3.0", r_ver="6.0.0"
name="`restart_strategy`", d_ver="4.4.0", r_ver="6.0.0"

self._search_space = IntersectionSearchSpace()
self._consider_pruned_trials = consider_pruned_trials
self._restart_strategy = restart_strategy
self._initial_popsize = popsize
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The population size is fixed (here or in the sample_relative). How about renaming this self._popsize?

@sawa3030
Copy link
Copy Markdown
Collaborator Author

Thank you for the review. I’ll make the necessary updates later.
For now, I’ve added the original implementation to OptunaHub.

@nabenabe0928 nabenabe0928 added this to the v4.4.0 milestone Apr 16, 2025
Copy link
Copy Markdown
Member

@HideakiImamura HideakiImamura 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. I have several another comments. Please take a look.

Comment on lines +167 to +171
Deprecated in v4.4.0. ``restart_strategy`` argument will be removed in the future.
The removal of this feature is currently scheduled for v6.0.0,
but this schedule is subject to change.
See https://github.com/optuna/optuna/releases/tag/v4.4.0.
`restart_strategy` will be supported in OptunaHub.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There seems to be a missing message that says you can't actually use the feature.

Comment on lines +181 to +182
.. warning::
Deprecated along with ``restart_strategy``.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Deprecation messages should be written at the same level of text as restart_strategy without simplification.

lr_adapt: bool = False,
source_trials: list[FrozenTrial] | None = None,
) -> None:
if restart_strategy is not None:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The deprecation message should also be emitted for inc_popsize.

@sawa3030
Copy link
Copy Markdown
Collaborator Author

@HideakiImamura Thank you for the review. I've made the update accordingly. PTAL!

Copy link
Copy Markdown
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.

Thank you for your pull request! I confirmed that this PR does not break the exisiting behavior using the following code.

import optuna
import optunahub
 
bbob = optunahub.load_module("benchmarks/bbob")
sphere2d = bbob.Problem(function_id=1, dimension=2, instance_id=1)

storage = optuna.storages.get_storage("sqlite:///cmaes_benchmark.db")

sampler = optuna.samplers.CmaEsSampler(seed=1)
study = optuna.create_study(
    sampler=sampler,
    directions=sphere2d.directions,
    study_name="cmaes_pr6025",
    storage=storage
)
study.optimize(sphere2d, n_trials=500)

cmaes_compare

I've also left a few minor suggestions.

lr_adapt: bool = False,
source_trials: list[FrozenTrial] | None = None,
) -> None:
if restart_strategy is not None or inc_popsize != 2:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about changing the default value for inc_popsize to -1?

Suggested change
if restart_strategy is not None or inc_popsize != 2:
if restart_strategy is not None or inc_popsize != -1:

This would allow us to show a warning message when users explicitly specify inc_popsize=2.

optimizer = self._init_optimizer(
trans, study.direction, population_size=self._initial_popsize
)
optimizer = self._init_optimizer(trans, study.direction, population_size=self._popsize)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we could simply removing population_size argument from _init_optimizer() method. What do you think?

Suggested change
optimizer = self._init_optimizer(trans, study.direction, population_size=self._popsize)
optimizer = self._init_optimizer(trans, study.direction)

Comment on lines +387 to +388
if self._popsize is None:
self._popsize = 4 + math.floor(3 * math.log(len(trans.bounds)))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These lines can be simply removed. You can access to optimizer.population_size property method instead.

Suggested change
if self._popsize is None:
self._popsize = 4 + math.floor(3 * math.log(len(trans.bounds)))

See https://github.com/CyberAgentAILab/cmaes/blob/f0749088ec53aca6f056c93faed71a775351591a/cmaes/_cma.py#L240-L243 for details.

Copy link
Copy Markdown
Member

@HideakiImamura HideakiImamura 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. Almost LGTM! I have two minor comments. PTAL.

or ``restart_strategy = 'bipop'`` is specified.

.. warning::
Deprecated in v4.4.0. ``restart_strategy`` argument will be removed in the future.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Deprecated in v4.4.0. ``restart_strategy`` argument will be removed in the future.
Deprecated in v4.4.0. ``inc_popsize`` argument will be removed in the future.

Comment on lines +186 to +187
From v4.4.0 onward, ``restart_strategy`` automatically falls back to ``None``, and
``restart_strategy`` will be supported in OptunaHub.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
From v4.4.0 onward, ``restart_strategy`` automatically falls back to ``None``, and
``restart_strategy`` will be supported in OptunaHub.
From v4.4.0 onward, ``inc_popsize`` automatically falls back to ``None``, and
``inc_popsize`` will be supported in OptunaHub.

@sawa3030 sawa3030 force-pushed the deprecate-restart-strategy branch from 8dc9671 to a87d238 Compare April 25, 2025 05:55
@sawa3030 sawa3030 force-pushed the deprecate-restart-strategy branch from a87d238 to 0245e8c Compare April 25, 2025 06:47
@sawa3030
Copy link
Copy Markdown
Collaborator Author

Still investigating the cause of the docs/readthedoc.org:optuna test failure in the latest commit.

@sawa3030
Copy link
Copy Markdown
Collaborator Author

@c-bata @HideakiImamura Thank you for your feedback and patience. Everything should be updated now. PTAL!

Copy link
Copy Markdown
Member

@HideakiImamura HideakiImamura 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! LGTM!

@HideakiImamura HideakiImamura removed their assignment May 1, 2025
Copy link
Copy Markdown
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.

LGTM! Thank you for your contribution.

Comment on lines -532 to -559
def optimizer_key_template(restart: int) -> str:
if self._restart_strategy is None:
return attr_prefix + "optimizer"
else:
return attr_prefix + "{}:restart_{}:optimizer".format(
self._restart_strategy, restart
)

def generation_attr_key_template(restart: int) -> str:
if self._restart_strategy is None:
return attr_prefix + "generation"
else:
return attr_prefix + "{}:restart_{}:generation".format(
self._restart_strategy, restart
)

def popsize_attr_key_template() -> str:
if self._restart_strategy is None:
return attr_prefix + "popsize"
else:
return attr_prefix + "{}:popsize".format(self._restart_strategy)

def n_restarts_attr_key_template() -> str:
if self._restart_strategy is None:
return attr_prefix + "n_restarts"
else:
return attr_prefix + "{}:n_restarts".format(self._restart_strategy)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could simply remove the _CmaEsAttrKeys class and return a string object, since all callable functions have been removed in this PR. This could be addressed in a follow-up PR.

Copy link
Copy Markdown
Collaborator Author

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 created the follow-up PR, #6068

@c-bata c-bata merged commit ddcc20e into optuna:master May 1, 2025
14 checks passed
@sawa3030 sawa3030 deleted the deprecate-restart-strategy branch November 14, 2025 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compatibility Change that breaks compatibility.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants