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
@@ -128,7 +98,7 @@ def __init__(
12898 ):
12999 self ._random_state = ensure_rng (random_state )
130100 self ._allow_duplicate_points = allow_duplicate_points
131- self ._queue = Queue ()
101+ self ._queue = deque ()
132102
133103 if acquisition_function is None :
134104 if constraint is None :
@@ -248,7 +218,7 @@ def probe(self, params, lazy=True):
248218 maximize(). Otherwise it will evaluate it at the moment.
249219 """
250220 if lazy :
251- self ._queue .add (params )
221+ self ._queue .append (params )
252222 else :
253223 self ._space .probe (params )
254224 self .dispatch (Events .OPTIMIZATION_STEP )
@@ -271,11 +241,11 @@ def _prime_queue(self, init_points):
271241 init_points: int
272242 Number of parameters to prime the queue with.
273243 """
274- if self ._queue . empty and self ._space .empty :
244+ if not self ._queue and self ._space .empty :
275245 init_points = max (init_points , 1 )
276246
277247 for _ in range (init_points ):
278- self ._queue .add (self ._space .random_sample ())
248+ self ._queue .append (self ._space .random_sample ())
279249
280250 def _prime_subscriptions (self ):
281251 if not any ([len (subs ) for subs in self ._events .values ()]):
@@ -311,10 +281,10 @@ def maximize(self, init_points=5, n_iter=25):
311281 self ._prime_queue (init_points )
312282
313283 iteration = 0
314- while not self ._queue . empty or iteration < n_iter :
284+ while self ._queue or iteration < n_iter :
315285 try :
316- x_probe = next ( self ._queue )
317- except StopIteration :
286+ x_probe = self ._queue . popleft ( )
287+ except IndexError :
318288 x_probe = self .suggest ()
319289 iteration += 1
320290 self .probe (x_probe , lazy = False )
0 commit comments