Skip to content

Commit

Permalink
Add InfeasibilityError exception (#2652)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Waschenbacher authored and facebook-github-bot committed Dec 16, 2024
1 parent ef7098a commit 7715ff4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions botorch/exceptions/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@ def __init__(self, /, *args: Any, current_x: npt.NDArray, **kwargs: Any) -> None
"""
super().__init__(*args, **kwargs)
self.current_x = current_x


class InfeasibilityError(BotorchError, ValueError):
r"""Exception raised when infeasibility occurs."""

pass
8 changes: 4 additions & 4 deletions botorch/utils/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import numpy.typing as npt
import scipy
import torch
from botorch.exceptions.errors import BotorchError
from botorch.exceptions.errors import BotorchError, InfeasibilityError
from botorch.exceptions.warnings import UserInputWarning
from botorch.sampling.qmc import NormalQMCEngine
from botorch.utils.transforms import unnormalize
Expand Down Expand Up @@ -249,7 +249,7 @@ def sample_polytope(
"""
# Check that starting point satisfies the constraints.
if not ((slack := A @ x0 - b) <= 0).all():
raise ValueError(
raise InfeasibilityError(
f"Starting point does not satisfy the constraints. Inputs: {A=},"
f"{b=}, {x0=}, A@x0-b={slack}."
)
Expand Down Expand Up @@ -442,7 +442,7 @@ def find_interior_point(
)

if result.status == 2:
raise ValueError(
raise InfeasibilityError(
"No feasible point found. Constraint polytope appears empty. "
+ "Check your constraints."
)
Expand Down Expand Up @@ -524,7 +524,7 @@ def __init__(
if self.feasible(interior_point):
self.x0 = interior_point
else:
raise ValueError("The given input point is not feasible.")
raise InfeasibilityError("The given input point is not feasible.")
else:
self.x0 = self.find_interior_point()

Expand Down
3 changes: 3 additions & 0 deletions test/exceptions/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
BotorchTensorDimensionError,
CandidateGenerationError,
DeprecationError,
InfeasibilityError,
InputDataError,
OptimizationGradientError,
OptimizationTimeoutError,
Expand All @@ -28,6 +29,7 @@ def test_botorch_exception_hierarchy(self):
InputDataError,
UnsupportedError,
BotorchTensorDimensionError,
InfeasibilityError,
]:
self.assertIsInstance(ErrorClass(), BotorchError)

Expand All @@ -38,6 +40,7 @@ def test_raise_botorch_exceptions(self):
CandidateGenerationError,
InputDataError,
UnsupportedError,
InfeasibilityError,
):
with self.assertRaises(ErrorClass):
raise ErrorClass("message")
Expand Down

0 comments on commit 7715ff4

Please sign in to comment.