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 convergence criteria #1438

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion nevergrad/optimization/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from . import multiobjective as mobj


_STAGNATION_ZERO_UNTIL: int = 10 # steps until stagnation_rate starts returning positive


OptCls = tp.Union["ConfiguredOptimizer", tp.Type["Optimizer"]]
registry: Registry[OptCls] = Registry()
_OptimCallBack = tp.Union[
Expand Down Expand Up @@ -90,6 +93,9 @@ def __init__(
# you can also replace or reinitialize this random state
self.num_workers = int(num_workers)
self.budget = budget
self.last_best_modification = -1 # Last time index at which the best so far moved.
self.min_moo_loss: tp.Optional[tp.ArrayLike] = None # Minimum loss for each of multiple objective functions.
self.sum_moo_loss: tp.Optional[tp.ArrayLike] = None # Sum of losses for each of multiple objective functions.

# How do we deal with cheap constraints i.e. constraints which are fast and use low resources and easy ?
# True ==> we penalize them (infinite values for candidates which violate the constraint).
Expand Down Expand Up @@ -140,6 +146,13 @@ def __init__(
# If not needed, an optimizer can set this to True.
self._no_hypervolume = False

def stagnation_rate(self) -> float:
"""Returns .3 if the last 30% of the run did not improve any "best" criterion."""
return (
(self.num_tell - self.last_best_modification) / self.num_tell
if self.num_tell > _STAGNATION_ZERO_UNTIL
else 0.0

def _warn(self, msg: str, e: tp.Any) -> None:
"""Warns only once per warning type"""
if e not in self._sent_warnings:
Expand Down Expand Up @@ -363,6 +376,17 @@ def tell(self, candidate: p.Parameter, loss: tp.Loss) -> None:
# preprocess multiobjective loss
if isinstance(loss, np.ndarray):
candidate._losses = loss
if self.min_moo_loss is None:
self.min_moo_loss = np.array(loss, copy=True)
self.sum_moo_loss = np.array(loss, copy=True)
else:
self.sum_moo_loss += loss
mean_moo_loss = self.sum_moo_loss / (self.num_tell + 1)
for i in range(self.num_objectives):
# In MOO cases, we update min only if significant change.
if loss[i] < self.min_moo_loss[i] - (mean_moo_loss[i] - self.min_moo_loss[i]) / 100.0:
self.last_best_modification = self.num_tell
self.min_moo_loss[i] = min(self.min_moo_loss[i], loss[i]) # type: ignore
if not isinstance(loss, float):
loss = self._preprocess_multiobjective(candidate)
# call callbacks for logging etc...
Expand Down Expand Up @@ -422,7 +446,12 @@ def _update_archive_and_bests(self, candidate: p.Parameter, loss: tp.FloatLoss)
# rebuild best point may change, and which value did not track the updated value anyway
self.current_bests[name] = best
else:
if self.archive[x].get_estimation(name) <= self.current_bests[name].get_estimation(name):
difference = self.archive[x].get_estimation(name) - self.current_bests[name].get_estimation(
name
)
if difference <= 0.0: # TODO: are we sure we want <= and not < ?
if difference < 0:
self.last_best_modification = self.num_tell
self.current_bests[name] = self.archive[x]
# deactivated checks
# if not (np.isnan(loss) or loss == np.inf):
Expand Down
24 changes: 24 additions & 0 deletions nevergrad/optimization/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,27 @@ def test_pruning_calls() -> None:
opt.minimize(constant)
assert isinstance(opt.pruning, utils.Pruning)
assert opt.pruning._num_prunings < 4


def test_stagnation() -> None:
# Test in the single objective case.
optim = ng.optimizers.OnePlusOne(2, budget=400)
for u in range(optim.budget): # type: ignore
x = optim.ask()
v = int(sum(10 * ((x.value - 7.0) ** 2)))
optim.tell(x, v)
assert u > 20 or optim.stagnation_rate() < 0.8, f"At iteration {u}, we get {optim.stagnation_rate()}."
assert (
u < 380 or optim.stagnation_rate() > 0.8
), f"At iteration {u}, we get {optim.stagnation_rate()}."

# Test in the multi-objective case.
optim = ng.optimizers.DE(2, budget=800)
for u in range(optim.budget): # type: ignore
x = optim.ask()
v = int(sum(10 * ((x.value - 3.0) ** 2)))
optim.tell(x, ((max(0, v)), max(0, 5 - v)))
assert u > 10 or optim.stagnation_rate() < 0.8, f"At iteration {u}, we get {optim.stagnation_rate()}."
assert (
u < 790 or optim.stagnation_rate() > 0.8
), f"At iteration {u}, we get {optim.stagnation_rate()}."