Skip to content

Commit

Permalink
Clean up support for legacy format non-linear constraints (#2627)
Browse files Browse the repository at this point in the history
Summary:

Legacy non-linear inequality constraint format has been deprecated since #1793.

Reviewed By: esantorella

Differential Revision: D66003879
  • Loading branch information
saitcakmak authored and facebook-github-bot committed Nov 15, 2024
1 parent b93b66f commit 561b41d
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 80 deletions.
15 changes: 1 addition & 14 deletions botorch/generation/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
from botorch.acquisition import AcquisitionFunction
from botorch.exceptions.errors import OptimizationGradientError
from botorch.exceptions.warnings import OptimizationWarning
from botorch.generation.utils import (
_convert_nonlinear_inequality_constraints,
_remove_fixed_features_from_optimization,
)
from botorch.generation.utils import _remove_fixed_features_from_optimization
from botorch.logging import logger
from botorch.optim.parameter_constraints import (
_arrayify,
Expand Down Expand Up @@ -136,16 +133,6 @@ def gen_candidates_scipy(
else:
reduced_domain = None not in fixed_features.values()

if nonlinear_inequality_constraints:
if not isinstance(nonlinear_inequality_constraints, list):
raise ValueError(
"`nonlinear_inequality_constraints` must be a list of tuples, "
f"got {type(nonlinear_inequality_constraints)}."
)
nonlinear_inequality_constraints = _convert_nonlinear_inequality_constraints(
nonlinear_inequality_constraints
)

if reduced_domain:
_no_fixed_features = _remove_fixed_features_from_optimization(
fixed_features=fixed_features,
Expand Down
30 changes: 0 additions & 30 deletions botorch/generation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

from __future__ import annotations

import warnings
from collections.abc import Callable
from dataclasses import dataclass

import torch

from botorch.acquisition import AcquisitionFunction, FixedFeatureAcquisitionFunction
from botorch.optim.parameter_constraints import (
_generate_unfixed_lin_constraints,
Expand All @@ -20,34 +18,6 @@
from torch import Tensor


def _convert_nonlinear_inequality_constraints(
nonlinear_inequality_constraints: list[Callable | tuple[Callable, bool]],
) -> list[tuple[Callable, bool]]:
"""Convert legacy defintions of nonlinear inequality constraints into the new
format. Assumes intra-point constraints.
"""
nlcs = []
legacy = False
# return nonlinear_inequality_constraints
for nlc in nonlinear_inequality_constraints:
if callable(nlc):
# old style --> convert
nlcs.append((nlc, True))
legacy = True
else:
nlcs.append(nlc)
if legacy:
warnings.warn(
"The `nonlinear_inequality_constraints` argument is expected "
"take a list of tuples. Passing a list of callables "
"will result in an error in future versions.",
DeprecationWarning,
stacklevel=3,
)

return nlcs


def _flip_sub_unique(x: Tensor, k: int) -> Tensor:
"""Get the first k unique elements of a single-dimensional tensor, traversing the
tensor from the back.
Expand Down
22 changes: 0 additions & 22 deletions test/generation/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,13 @@

from botorch.acquisition import FixedFeatureAcquisitionFunction
from botorch.generation.utils import (
_convert_nonlinear_inequality_constraints,
_flip_sub_unique,
_remove_fixed_features_from_optimization,
)
from botorch.utils.testing import BotorchTestCase, MockAcquisitionFunction


class TestGenerationUtils(BotorchTestCase):
def test_convert_nonlinear_inequality_constraints(self):
def nlc(x):
return x[..., 2]

def nlc2(x):
return x[..., 3]

nlcs = [nlc]
with self.assertWarns(DeprecationWarning):
new_nlcs = _convert_nonlinear_inequality_constraints(nlcs)
self.assertEqual(new_nlcs, [(nlc, True)])

nlcs = [(nlc, False)]
new_nlcs = _convert_nonlinear_inequality_constraints(nlcs)
self.assertEqual(new_nlcs, [(nlc, False)])

nlcs = [(nlc, False), nlc2]
with self.assertWarns(DeprecationWarning):
new_nlcs = _convert_nonlinear_inequality_constraints(nlcs)
self.assertEqual(new_nlcs, [(nlc, False), (nlc2, True)])

def test_flip_sub_unique(self):
for dtype in (torch.float, torch.double):
tkwargs = {"device": self.device, "dtype": dtype}
Expand Down
14 changes: 0 additions & 14 deletions test/optim/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,20 +953,6 @@ def nlc4(x):
)
self.assertEqual(candidates.size(), torch.Size([1, 3]))

# Constraints must be passed in as lists
with self.assertRaisesRegex(
ValueError,
"`nonlinear_inequality_constraints` must be a list of tuples, "
"got <class 'function'>.",
):
optimize_acqf(
acq_function=mock_acq_function,
bounds=bounds,
q=1,
nonlinear_inequality_constraints=nlc1,
num_restarts=num_restarts,
batch_initial_conditions=batch_initial_conditions,
)
# batch_initial_conditions must be feasible
with self.assertRaisesRegex(
ValueError,
Expand Down

0 comments on commit 561b41d

Please sign in to comment.