Add validity check functions for ContinuousConstraints - #909
Closed
AVHopp wants to merge 3 commits into
Closed
Conversation
For linear constraints, we can calculate and evaluate the constraints. For cardinality constraints, we raise an error and point the user to the general utility function. This is necessary as more context is needed for cardinality constraints and we'd need to change the interface.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a row-level validity API for continuous constraints so BayBE can validate continuous constraints directly against candidate configuration dataframes (needed for the ongoing LLMRecommender work), instead of relying solely on handing constraints off to BoTorch.
Changes:
- Add
ContinuousConstraint.get_invalid(df)(abstract) andContinuousConstraint.get_valid(df)(derived complement) to provide a common validation interface. - Implement
ContinuousLinearConstraint.get_invalid(df)for both intra- and interpoint semantics. - Add tests covering
get_invalid/get_validbehavior and document the new API in the changelog; cardinality constraints explicitly raise with a pointer to the existing helper.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
baybe/constraints/base.py |
Adds the get_invalid/get_valid API to the ContinuousConstraint base class. |
baybe/constraints/continuous.py |
Implements row-level invalid-index extraction for ContinuousLinearConstraint; adds an explicit “not supported” implementation for cardinality constraints. |
tests/constraints/test_constraints_continuous.py |
Adds unit tests for get_invalid/get_valid on continuous linear constraints and the cardinality “raises” behavior. |
CHANGELOG.md |
Documents the addition of get_invalid/get_valid for continuous constraint validation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+365
to
+366
| Returns: | ||
| None. Always raises NotImplementedError. |
Comment on lines
+219
to
+235
| if missing := self._required_parameters - set(df.columns): | ||
| raise ValueError( | ||
| f"'{self.__class__.__name__}' requires columns {missing} " | ||
| f"which are missing from the dataframe." | ||
| ) | ||
| series = pd.Series( | ||
| sum( | ||
| df[p].to_numpy() * c for p, c in zip(self.parameters, self.coefficients) | ||
| ), | ||
| index=df.index, | ||
| ) | ||
| condition = ThresholdCondition(threshold=self.rhs, operator=self.operator) | ||
| if self.is_interpoint: | ||
| # Aggregate across all batch rows (mirrors BoTorch interpoint semantics). | ||
| satisfied = bool(condition.evaluate(pd.Series([float(series.sum())]))[0]) | ||
| return pd.Index([]) if satisfied else df.index | ||
| return df.index[~condition.evaluate(series)] |
Collaborator
Author
There was a problem hiding this comment.
Let me ask @AdrianSosic and @Scienfitz what they'd prefer here: New logic or re-using what we have?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds
get_validandget_invalidfunctions forContinuousConstraintobjects.Previously, we never needed those checks and only handed the constraints directly to Botorch. Now, with the current implementation efforts of the
LLMRecommender, we need to be able to validate those kind of constraints on our own.Design note: To keep the same interface, these functions also simply accedpt dataframes. As a consequence, they cannot directly be used for cardinality constraints. Consequently, the corresponding function raises an error and points the user to the corresponding helper.