Skip to content

Commit 968e465

Browse files
saitcakmakfacebook-github-bot
authored andcommitted
Error out if scipy optimizer does not support bounds / constraints (#2282)
Summary: Pull Request resolved: #2282 Scipy ignores the bounds / constraints with a warning, if the given method does not support them. This can lead to significant bugs if the warning is filtered out somewhere. Scipy behavior: https://github.com/scipy/scipy/blob/v1.13.0/scipy/optimize/_minimize.py#L577-L584 Reviewed By: Balandat Differential Revision: D55724311 fbshipit-source-id: 5167416976364846883f8d9fdee2cae8354d5f74
1 parent 2e67361 commit 968e465

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

botorch/generation/utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def _convert_nonlinear_inequality_constraints(
4242
"take a list of tuples. Passing a list of callables "
4343
"will result in an error in future versions.",
4444
DeprecationWarning,
45+
stacklevel=3,
4546
)
4647

4748
return nlcs

botorch/optim/utils/timeout.py

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import time
10+
import warnings
1011
from typing import Any, Callable, Dict, Optional, Sequence, Tuple, Union
1112

1213
import numpy as np
@@ -77,6 +78,7 @@ def wrapped_callback(xk: np.ndarray) -> None:
7778
wrapped_callback = callback
7879

7980
try:
81+
warnings.filterwarnings("error", message="Method .* cannot handle")
8082
return optimize.minimize(
8183
fun=fun,
8284
x0=x0,

test/generation/test_gen.py

+29
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,35 @@ def test_gen_candidates_without_grad(self) -> None:
350350
options={"disp": False, "with_grad": False},
351351
)
352352

353+
def test_gen_candidates_scipy_invalid_method(self) -> None:
354+
"""Test with method that doesn't support constraint / bounds."""
355+
self._setUp(double=True, expand=True)
356+
acqf = qExpectedImprovement(self.model, best_f=self.f_best)
357+
with self.assertRaisesRegex(
358+
RuntimeWarning,
359+
"Method L-BFGS-B cannot handle constraints",
360+
):
361+
gen_candidates_scipy(
362+
initial_conditions=self.initial_conditions,
363+
acquisition_function=acqf,
364+
options={"method": "L-BFGS-B"},
365+
inequality_constraints=[
366+
(torch.tensor([0]), torch.tensor([1]), 0),
367+
(torch.tensor([1]), torch.tensor([-1]), -1),
368+
],
369+
)
370+
with self.assertRaisesRegex(
371+
RuntimeWarning,
372+
"Method Newton-CG cannot handle bounds",
373+
):
374+
gen_candidates_scipy(
375+
initial_conditions=self.initial_conditions,
376+
acquisition_function=acqf,
377+
options={"method": "Newton-CG"},
378+
lower_bounds=0,
379+
upper_bounds=1,
380+
)
381+
353382

354383
class TestRandomRestartOptimization(TestBaseCandidateGeneration):
355384
def test_random_restart_optimization(self):

0 commit comments

Comments
 (0)