Skip to content

Commit 4d0e5a0

Browse files
committed
adapt search class to new stop feature
1 parent 620bfdb commit 4d0e5a0

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/gradient_free_optimizers/search.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ._results_manager import ResultsManager
1313
from ._objective_adapter import ObjectiveAdapter
1414
from ._memory import CachedObjectiveAdapter
15+
from ._stopping_conditions import OptimizationStopper
1516

1617

1718
class Search(TimesTracker, SearchStatistics):
@@ -49,8 +50,6 @@ def _initialization(self):
4950
self.n_init_total += 1
5051
self.n_init_search += 1
5152

52-
self.stop.update(self.p_bar.score_best, self.score_l)
53-
5453
@TimesTracker.iter_time
5554
def _iteration(self):
5655
self.best_score = self.p_bar.score_best
@@ -68,8 +67,6 @@ def _iteration(self):
6867
self.n_iter_total += 1
6968
self.n_iter_search += 1
7069

71-
self.stop.update(self.p_bar.score_best, self.score_l)
72-
7370
def search(
7471
self,
7572
objective_function,
@@ -96,7 +93,18 @@ def search(
9693

9794
for nth_trial in range(n_iter):
9895
self.search_step(nth_trial)
99-
if self.stop.check():
96+
97+
# Update stopper with current state
98+
current_score = self.score_l[-1] if self.score_l else -np.inf
99+
best_score = self.p_bar.score_best
100+
self.stopper.update(current_score, best_score, nth_trial)
101+
102+
if self.stopper.should_stop():
103+
# Log debugging information when stopping
104+
if "debug_stop" in self.verbosity:
105+
debug_info = self.stopper.get_debug_info()
106+
print("\nStopping condition debug info:")
107+
print(json.dumps(debug_info, indent=2))
100108
break
101109

102110
self.finish_search()
@@ -137,8 +145,11 @@ def init_search(
137145
self.verbosity = []
138146

139147
start_time = time.time()
140-
self.stop = StopRun(
141-
start_time, self.max_time, self.max_score, self.early_stopping
148+
self.stopper = OptimizationStopper(
149+
start_time=start_time,
150+
max_time=max_time,
151+
max_score=max_score,
152+
early_stopping=early_stopping,
142153
)
143154

144155
if "progress_bar" in self.verbosity:

0 commit comments

Comments
 (0)