Skip to content

Commit

Permalink
Hotfix: Minimization with bounds properly minimize targets (#462)
Browse files Browse the repository at this point in the history
This PR hot-fixes #460 by multiplying the output of the
`objective.transform` with -1 if the target is bounded and to be
minimized.

This PR also introduces a test and a small example for verifying the
desired behavior. The solution implemented here is only temporary and
will be replaced with a proper mechanism soon.
  • Loading branch information
AdrianSosic authored Jan 15, 2025
2 parents 16b4041 + 4cec8ad commit deb3d5a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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)

0 comments on commit deb3d5a

Please sign in to comment.