Skip to content

Commit

Permalink
Add convergence status and reason to Convergence_checker_optuna
Browse files Browse the repository at this point in the history
  • Loading branch information
RichieHakim committed Mar 10, 2024
1 parent fe9ff92 commit f2a7ae4
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bnpm/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ def __init__(
self.max_trials = max_trials
self.max_duration = max_duration
self.num_trial = 0
self.converged = False
self.reason_converged = None
self.verbose = verbose

def check(
Expand Down Expand Up @@ -240,12 +242,15 @@ def check(

bests_recent = np.unique(self.bests[-self.n_patience:])
if self.num_trial > self.n_patience and ((np.abs(bests_recent.max() - bests_recent.min())/np.abs(self.best)) < self.tol_frac):
self.converged, self.reason_converged = True, 'tol_frac'
print(f'Stopping. Convergence reached. Best value ({self.best*10000}) over last ({self.n_patience}) trials fractionally changed less than ({self.tol_frac})') if self.verbose else None
study.stop()
if self.num_trial >= self.max_trials:
self.converged, self.reason_converged = True, 'max_trials'
print(f'Stopping. Trial number limit reached. num_trial={self.num_trial}, max_trials={self.max_trials}.') if self.verbose else None
study.stop()
if duration > self.max_duration:
self.converged, self.reason_converged = True, 'max_duration'
print(f'Stopping. Duration limit reached. study.duration={duration}, max_duration={self.max_duration}.') if self.verbose else None
study.stop()

Expand Down

0 comments on commit f2a7ae4

Please sign in to comment.