6
6
7
7
from __future__ import annotations
8
8
9
+ from collections import deque
10
+
9
11
from sklearn .gaussian_process import GaussianProcessRegressor
10
12
from sklearn .gaussian_process .kernels import Matern
11
13
17
19
from bayes_opt .util import ensure_rng
18
20
19
21
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
-
52
22
class Observable :
53
23
"""Inspired by https://www.protechtraining.com/blog/post/879#simple-observer."""
54
24
@@ -128,7 +98,7 @@ def __init__(
128
98
):
129
99
self ._random_state = ensure_rng (random_state )
130
100
self ._allow_duplicate_points = allow_duplicate_points
131
- self ._queue = Queue ()
101
+ self ._queue = deque ()
132
102
133
103
if acquisition_function is None :
134
104
if constraint is None :
@@ -248,7 +218,7 @@ def probe(self, params, lazy=True):
248
218
maximize(). Otherwise it will evaluate it at the moment.
249
219
"""
250
220
if lazy :
251
- self ._queue .add (params )
221
+ self ._queue .append (params )
252
222
else :
253
223
self ._space .probe (params )
254
224
self .dispatch (Events .OPTIMIZATION_STEP )
@@ -271,11 +241,11 @@ def _prime_queue(self, init_points):
271
241
init_points: int
272
242
Number of parameters to prime the queue with.
273
243
"""
274
- if self ._queue . empty and self ._space .empty :
244
+ if not self ._queue and self ._space .empty :
275
245
init_points = max (init_points , 1 )
276
246
277
247
for _ in range (init_points ):
278
- self ._queue .add (self ._space .random_sample ())
248
+ self ._queue .append (self ._space .random_sample ())
279
249
280
250
def _prime_subscriptions (self ):
281
251
if not any ([len (subs ) for subs in self ._events .values ()]):
@@ -311,10 +281,10 @@ def maximize(self, init_points=5, n_iter=25):
311
281
self ._prime_queue (init_points )
312
282
313
283
iteration = 0
314
- while not self ._queue . empty or iteration < n_iter :
284
+ while self ._queue or iteration < n_iter :
315
285
try :
316
- x_probe = next ( self ._queue )
317
- except StopIteration :
286
+ x_probe = self ._queue . popleft ( )
287
+ except IndexError :
318
288
x_probe = self .suggest ()
319
289
iteration += 1
320
290
self .probe (x_probe , lazy = False )
0 commit comments