-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.py
26 lines (22 loc) · 940 Bytes
/
callback.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from skopt.callbacks import EarlyStopper
class NImprovementStopper(EarlyStopper):
"""Stop the optimization if the optimum did not change for the last n_iterations.
"""
def __init__(self, n_iterations=10):
super(EarlyStopper, self).__init__()
self.n_iterations = n_iterations
def _criterion(self, result):
total_iterations = len(result.func_vals)
if self.n_iterations is None:
# disable nIterationsStopper
return None
if total_iterations > self.n_iterations:
start = total_iterations-self.n_iterations-1
stop = total_iterations
for value in result.func_vals[start:stop]:
if value <= result.fun:
# current optimum was found during last n_iterations
return None
# optimium was not found during last n_iterations
return True
return None