Skip to content

Commit

Permalink
Keep 1+1 sigma unchanged if equal loss (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrapin authored Feb 26, 2021
1 parent 6692161 commit b59315d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
3 changes: 2 additions & 1 deletion nevergrad/functions/pyomo/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_abstract_model_with_constraints() -> None:

def test_pyomo_set() -> None:
def square2(m: tp.Any) -> float:
return (m.x - 1) ** 2
return (m.x - 1) ** 2 # type: ignore

model = pyomo.ConcreteModel()
model.P = pyomo.Set(initialize=list(range(1, 11)))
Expand All @@ -78,6 +78,7 @@ def square2(m: tp.Any) -> float:
model.constraint1 = pyomo.Constraint(rule=lambda m: m.x >= 2)

func = core.Pyomo(model)
func.parametrization.random_state.seed(12)
optimizer = ng.optimizers.OnePlusOne(parametrization=func.parametrization, budget=100)
recommendation = optimizer.minimize(func.function)

Expand Down
13 changes: 6 additions & 7 deletions nevergrad/optimization/optimizerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(
) -> None:
super().__init__(parametrization, budget=budget, num_workers=num_workers)
self._sigma: float = 1
self._previous_best_loss = float("inf")
self.use_pareto = use_pareto
all_params = paramhelpers.flatten_parameter(self.parametrization)
arity = max(
Expand Down Expand Up @@ -241,6 +242,8 @@ def _internal_ask_candidate(self) -> p.Parameter:

def _internal_tell(self, x: tp.ArrayLike, loss: tp.FloatLoss) -> None:
# only used for cauchy and gaussian
if self._previous_best_loss != loss:
self._sigma *= 2.0 if loss < self._previous_best_loss else 0.84
if self.mutation == "doerr" and self._doerr_current_best < float("inf") and self._doerr_index >= 0:
improvement = max(0.0, self._doerr_current_best - loss)
# Decay.
Expand All @@ -254,20 +257,16 @@ def _internal_tell(self, x: tp.ArrayLike, loss: tp.FloatLoss) -> None:
self._doerr_index = -1
if self.mutation == "doerr":
self._doerr_current_best = min(self._doerr_current_best, loss)
self._sigma *= 2.0 if loss <= self.current_bests["pessimistic"].mean else 0.84
if self.mutation == "adaptive":
factor = (
1.2 if loss <= self.current_bests["pessimistic"].mean else 0.731
) # 0.731 = 1.2**(-np.exp(1)-1)
factor = 1.2 if loss <= self._previous_best_loss else 0.731 # 0.731 = 1.2**(-np.exp(1)-1)
self._adaptive_mr = min(1.0, factor * self._adaptive_mr)
if self.mutation == "coordinatewise_adaptive":
factor = (
1.2 if loss < self.current_bests["pessimistic"].mean else 0.731
) # 0.731 = 1.2**(-np.exp(1)-1)
factor = 1.2 if loss < self._previous_best_loss else 0.731 # 0.731 = 1.2**(-np.exp(1)-1)
inds = self._modified_variables
self._velocity[inds] = np.clip(
self._velocity[inds] * factor, 1.0, self.arity_for_discrete_mutation / 4.0
)
self._previous_best_loss = self.current_bests["pessimistic"].mean # could be the current one


class ParametrizedOnePlusOne(base.ConfiguredOptimizer):
Expand Down

0 comments on commit b59315d

Please sign in to comment.