Skip to content

Commit

Permalink
Fix replace list with deque (#508)
Browse files Browse the repository at this point in the history
* fix: remove Queue

* fix: rm queue
  • Loading branch information
phi-friday authored Sep 8, 2024
1 parent 04670dd commit 2298f7c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 92 deletions.
48 changes: 9 additions & 39 deletions bayes_opt/bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from __future__ import annotations

from collections import deque

from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern

Expand All @@ -17,38 +19,6 @@
from bayes_opt.util import ensure_rng


class Queue:
"""Queue datastructure.
Append items in the end, remove items from the front.
"""

def __init__(self):
self._queue = []

@property
def empty(self):
"""Check whether the queue holds any items."""
return len(self) == 0

def __len__(self):
"""Return number of items in the Queue."""
return len(self._queue)

def __next__(self):
"""Remove and return first item in the Queue."""
if self.empty:
error_msg = "Queue is empty, no more objects to retrieve."
raise StopIteration(error_msg)
obj = self._queue[0]
self._queue = self._queue[1:]
return obj

def add(self, obj):
"""Add object to end of queue."""
self._queue.append(obj)


class Observable:
"""Inspired by https://www.protechtraining.com/blog/post/879#simple-observer."""

Expand Down Expand Up @@ -128,7 +98,7 @@ def __init__(
):
self._random_state = ensure_rng(random_state)
self._allow_duplicate_points = allow_duplicate_points
self._queue = Queue()
self._queue = deque()

if acquisition_function is None:
if constraint is None:
Expand Down Expand Up @@ -248,7 +218,7 @@ def probe(self, params, lazy=True):
maximize(). Otherwise it will evaluate it at the moment.
"""
if lazy:
self._queue.add(params)
self._queue.append(params)
else:
self._space.probe(params)
self.dispatch(Events.OPTIMIZATION_STEP)
Expand All @@ -271,11 +241,11 @@ def _prime_queue(self, init_points):
init_points: int
Number of parameters to prime the queue with.
"""
if self._queue.empty and self._space.empty:
if not self._queue and self._space.empty:
init_points = max(init_points, 1)

for _ in range(init_points):
self._queue.add(self._space.random_sample())
self._queue.append(self._space.random_sample())

def _prime_subscriptions(self):
if not any([len(subs) for subs in self._events.values()]):
Expand Down Expand Up @@ -311,10 +281,10 @@ def maximize(self, init_points=5, n_iter=25):
self._prime_queue(init_points)

iteration = 0
while not self._queue.empty or iteration < n_iter:
while self._queue or iteration < n_iter:
try:
x_probe = next(self._queue)
except StopIteration:
x_probe = self._queue.popleft()
except IndexError:
x_probe = self.suggest()
iteration += 1
self.probe(x_probe, lazy=False)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,23 @@ def reset(self):
optimizer.subscribe(event=Events.OPTIMIZATION_END, subscriber=tracker, callback=tracker.update_end)

optimizer.maximize(init_points=0, n_iter=0)
assert optimizer._queue.empty
assert not optimizer._queue
assert len(optimizer.space) == 1
assert tracker.start_count == 1
assert tracker.step_count == 1
assert tracker.end_count == 1

optimizer.set_gp_params(alpha=1e-2)
optimizer.maximize(init_points=2, n_iter=0)
assert optimizer._queue.empty
assert not optimizer._queue
assert len(optimizer.space) == 3
assert optimizer._gp.alpha == 1e-2
assert tracker.start_count == 2
assert tracker.step_count == 3
assert tracker.end_count == 2

optimizer.maximize(init_points=0, n_iter=2)
assert optimizer._queue.empty
assert not optimizer._queue
assert len(optimizer.space) == 5
assert tracker.start_count == 3
assert tracker.step_count == 5
Expand Down
50 changes: 0 additions & 50 deletions tests/test_queue.py

This file was deleted.

0 comments on commit 2298f7c

Please sign in to comment.