66
77from __future__ import annotations
88
9+ from collections import deque
10+
911from sklearn .gaussian_process import GaussianProcessRegressor
1012from sklearn .gaussian_process .kernels import Matern
1113
1719from bayes_opt .util import ensure_rng
1820
1921
20- class Queue :
21- """Queue datastructure.
22-
23- Append items in the end, remove items from the front.
24- """
25-
26- def __init__ (self ):
27- self ._queue = []
28-
29- @property
30- def empty (self ):
31- """Check whether the queue holds any items."""
32- return len (self ) == 0
33-
34- def __len__ (self ):
35- """Return number of items in the Queue."""
36- return len (self ._queue )
37-
38- def __next__ (self ):
39- """Remove and return first item in the Queue."""
40- if self .empty :
41- error_msg = "Queue is empty, no more objects to retrieve."
42- raise StopIteration (error_msg )
43- obj = self ._queue [0 ]
44- self ._queue = self ._queue [1 :]
45- return obj
46-
47- def add (self , obj ):
48- """Add object to end of queue."""
49- self ._queue .append (obj )
50-
51-
5222class Observable :
5323 """Inspired by https://www.protechtraining.com/blog/post/879#simple-observer."""
5424
@@ -140,7 +110,7 @@ def __init__(
140110 ):
141111 self ._random_state = ensure_rng (random_state )
142112 self ._allow_duplicate_points = allow_duplicate_points
143- self ._queue = Queue ()
113+ self ._queue = deque ()
144114
145115 if acquisition_function is None :
146116 if constraint is None :
@@ -260,7 +230,7 @@ def probe(self, params, lazy=True):
260230 maximize(). Otherwise it will evaluate it at the moment.
261231 """
262232 if lazy :
263- self ._queue .add (params )
233+ self ._queue .append (params )
264234 else :
265235 self ._space .probe (params )
266236 self .dispatch (Events .OPTIMIZATION_STEP )
@@ -283,11 +253,11 @@ def _prime_queue(self, init_points):
283253 init_points: int
284254 Number of parameters to prime the queue with.
285255 """
286- if self ._queue . empty and self ._space .empty :
256+ if not self ._queue and self ._space .empty :
287257 init_points = max (init_points , 1 )
288258
289259 for _ in range (init_points ):
290- self ._queue .add (self ._space .random_sample ())
260+ self ._queue .append (self ._space .random_sample ())
291261
292262 def _prime_subscriptions (self ):
293263 if not any ([len (subs ) for subs in self ._events .values ()]):
@@ -315,10 +285,10 @@ def maximize(self, init_points=5, n_iter=25):
315285 self ._prime_queue (init_points )
316286
317287 iteration = 0
318- while not self ._queue . empty or iteration < n_iter :
288+ while self ._queue or iteration < n_iter :
319289 try :
320- x_probe = next ( self ._queue )
321- except StopIteration :
290+ x_probe = self ._queue . popleft ( )
291+ except IndexError :
322292 x_probe = self .suggest ()
323293 iteration += 1
324294 self .probe (x_probe , lazy = False )
0 commit comments