Skip to content

Commit

Permalink
Skip fixed feature enumerations in optimize_acqf_mixed that can't sat…
Browse files Browse the repository at this point in the history
…isfy the parameter constraints (pytorch#2614)

Summary:

When using `optimize_acqf_mixed`, some combinations of fixed features may result in the parameter constraints being impossible to satisfy. This causes `optimize_acqf` to error out. This diff skips the combinations of fixed features where the parameter constraints are impossible to satisfy.

Reviewed By: saitcakmak, esantorella

Differential Revision: D65514819
  • Loading branch information
David Eriksson authored and facebook-github-bot committed Nov 6, 2024
1 parent 8b13120 commit 0b7d557
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
54 changes: 37 additions & 17 deletions botorch/optim/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
qHypervolumeKnowledgeGradient,
)
from botorch.exceptions import InputDataError, UnsupportedError
from botorch.exceptions.errors import CandidateGenerationError
from botorch.exceptions.warnings import OptimizationWarning
from botorch.generation.gen import gen_candidates_scipy, TGenCandidates
from botorch.logging import logger
Expand Down Expand Up @@ -938,27 +939,46 @@ def optimize_acqf_mixed(

if q == 1:
ff_candidate_list, ff_acq_value_list = [], []
num_candidate_generation_failures = 0
for fixed_features in fixed_features_list:
candidate, acq_value = optimize_acqf(
acq_function=acq_function,
bounds=bounds,
q=q,
num_restarts=num_restarts,
raw_samples=raw_samples,
options=options or {},
inequality_constraints=inequality_constraints,
equality_constraints=equality_constraints,
nonlinear_inequality_constraints=nonlinear_inequality_constraints,
fixed_features=fixed_features,
post_processing_func=post_processing_func,
batch_initial_conditions=batch_initial_conditions,
ic_generator=ic_generator,
return_best_only=True,
**ic_gen_kwargs,
)
try:
candidate, acq_value = optimize_acqf(
acq_function=acq_function,
bounds=bounds,
q=q,
num_restarts=num_restarts,
raw_samples=raw_samples,
options=options or {},
inequality_constraints=inequality_constraints,
equality_constraints=equality_constraints,
nonlinear_inequality_constraints=nonlinear_inequality_constraints,
fixed_features=fixed_features,
post_processing_func=post_processing_func,
batch_initial_conditions=batch_initial_conditions,
ic_generator=ic_generator,
return_best_only=True,
**ic_gen_kwargs,
)
except CandidateGenerationError:
# if candidate generation fails, we skip this candidate
num_candidate_generation_failures += 1
continue
ff_candidate_list.append(candidate)
ff_acq_value_list.append(acq_value)

if len(ff_candidate_list) == 0:
raise CandidateGenerationError(
"Candidate generation failed for all `fixed_features`."
)
elif num_candidate_generation_failures > 0:
warnings.warn(
f"Candidate generation failed for {num_candidate_generation_failures} "
"combinations of `fixed_features`. To surpress this warning, make "
"sure all equality/inequality constraints can be satisfied by all "
"`fixed_features` in `fixed_features_list`.",
OptimizationWarning,
stacklevel=3,
)
ff_acq_values = torch.stack(ff_acq_value_list)
best = torch.argmax(ff_acq_values)
return ff_candidate_list[best], ff_acq_values[best]
Expand Down
38 changes: 38 additions & 0 deletions test/optim/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
qHypervolumeKnowledgeGradient,
)
from botorch.exceptions import InputDataError, UnsupportedError
from botorch.exceptions.errors import CandidateGenerationError
from botorch.exceptions.warnings import OptimizationWarning
from botorch.generation.gen import gen_candidates_scipy, gen_candidates_torch
from botorch.models import SingleTaskGP
Expand Down Expand Up @@ -1588,6 +1589,43 @@ def test_optimize_acqf_one_shot_large_q(self):
raw_samples=10,
)

def test_optimize_acqf_mixed_ff_with_constraint(self):
mock_acq_function = MockAcquisitionFunction()
with self.assertWarnsRegex(
OptimizationWarning,
"Candidate generation failed for 1 combinations of `fixed_features`. "
"To surpress this warning, make sure all equality/inequality "
"constraints can be satisfied by all `fixed_features` in "
"`fixed_features_list`.",
):
optimize_acqf_mixed(
acq_function=mock_acq_function,
q=1,
fixed_features_list=[{0: 0}, {0: 1}],
bounds=torch.stack([torch.zeros(3), 4 * torch.ones(3)]),
num_restarts=2,
raw_samples=10,
inequality_constraints=[
(torch.zeros(1), torch.ones(1), 1)
], # x[0] >= 1
)
# No fixed features satisfy the constraint
with self.assertRaisesRegex(
CandidateGenerationError,
"Candidate generation failed for all `fixed_features`.",
):
optimize_acqf_mixed(
acq_function=mock_acq_function,
q=1,
fixed_features_list=[{0: 0}],
bounds=torch.stack([torch.zeros(3), 4 * torch.ones(3)]),
num_restarts=2,
raw_samples=10,
inequality_constraints=[
(torch.zeros(1), torch.ones(1), 1)
], # x[0] >= 1
)


class TestOptimizeAcqfDiscrete(BotorchTestCase):
def test_optimize_acqf_discrete(self):
Expand Down

0 comments on commit 0b7d557

Please sign in to comment.