Skip to content

Commit 8bf1ef8

Browse files
committed
Test registration of point without constraint value
1 parent 8acacf2 commit 8bf1ef8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

tests/test_target_space.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import numpy as np
3+
from bayes_opt.constraint import ConstraintModel
34
from bayes_opt.target_space import TargetSpace
45
from bayes_opt.util import NotUniqueError
56

@@ -94,6 +95,29 @@ def test_register():
9495
space.register(params={"p1": 5, "p2": 4}, target=9)
9596

9697

98+
def test_register_with_constraint():
99+
constraint = ConstraintModel(lambda x: x, -2, 2)
100+
space = TargetSpace(target_func, PBOUNDS, constraint=constraint)
101+
102+
assert len(space) == 0
103+
# registering with dict
104+
space.register(params={"p1": 1, "p2": 2}, target=3, constraint_value=0.)
105+
assert len(space) == 1
106+
assert all(space.params[0] == np.array([1, 2]))
107+
assert all(space.target == np.array([3]))
108+
assert all(space.constraint_values == np.array([0]))
109+
110+
# registering with array
111+
space.register(params={"p1": 5, "p2": 4}, target=9, constraint_value=2)
112+
assert len(space) == 2
113+
assert all(space.params[1] == np.array([5, 4]))
114+
assert all(space.target == np.array([3, 9]))
115+
assert all(space.constraint_values == np.array([0, 2]))
116+
117+
with pytest.raises(ValueError):
118+
space.register(params={"p1": 2, "p2": 2}, target=3)
119+
120+
97121
def test_probe():
98122
space = TargetSpace(target_func, PBOUNDS)
99123

0 commit comments

Comments
 (0)