Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix: Minimization with bounds properly minimize targets #462

Merged
merged 8 commits into from
Jan 15, 2025
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`NumericalTarget` now raises an error
- Crash when using `ContinuousCardinalityConstraint` caused by an unintended interplay
between constraints and dropped parameters yielding empty parameter sets
- Minimizing a single `NumericalTarget` with specified bounds/transformation via
`SingleTargetObjective` no longer erroneously maximizes it

### Removed
- `botorch_function_wrapper` utility for creating lookup callables
Expand Down
13 changes: 12 additions & 1 deletion baybe/objectives/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from baybe.objectives.base import Objective
from baybe.targets.base import Target
from baybe.targets.enum import TargetMode
from baybe.targets.numerical import NumericalTarget
from baybe.utils.dataframe import get_transform_objects, pretty_print_df
from baybe.utils.plotting import to_string

Expand Down Expand Up @@ -86,7 +88,16 @@ def transform(

target_data = df[self._target.name].copy()

return self._target.transform(target_data).to_frame()
out = self._target.transform(target_data).to_frame()

# TODO: Remove hotfix (https://github.com/emdgroup/baybe/issues/460)
if (
isinstance(t := self._target, NumericalTarget)
and t.mode is TargetMode.MIN
and t.bounds.is_bounded
):
out = -out
return out


# Collect leftover original slotted classes processed by `attrs.define`
Expand Down
19 changes: 19 additions & 0 deletions tests/test_objective.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Tests for the objective module."""

import numpy as np
import pandas as pd
import pytest
from cattrs import IterableValidationError

from baybe.objectives.desirability import DesirabilityObjective, scalarize
from baybe.objectives.enum import Scalarizer
from baybe.objectives.single import SingleTargetObjective
from baybe.parameters.numerical import NumericalContinuousParameter
from baybe.recommenders import BotorchRecommender
from baybe.targets import NumericalTarget


Expand Down Expand Up @@ -93,3 +96,19 @@ def test_desirability_scalarization(values, scalarizer, weights, expected):
"""The desirability scalarization yields the expected result."""
actual = scalarize(values, scalarizer, weights)
assert np.array_equal(actual, expected), (expected, actual)


@pytest.mark.parametrize(
("mode", "bounds", "opt"),
[("MIN", None, 0), ("MAX", None, 1), ("MIN", (0, 1), 0), ("MAX", (0, 1), 1)],
)
def test_single_objective(mode, bounds, opt):
"""Recommendations yield expected results with and without bounded objective."""
searchspace = NumericalContinuousParameter("p", [0, 1]).to_searchspace()
objective = NumericalTarget("t", mode=mode, bounds=bounds).to_objective()
recommender = BotorchRecommender()
measurements = pd.DataFrame(
{"p": np.linspace(0, 1, 100), "t": np.linspace(0, 1, 100)}
)
rec = recommender.recommend(1, searchspace, objective, measurements)
assert np.isclose(rec["p"].item(), opt)
Loading