|
1 | 1 | import pytest |
2 | 2 | import numpy as np |
| 3 | +from bayes_opt.constraint import ConstraintModel |
3 | 4 | from bayes_opt.target_space import TargetSpace |
4 | 5 | from bayes_opt.util import NotUniqueError |
5 | 6 |
|
@@ -94,6 +95,29 @@ def test_register(): |
94 | 95 | space.register(params={"p1": 5, "p2": 4}, target=9) |
95 | 96 |
|
96 | 97 |
|
| 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 | + |
97 | 121 | def test_probe(): |
98 | 122 | space = TargetSpace(target_func, PBOUNDS) |
99 | 123 |
|
|
0 commit comments