Skip to content

Commit 27b7997

Browse files
committed
Move the instantiation of ConstraintModel
1 parent 05fc602 commit 27b7997

File tree

4 files changed

+32
-38
lines changed

4 files changed

+32
-38
lines changed

bayes_opt/bayesian_optimization.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,15 @@ def __init__(
105105
# bounds of its domain, and a record of the evaluations we have
106106
# done so far
107107
self._space = TargetSpace(
108-
f, pbounds, random_state=random_state, allow_duplicate_points=self._allow_duplicate_points
108+
f,
109+
pbounds,
110+
constraint=constraint,
111+
random_state=random_state,
112+
allow_duplicate_points=self._allow_duplicate_points,
109113
)
110114
if constraint is None:
111115
self.is_constrained = False
112116
else:
113-
constraint_ = ConstraintModel(
114-
constraint.fun,
115-
constraint.lb,
116-
constraint.ub,
117-
transform=self._space.kernel_transform,
118-
random_state=random_state,
119-
)
120-
self._space.set_constraint(constraint_)
121117
self.is_constrained = True
122118

123119
# Internal GP regressor

bayes_opt/target_space.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
from colorama import Fore
1111

12+
from bayes_opt.constraint import ConstraintModel
1213
from bayes_opt.exception import NotUniqueError
1314
from bayes_opt.parameter import BayesParameter, CategoricalParameter, FloatParameter, IntParameter, is_numeric
1415
from bayes_opt.util import ensure_rng
@@ -18,8 +19,8 @@
1819

1920
from numpy.random import RandomState
2021
from numpy.typing import NDArray
22+
from scipy.optimize import NonlinearConstraint
2123

22-
from bayes_opt.constraint import ConstraintModel
2324
from bayes_opt.parameter import BoundsMapping, ParamsType
2425

2526
Float = np.floating[Any]
@@ -71,6 +72,7 @@ def __init__(
7172
self,
7273
target_func: Callable[..., float] | None,
7374
pbounds: BoundsMapping,
75+
constraint: NonlinearConstraint | None = None,
7476
random_state: int | RandomState | None = None,
7577
allow_duplicate_points: bool | None = False,
7678
) -> None:
@@ -98,23 +100,23 @@ def __init__(
98100
self._cache: dict[tuple[float, ...], float | tuple[float, float | NDArray[Float]]] = {}
99101

100102
self._constraint: ConstraintModel | None = None
101-
102-
def set_constraint(self, constraint: ConstraintModel) -> None:
103-
"""Set the constraint model.
104-
105-
Parameters
106-
----------
107-
constraint : ConstraintModel
108-
The constraint model to be set.
109-
"""
110-
self._constraint = constraint
111-
112-
# preallocated memory for constraint fulfillment
113-
self._constraint_values: NDArray[Float]
114-
if constraint.lb.size == 1:
115-
self._constraint_values = np.empty(shape=(0), dtype=float)
103+
if constraint is None:
104+
self._constraint = None
116105
else:
117-
self._constraint_values = np.empty(shape=(0, self._constraint.lb.size), dtype=float)
106+
self._constraint = ConstraintModel(
107+
constraint.fun,
108+
constraint.lb,
109+
constraint.ub,
110+
transform=self.kernel_transform,
111+
random_state=random_state,
112+
)
113+
114+
# preallocated memory for constraint fulfillment
115+
self._constraint_values: NDArray[Float]
116+
if self._constraint.lb.size == 1:
117+
self._constraint_values = np.empty(shape=(0), dtype=float)
118+
else:
119+
self._constraint_values = np.empty(shape=(0, self._constraint.lb.size), dtype=float)
118120

119121
def __contains__(self, x: NDArray[Float]) -> bool:
120122
"""Check if this parameter has already been registered.

tests/test_acquisition.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ def constraint_func():
6464
@pytest.fixture
6565
def constrained_target_space(target_func):
6666
constraint_model = ConstraintModel(fun=lambda params: params["x"] + params["y"], lb=0.0, ub=1.0)
67-
space = TargetSpace(target_func=target_func, pbounds={"x": (1, 4), "y": (0, 3)})
68-
space.set_constraint(constraint_model)
69-
return space
67+
return TargetSpace(
68+
target_func=target_func, pbounds={"x": (1, 4), "y": (0, 3)}, constraint=constraint_model
69+
)
7070

7171

7272
class MockAcquisition(acquisition.AcquisitionFunction):

tests/test_target_space.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ def test_register():
9999

100100

101101
def test_register_with_constraint():
102-
constraint = ConstraintModel(lambda x: x, -2, 2, transform=None)
103-
space = TargetSpace(target_func, PBOUNDS)
104-
space.set_constraint(constraint)
102+
constraint = ConstraintModel(lambda x: x, -2, 2, transform=lambda x: x)
103+
space = TargetSpace(target_func, PBOUNDS, constraint=constraint)
105104

106105
assert len(space) == 0
107106
# registering with dict
@@ -196,8 +195,7 @@ def test_y_max():
196195
def test_y_max_with_constraint():
197196
PBOUNDS = {"p1": (0, 10), "p2": (1, 100)}
198197
constraint = ConstraintModel(lambda p1, p2: p1 - p2, -2, 2)
199-
space = TargetSpace(target_func, PBOUNDS)
200-
space.set_constraint(constraint)
198+
space = TargetSpace(target_func, PBOUNDS, constraint)
201199
assert space._target_max() is None
202200
space.probe(params={"p1": 1, "p2": 2}) # Feasible
203201
space.probe(params={"p1": 5, "p2": 1}) # Unfeasible
@@ -231,8 +229,7 @@ def test_max():
231229
def test_max_with_constraint():
232230
PBOUNDS = {"p1": (0, 10), "p2": (1, 100)}
233231
constraint = ConstraintModel(lambda p1, p2: p1 - p2, -2, 2)
234-
space = TargetSpace(target_func, PBOUNDS)
235-
space.set_constraint(constraint)
232+
space = TargetSpace(target_func, PBOUNDS, constraint=constraint)
236233

237234
assert space.max() is None
238235
space.probe(params={"p1": 1, "p2": 2}) # Feasible
@@ -245,8 +242,7 @@ def test_max_with_constraint():
245242
def test_max_with_constraint_identical_target_value():
246243
PBOUNDS = {"p1": (0, 10), "p2": (1, 100)}
247244
constraint = ConstraintModel(lambda p1, p2: p1 - p2, -2, 2)
248-
space = TargetSpace(target_func, PBOUNDS)
249-
space.set_constraint(constraint)
245+
space = TargetSpace(target_func, PBOUNDS, constraint=constraint)
250246

251247
assert space.max() is None
252248
space.probe(params={"p1": 1, "p2": 2}) # Feasible

0 commit comments

Comments
 (0)