Skip to content

Commit 7715ff4

Browse files
Waschenbacherfacebook-github-bot
authored andcommitted
Add InfeasibilityError exception (#2652)
Summary: <!-- Thank you for sending the PR! We appreciate you spending the time to make BoTorch better. Help us understand your motivation by explaining why you decided to make this change. You can learn more about contributing to BoTorch here: https://github.com/pytorch/botorch/blob/main/CONTRIBUTING.md --> ## Motivation Add customised error for infeasible problem. Details see #2631 ### Have you read the [Contributing Guidelines on pull requests](https://github.com/pytorch/botorch/blob/main/CONTRIBUTING.md#pull-requests)? Yes. Pull Request resolved: #2652 Test Plan: The customised error is tested in a similar way as the other customised errors. ## Related PRs (If this PR adds or changes functionality, please take some time to update the docs at https://github.com/pytorch/botorch, and link to your PR here.) Reviewed By: saitcakmak Differential Revision: D67280696 Pulled By: Balandat fbshipit-source-id: 581dbe23d304966632c83feb1286071d7fbddade
1 parent ef7098a commit 7715ff4

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

botorch/exceptions/errors.py

+6
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,9 @@ def __init__(self, /, *args: Any, current_x: npt.NDArray, **kwargs: Any) -> None
8686
"""
8787
super().__init__(*args, **kwargs)
8888
self.current_x = current_x
89+
90+
91+
class InfeasibilityError(BotorchError, ValueError):
92+
r"""Exception raised when infeasibility occurs."""
93+
94+
pass

botorch/utils/sampling.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import numpy.typing as npt
2828
import scipy
2929
import torch
30-
from botorch.exceptions.errors import BotorchError
30+
from botorch.exceptions.errors import BotorchError, InfeasibilityError
3131
from botorch.exceptions.warnings import UserInputWarning
3232
from botorch.sampling.qmc import NormalQMCEngine
3333
from botorch.utils.transforms import unnormalize
@@ -249,7 +249,7 @@ def sample_polytope(
249249
"""
250250
# Check that starting point satisfies the constraints.
251251
if not ((slack := A @ x0 - b) <= 0).all():
252-
raise ValueError(
252+
raise InfeasibilityError(
253253
f"Starting point does not satisfy the constraints. Inputs: {A=},"
254254
f"{b=}, {x0=}, A@x0-b={slack}."
255255
)
@@ -442,7 +442,7 @@ def find_interior_point(
442442
)
443443

444444
if result.status == 2:
445-
raise ValueError(
445+
raise InfeasibilityError(
446446
"No feasible point found. Constraint polytope appears empty. "
447447
+ "Check your constraints."
448448
)
@@ -524,7 +524,7 @@ def __init__(
524524
if self.feasible(interior_point):
525525
self.x0 = interior_point
526526
else:
527-
raise ValueError("The given input point is not feasible.")
527+
raise InfeasibilityError("The given input point is not feasible.")
528528
else:
529529
self.x0 = self.find_interior_point()
530530

test/exceptions/test_errors.py

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
BotorchTensorDimensionError,
1212
CandidateGenerationError,
1313
DeprecationError,
14+
InfeasibilityError,
1415
InputDataError,
1516
OptimizationGradientError,
1617
OptimizationTimeoutError,
@@ -28,6 +29,7 @@ def test_botorch_exception_hierarchy(self):
2829
InputDataError,
2930
UnsupportedError,
3031
BotorchTensorDimensionError,
32+
InfeasibilityError,
3133
]:
3234
self.assertIsInstance(ErrorClass(), BotorchError)
3335

@@ -38,6 +40,7 @@ def test_raise_botorch_exceptions(self):
3840
CandidateGenerationError,
3941
InputDataError,
4042
UnsupportedError,
43+
InfeasibilityError,
4144
):
4245
with self.assertRaises(ErrorClass):
4346
raise ErrorClass("message")

0 commit comments

Comments
 (0)