Skip to content

Commit 075be2a

Browse files
Use explicit int64 sampling for integer parameter bounds
Fixes #623 Co-authored-by: Codex <noreply@openai.com>
1 parent af8b928 commit 075be2a

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎bayes_opt/parameter.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ def random_sample(
278278
The samples.
279279
"""
280280
random_state = ensure_rng(random_state)
281-
return random_state.randint(self.bounds[0], self.bounds[1] + 1, n_samples).astype(float)
281+
return random_state.randint(self.bounds[0], self.bounds[1] + 1, n_samples, dtype=np.int64).astype(
282+
float
283+
)
282284

283285
def to_float(self, value: int | float) -> float:
284286
"""Convert a parameter value to a float.

‎tests/test_parameter.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ def target_func(**kwargs):
8787
assert p1.kernel_transform(np.array([1.3, 3.6, 7.2])) == pytest.approx(np.array([1, 4, 7]))
8888

8989

90+
@pytest.mark.parametrize("bounds", [(3_000_000_000, 3_000_000_010), (-3_000_000_010, -3_000_000_000), (0, 5)])
91+
def test_int_random_sample_large_bounds(bounds):
92+
parameter = IntParameter("x", bounds)
93+
samples = parameter.random_sample(100, random_state=np.random.RandomState(42))
94+
repeated = parameter.random_sample(100, random_state=np.random.RandomState(42))
95+
assert samples.dtype == np.dtype(float)
96+
np.testing.assert_array_equal(samples, repeated)
97+
assert np.all(samples >= bounds[0])
98+
assert np.all(samples <= bounds[1])
99+
assert np.all(samples == np.floor(samples))
100+
assert bounds[0] in samples
101+
assert bounds[1] in samples
102+
103+
90104
def test_cat_parameters():
91105
fruit_ratings = {"apple": 1.0, "banana": 2.0, "mango": 5.0, "honeydew melon": -10.0, "strawberry": np.pi}
92106

0 commit comments

Comments
 (0)