Skip to content

Commit

Permalink
ESS optimizers: suppress divide-by-zero warnings; report n_eval
Browse files Browse the repository at this point in the history
For ESSOptimizer and SacessOptimizer:
* supporess divide-by-zero warnings
* report number of function evaluations
  • Loading branch information
dweindl committed Apr 30, 2024
1 parent 3edf785 commit 8c984a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pypesto/optimize/ess/ess.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ def _report_final(self):
formatter={"float": lambda x: "%.3g" % x},
):
self.logger.info(
f"-- Final ESS fval after {self.n_iter} "
f"iterations: {self.fx_best}. "
f"-- Final ESS fval after {self.n_iter} iterations, "
f"{self.evaluator.n_eval} function evaluations: {self.fx_best}. "
f"Exit flag: {self.exit_flag.name}. "
f"Num local solutions: {len(self.local_solutions)}."
)
Expand Down
15 changes: 12 additions & 3 deletions pypesto/optimize/ess/sacess.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,15 @@ def submit_solution(
# reject solution
self._rejections.value += 1

rel_change = (
abs(abs_change / self._best_known_fx.value)
if self._best_known_fx.value != 0
else np.nan
)
self._logger.debug(
f"Rejected solution from worker {sender_idx} "
f"abs change: {abs_change} "
f"rel change: {abs(abs_change / self._best_known_fx.value):.4g} "
f"rel change: {rel_change:.4g} "
f"(threshold: {self._rejection_threshold.value}) "
f"(total rejections: {self._rejections.value})."
)
Expand Down Expand Up @@ -577,7 +582,8 @@ def run(

self._logger.info(
f"sacess worker {self._worker_idx} iteration {ess.n_iter} "
f"(best: {self._best_known_fx})."
f"(best: {self._best_known_fx}, "
f"n_eval: {ess.evaluator.n_eval})."
)

ess.history.finalize(exitflag=ess.exit_flag.name)
Expand Down Expand Up @@ -650,10 +656,13 @@ def _maybe_adapt(self, problem: Problem):

def maybe_update_best(self, x: np.array, fx: float):
"""Maybe update the best known solution and send it to the manager."""
rel_change = (
abs((fx - self._best_known_fx) / fx) if fx != 0 else np.nan
)
self._logger.debug(
f"Worker {self._worker_idx} maybe sending solution {fx}. "
f"best known: {self._best_known_fx}, "
f"rel change: {(fx - self._best_known_fx) / fx:.4g}, "
f"rel change: {rel_change:.4g}, "
f"threshold: {self._acceptance_threshold}"
)

Expand Down

0 comments on commit 8c984a0

Please sign in to comment.