Skip to content
Closed
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
5 changes: 4 additions & 1 deletion baybe/campaign.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Are you sure this is constructed in the best possible manner?

  1. The fuzzy row match is not 100% free from a computaitional perspective, its application should thus only happen if needed. This PR as is adds cost for all paths
  2. all existing recommenders have the index correct by construction, applying a fuzzy row match here has no added benefit for them as its not needed and just adds cost
  3. I understand its needed for the LLM recommender. But since thats the only one, why are you not using the fuzzy row match within that recommender to set the correct index of the recommendations? This would result in the exact same behavior but adds no cost for all other paths

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It seems like this is even incorrect and I need to rethink this in general, but looking at it now, I agree that this should be part of the LLMRecommender. Closing this PR.

Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,10 @@ def recommend(

# Update metadata
if self.searchspace.type in (SearchSpaceType.DISCRETE, SearchSpaceType.HYBRID):
self._searchspace_metadata.loc[rec.index, _RECOMMENDED] = True
idxs_matched = fuzzy_row_match(
self.searchspace.discrete.exp_rep, rec, self.parameters
)
self._searchspace_metadata.loc[idxs_matched, _RECOMMENDED] = True

return rec

Expand Down
29 changes: 28 additions & 1 deletion tests/test_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pytest import param

from baybe.acquisition import qLogEI, qLogNEHVI, qTS, qUCB
from baybe.campaign import _EXCLUDED, Campaign
from baybe.campaign import _EXCLUDED, _RECOMMENDED, Campaign
from baybe.constraints.conditions import SubSelectionCondition
from baybe.constraints.discrete import DiscreteSelectionConstraint
from baybe.exceptions import IncompatibilityError, NotEnoughPointsLeftError
Expand Down Expand Up @@ -450,3 +450,30 @@ def test_cache_invalidation(
assert campaign._cached_recommendation is None
else:
assert campaign._cached_recommendation is not None


def test_recommended_metadata_uses_content_not_index():
"""Metadata marks the correct rows even when the recommender resets the index."""
params = [
NumericalDiscreteParameter("x", [1, 2, 3]),
NumericalDiscreteParameter("y", [10, 20]),
]
searchspace = SearchSpace.from_product(params)
objective = NumericalTarget("t").to_objective()
campaign = Campaign(searchspace, objective)

# Choose the original indices of the rows we want to recommend
original_idx = [2, 4]
rec_content = searchspace.discrete.exp_rep.iloc[original_idx].reset_index(drop=True)

mock_recommender = Mock(spec=RecommenderProtocol)
mock_recommender.recommend.return_value = rec_content
campaign.recommender = mock_recommender

rec = campaign.recommend(2)

# Verify that rec does not have the original indices but has a reset index
assert rec.index.tolist() == [0, 1]

recommended = campaign._searchspace_metadata[_RECOMMENDED]
assert list(recommended[recommended].index) == original_idx
Loading