Skip to content

Commit

Permalink
Unflakify TestOptimizeAcqfMixed.test_continuous_step (#2578)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #2578

Due to randomized continuous & binary dimensions, the fixed continuous dimension would occasionally become part of the constraint and fail the test since the fixed value cannot satisfy the constraint. This diff updates the fixed dimension to ensure this cannot happen.

Reviewed By: Balandat

Differential Revision: D64475003

fbshipit-source-id: 03164639e462bc7583c0b1ba47cbeb9463085c4b
  • Loading branch information
saitcakmak authored and facebook-github-bot committed Oct 16, 2024
1 parent fb0c667 commit ca956e1
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions test/optim/test_optimize_acqf_mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def test_continuous_step(self):

best_f = model(X)
ei = ExpectedImprovement(model, best_f=best_f)
X_clone, ei_val = continuous_step(
X_new, ei_val = continuous_step(
opt_inputs=_make_opt_inputs(
acq_function=ei,
bounds=bounds,
Expand All @@ -363,15 +363,18 @@ def test_continuous_step(self):
discrete_dims=binary_dims,
current_x=X.clone(),
)
self.assertAllClose(X_clone[cont_dims], root[cont_dims])
self.assertAllClose(X_clone[binary_dims], X[binary_dims])
self.assertAllClose(X_new[cont_dims], root[cont_dims])
self.assertAllClose(X_new[binary_dims], X[binary_dims])

# Test with fixed features and constraints.
fixed_binary = int(binary_dims[0])
fixed_cont = int(cont_dims[0])
# We don't want fixed cont to be one of the first two indices,
# to avoid it being a part of the constraint. This ensures that.
# The fixed value of 0.5 cannot satisfy the constraint.
fixed_cont = int(cont_dims[:3].max())
X_ = X.clone()
X_[:2] = 1.0 # To satisfy the constraint.
X_clone, ei_val = continuous_step(
X_new, ei_val = continuous_step(
opt_inputs=_make_opt_inputs(
acq_function=ei,
bounds=bounds,
Expand All @@ -390,11 +393,11 @@ def test_continuous_step(self):
)
self.assertTrue(
torch.equal(
X_clone[[fixed_binary, fixed_cont]],
X_new[[fixed_binary, fixed_cont]],
torch.tensor([1.0, 0.5], device=self.device),
)
)
self.assertAllClose(X_clone[:2], X_[:2])
self.assertAllClose(X_new[:2], X_[:2])

# test edge case when all parameters are binary
root = torch.rand(d_bin)
Expand Down Expand Up @@ -422,7 +425,7 @@ def test_continuous_step(self):
ValueError,
"continuous_step requires current_x to be",
):
X_clone, ei_val = continuous_step(
X_new, ei_val = continuous_step(
opt_inputs=_make_opt_inputs(
acq_function=ei,
bounds=bounds,
Expand Down

0 comments on commit ca956e1

Please sign in to comment.