Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bayes_opt/target_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def max(self) -> dict[str, Any] | None:
params = self.params[self.mask]
target_max_idx = np.argmax(target)

res = {"target": target_max, "params": dict(zip(self.keys, params[target_max_idx]))}
res = {"target": target_max, "params": self.array_to_params(params[target_max_idx])}

if self._constraint is not None:
constraint_values = self.constraint_values[self.mask]
Expand Down Expand Up @@ -672,7 +672,7 @@ def res(self) -> list[dict[str, Any]]:

return [{"target": target, "params": param} for target, param in zip(self.target, params)]

params = [dict(zip(self.keys, p)) for p in self.params]
params = [self.array_to_params(p) for p in self.params]

return [
{"target": target, "constraint": constraint_value, "params": param, "allowed": allowed}
Expand Down
68 changes: 68 additions & 0 deletions tests/test_target_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@ def test_max_with_constraint_identical_target_value():
assert space.max() == {"params": {"p1": 2, "p2": 3}, "target": 5, "constraint": -1}


def test_max_categorical() -> None:
PBOUNDS = {
"first_float": (0.0, 1.0),
"categorical_value": ("a", "b", "c", "d"),
"second_float": (0.0, 1.0),
}

def _f(first_float: float, categorical_value: str, second_float: float) -> float:
return second_float if categorical_value == "c" else first_float

space = TargetSpace(_f, PBOUNDS)
space.probe(params={"first_float": 0.1, "categorical_value": "a", "second_float": 0.1})
space.probe(params={"first_float": 0.1, "categorical_value": "b", "second_float": 0.9})
space.probe(params={"first_float": 0.1, "categorical_value": "c", "second_float": 0.8})
space.probe(params={"first_float": 0.1, "categorical_value": "d", "second_float": 0.9})

expected = {"first_float": 0.1, "categorical_value": "c", "second_float": 0.8}
assert space.max()["params"] == expected


def test_res():
PBOUNDS = {"p1": (0, 10), "p2": (1, 100)}
space = TargetSpace(target_func, PBOUNDS)
Expand All @@ -273,6 +293,54 @@ def test_res():
assert space.res() == expected_res


def test_res_categorical() -> None:
PBOUNDS = {"p1": (0, 10), "p2": ["a", "b", "c"]}

def _f(p1: float, p2: str) -> float:
return p1 + len(p2)

space = TargetSpace(_f, PBOUNDS)

assert space.res() == []
space.probe(params={"p1": 1, "p2": "a"})
space.probe(params={"p1": 5, "p2": "b"})
space.probe(params={"p1": 2, "p2": "c"})
space.probe(params={"p1": 2, "p2": "a"})

expected_res = [
{"params": {"p1": 1, "p2": "a"}, "target": 2},
{"params": {"p1": 5, "p2": "b"}, "target": 6},
{"params": {"p1": 2, "p2": "c"}, "target": 3},
{"params": {"p1": 2, "p2": "a"}, "target": 3},
]
assert len(space.res()) == 4
assert space.res() == expected_res


def test_res_categorical_with_constraints() -> None:
PBOUNDS = {"p1": (0, 10), "p2": ["a", "b", "c"]}

def _f(p1: float, p2: str) -> float:
return p1 + len(p2)

space = TargetSpace(_f, PBOUNDS, constraint=NonlinearConstraint(lambda p1, p2: p1 - 2, 0, 5))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Rename the unused constraint argument.

Line 326 will trip Ruff's ARG005. The callback still needs a second positional argument for the signature, but naming it _p2 avoids the lint failure cleanly.

🧹 Minimal fix
-    space = TargetSpace(_f, PBOUNDS, constraint=NonlinearConstraint(lambda p1, p2: p1 - 2, 0, 5))
+    space = TargetSpace(_f, PBOUNDS, constraint=NonlinearConstraint(lambda p1, _p2: p1 - 2, 0, 5))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
space = TargetSpace(_f, PBOUNDS, constraint=NonlinearConstraint(lambda p1, p2: p1 - 2, 0, 5))
space = TargetSpace(_f, PBOUNDS, constraint=NonlinearConstraint(lambda p1, _p2: p1 - 2, 0, 5))
🧰 Tools
🪛 Ruff (0.15.4)

[warning] 326-326: Unused lambda argument: p2

(ARG005)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_target_space.py` at line 326, The lambda passed into the
NonlinearConstraint for TargetSpace currently names its second positional
parameter p2 which is unused and triggers ARG005; update the callback signature
to use a discarded name (e.g., change the lambda from lambda p1, p2: ... to
lambda p1, _p2: ...) so the second argument remains present for the required
signature but avoids the linter error (affecting the TargetSpace /
NonlinearConstraint call site).


assert space.res() == []
space.probe(params={"p1": 1, "p2": "a"})
space.probe(params={"p1": 5, "p2": "b"})
space.probe(params={"p1": 2, "p2": "c"})
space.probe(params={"p1": 2, "p2": "a"})

expected_res = [
{"params": {"p1": 1, "p2": "a"}, "target": 2, "allowed": False, "constraint": -1},
{"params": {"p1": 5, "p2": "b"}, "target": 6, "allowed": True, "constraint": 3},
{"params": {"p1": 2, "p2": "c"}, "target": 3, "allowed": True, "constraint": 0},
{"params": {"p1": 2, "p2": "a"}, "target": 3, "allowed": True, "constraint": 0},
]
assert len(space.res()) == 4
assert space.res() == expected_res


def test_set_bounds():
pbounds = {"p1": (0, 1), "p3": (0, 3), "p2": (0, 2), "p4": (0, 4)}
space = TargetSpace(target_func, pbounds)
Expand Down